diff options
author | Thomas Citharel <tcit@tcit.fr> | 2017-07-05 17:37:12 +0200 |
---|---|---|
committer | Georg Ehrke <developer@georgehrke.com> | 2018-06-27 13:17:26 +0200 |
commit | ea380b2918b298259f7f45a3e4671f50d89e6c13 (patch) | |
tree | 68273792b8a1047f32ea1f0f69f8d56cd5264856 /lib/private/L10N/L10N.php | |
parent | d8921ccd85f3414c1f3e13b23b6da735bf9b69b8 (diff) | |
download | nextcloud-server-ea380b2918b298259f7f45a3e4671f50d89e6c13.tar.gz nextcloud-server-ea380b2918b298259f7f45a3e4671f50d89e6c13.zip |
Allow apps to specify locale for localisation
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'lib/private/L10N/L10N.php')
-rw-r--r-- | lib/private/L10N/L10N.php | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/lib/private/L10N/L10N.php b/lib/private/L10N/L10N.php index a9b1b7377aa..7087fcae049 100644 --- a/lib/private/L10N/L10N.php +++ b/lib/private/L10N/L10N.php @@ -5,6 +5,7 @@ declare(strict_types=1); * * @author Georg Ehrke <oc.list@georgehrke.com> * @author Joas Schilling <coding@schilljs.com> + * @author Thomas Citharel <tcit@tcit.fr> * @author Roeland Jago Douma <roeland@famdouma.nl> * * @license AGPL-3.0 @@ -41,6 +42,9 @@ class L10N implements IL10N { /** @var string Language of this object */ protected $lang; + /** @var string Locale of this object */ + protected $locale; + /** @var string Plural forms (string) */ private $pluralFormString = 'nplurals=2; plural=(n != 1);'; @@ -54,12 +58,14 @@ class L10N implements IL10N { * @param IFactory $factory * @param string $app * @param string $lang + * @param string $locale * @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->app = $app; $this->lang = $lang; + $this->locale = $locale; foreach ($files as $languageFile) { $this->load($languageFile); @@ -76,6 +82,15 @@ class L10N implements IL10N { } /** + * 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 * @param string $text The text we need a translation for * @param array|string $parameters default:array() Parameters for sprintf @@ -143,17 +158,19 @@ class L10N implements IL10N { * - jsdate: Returns the short JS date format */ public function l(string $type, $data = null, array $options = []) { - // Use the language of the instance - $locale = $this->getLanguageCode(); - if ($locale === 'sr@latin') { - $locale = 'sr_latn'; + if (null === $this->locale) { + // Use the language of the instance + $this->locale = $this->getLanguageCode(); + } + if ($this->locale === 'sr@latin') { + $this->locale = 'sr_latn'; } if ($type === 'firstday') { - return (int) Calendar::getFirstWeekday($locale); + return (int) Calendar::getFirstWeekday($this->locale); } if ($type === 'jsdate') { - return (string) Calendar::getDateFormat('short', $locale); + return (string) Calendar::getDateFormat('short', $this->locale); } $value = new \DateTime(); @@ -171,13 +188,13 @@ class L10N implements IL10N { $width = $options['width']; switch ($type) { case 'date': - return (string) Calendar::formatDate($value, $width, $locale); + return (string) Calendar::formatDate($value, $width, $this->locale); case 'datetime': - return (string) Calendar::formatDatetime($value, $width, $locale); + return (string) Calendar::formatDatetime($value, $width, $this->locale); case 'time': - return (string) Calendar::formatTime($value, $width, $locale); + return (string) Calendar::formatTime($value, $width, $this->locale); case 'weekdayName': - return (string) Calendar::getWeekdayName($value, $width, $locale); + return (string) Calendar::getWeekdayName($value, $width, $this->locale); default: return false; } |