Allow apps to specify locale for localisation

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2017-07-05 17:37:12 +02:00 committed by Georg Ehrke
parent d8921ccd85
commit ea380b2918
No known key found for this signature in database
GPG Key ID: 9D98FD9380A1CB43
3 changed files with 40 additions and 13 deletions

View File

@ -97,9 +97,10 @@ class Factory implements IFactory {
* *
* @param string $app * @param string $app
* @param string|null $lang * @param string|null $lang
* @param string|null $locale
* @return \OCP\IL10N * @return \OCP\IL10N
*/ */
public function get($app, $lang = null) { public function get($app, $lang = null, $locale = null) {
$app = \OC_App::cleanAppId($app); $app = \OC_App::cleanAppId($app);
if ($lang !== null) { if ($lang !== null) {
$lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang); $lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang);
@ -116,7 +117,7 @@ class Factory implements IFactory {
if (!isset($this->instances[$lang][$app])) { if (!isset($this->instances[$lang][$app])) {
$this->instances[$lang][$app] = new L10N( $this->instances[$lang][$app] = new L10N(
$this, $app, $lang, $this, $app, $lang, $locale,
$this->getL10nFilesForApp($app, $lang) $this->getL10nFilesForApp($app, $lang)
); );
} }

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
* *
* @author Georg Ehrke <oc.list@georgehrke.com> * @author Georg Ehrke <oc.list@georgehrke.com>
* @author Joas Schilling <coding@schilljs.com> * @author Joas Schilling <coding@schilljs.com>
* @author Thomas Citharel <tcit@tcit.fr>
* @author Roeland Jago Douma <roeland@famdouma.nl> * @author Roeland Jago Douma <roeland@famdouma.nl>
* *
* @license AGPL-3.0 * @license AGPL-3.0
@ -41,6 +42,9 @@ class L10N implements IL10N {
/** @var string Language of this object */ /** @var string Language of this object */
protected $lang; protected $lang;
/** @var string Locale of this object */
protected $locale;
/** @var string Plural forms (string) */ /** @var string Plural forms (string) */
private $pluralFormString = 'nplurals=2; plural=(n != 1);'; private $pluralFormString = 'nplurals=2; plural=(n != 1);';
@ -54,12 +58,14 @@ class L10N implements IL10N {
* @param IFactory $factory * @param IFactory $factory
* @param string $app * @param string $app
* @param string $lang * @param string $lang
* @param string $locale
* @param array $files * @param array $files
*/ */
public function __construct(IFactory $factory, $app, $lang, array $files) { public function __construct(IFactory $factory, $app, $lang, $locale, array $files) {
$this->factory = $factory; $this->factory = $factory;
$this->app = $app; $this->app = $app;
$this->lang = $lang; $this->lang = $lang;
$this->locale = $locale;
foreach ($files as $languageFile) { foreach ($files as $languageFile) {
$this->load($languageFile); $this->load($languageFile);
@ -75,6 +81,15 @@ class L10N implements IL10N {
return $this->lang; return $this->lang;
} }
/**
* The code (en_US, fr_CA, ...) of the locale that is used for this instance
*
* @return string locale
*/
public function getLocaleCode() {
return $this->locale;
}
/** /**
* Translating * Translating
* @param string $text The text we need a translation for * @param string $text The text we need a translation for
@ -143,17 +158,19 @@ class L10N implements IL10N {
* - jsdate: Returns the short JS date format * - jsdate: Returns the short JS date format
*/ */
public function l(string $type, $data = null, array $options = []) { public function l(string $type, $data = null, array $options = []) {
// Use the language of the instance if (null === $this->locale) {
$locale = $this->getLanguageCode(); // Use the language of the instance
if ($locale === 'sr@latin') { $this->locale = $this->getLanguageCode();
$locale = 'sr_latn'; }
if ($this->locale === 'sr@latin') {
$this->locale = 'sr_latn';
} }
if ($type === 'firstday') { if ($type === 'firstday') {
return (int) Calendar::getFirstWeekday($locale); return (int) Calendar::getFirstWeekday($this->locale);
} }
if ($type === 'jsdate') { if ($type === 'jsdate') {
return (string) Calendar::getDateFormat('short', $locale); return (string) Calendar::getDateFormat('short', $this->locale);
} }
$value = new \DateTime(); $value = new \DateTime();
@ -171,13 +188,13 @@ class L10N implements IL10N {
$width = $options['width']; $width = $options['width'];
switch ($type) { switch ($type) {
case 'date': case 'date':
return (string) Calendar::formatDate($value, $width, $locale); return (string) Calendar::formatDate($value, $width, $this->locale);
case 'datetime': case 'datetime':
return (string) Calendar::formatDatetime($value, $width, $locale); return (string) Calendar::formatDatetime($value, $width, $this->locale);
case 'time': case 'time':
return (string) Calendar::formatTime($value, $width, $locale); return (string) Calendar::formatTime($value, $width, $this->locale);
case 'weekdayName': case 'weekdayName':
return (string) Calendar::getWeekdayName($value, $width, $locale); return (string) Calendar::getWeekdayName($value, $width, $this->locale);
default: default:
return false; return false;
} }

View File

@ -8,6 +8,7 @@ declare(strict_types=1);
* @author Joas Schilling <coding@schilljs.com> * @author Joas Schilling <coding@schilljs.com>
* @author Morris Jobke <hey@morrisjobke.de> * @author Morris Jobke <hey@morrisjobke.de>
* @author Roeland Jago Douma <roeland@famdouma.nl> * @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Citharel <tcit@tcit.fr>
* *
* @license AGPL-3.0 * @license AGPL-3.0
* *
@ -107,4 +108,12 @@ interface IL10N {
* @since 7.0.0 * @since 7.0.0
*/ */
public function getLanguageCode(): string ; public function getLanguageCode(): string ;
/**
* * The code (en_US, fr_CA, ...) of the locale that is used for this IL10N object
*
* @return string locale
* @since 13.0.0
*/
public function getLocaleCode();
} }