diff options
author | Thomas Citharel <tcit@tcit.fr> | 2017-07-07 11:17:54 +0200 |
---|---|---|
committer | Georg Ehrke <developer@georgehrke.com> | 2018-06-27 13:19:21 +0200 |
commit | 4a9f8623b50ab0ba6a7fd411c0750a6bd5f63c14 (patch) | |
tree | ab2a2fc068236db4a415391275ddf6dab75ea485 /lib | |
parent | 93011579fe27624ed746b54f1094da556d019f5a (diff) | |
download | nextcloud-server-4a9f8623b50ab0ba6a7fd411c0750a6bd5f63c14.tar.gz nextcloud-server-4a9f8623b50ab0ba6a7fd411c0750a6bd5f63c14.zip |
Fix review & tests
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/L10N/Factory.php | 60 | ||||
-rw-r--r-- | lib/private/L10N/locales.json (renamed from lib/private/Settings/Personal/locales.json) | 0 | ||||
-rw-r--r-- | lib/private/Settings/Personal/PersonalInfo.php | 13 | ||||
-rw-r--r-- | lib/public/L10N/IFactory.php | 22 |
4 files changed, 84 insertions, 11 deletions
diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php index c84bc53f9e4..a6b91d53e4f 100644 --- a/lib/private/L10N/Factory.php +++ b/lib/private/L10N/Factory.php @@ -115,6 +115,10 @@ class Factory implements IFactory { $lang = $this->findLanguage($app); } + if ($locale === null || !$this->localeExists($locale)) { + $locale = $this->findLocale($app, $lang); + } + if (!isset($this->instances[$lang][$app])) { $this->instances[$lang][$app] = new L10N( $this, $app, $lang, $locale, @@ -186,6 +190,38 @@ class Factory implements IFactory { return 'en'; } + public function findLocale($app = null, $lang = null) + { + if ($this->config->getSystemValue('installed', false)) { + $userId = null !== $this->userSession->getUser() ? $this->userSession->getUser()->getUID() : null; + $userLocale = null; + if (null !== $userId) { + $userLocale = $this->config->getUserValue($userId, 'core', 'locale', null); + } + } else { + $userId = null; + $userLocale = null; + } + + if ($userLocale && $this->localeExists($userLocale)) { + return $userLocale; + } + + // If no user locale set, use lang as locale + if (null !== $lang && $this->localeExists($lang)) { + return $lang; + } + + // Default : use system default locale + $defaultLocale = $this->config->getSystemValue('default_locale', false); + if ($defaultLocale !== false && $this->localeExists($defaultLocale)) { + return $defaultLocale; + } + + // At last, return USA + return 'en_US'; + } + /** * Find all available languages for an app * @@ -237,6 +273,12 @@ class Factory implements IFactory { return $available; } + public function findAvailableLocales() + { + $localeData = file_get_contents(__DIR__ . '/locales.json'); + return json_decode($localeData, true); + } + /** * @param string|null $app App id or null for core * @param string $lang @@ -252,6 +294,24 @@ class Factory implements IFactory { } /** + * @param string $locale + * @return bool + */ + public function localeExists($locale) { + if ($locale === 'en') { //english is always available + return true; + } + + $locales = $this->findAvailableLocales(); + + $userLocale = array_filter($locales, function($value) use ($locale) { + return $locale === $value['code']; + }); + + return !empty($userLocale); + } + + /** * @param string|null $app * @return string * @throws LanguageNotFoundException diff --git a/lib/private/Settings/Personal/locales.json b/lib/private/L10N/locales.json index 1098973c5ef..1098973c5ef 100644 --- a/lib/private/Settings/Personal/locales.json +++ b/lib/private/L10N/locales.json diff --git a/lib/private/Settings/Personal/PersonalInfo.php b/lib/private/Settings/Personal/PersonalInfo.php index f6fb9d48d99..267be2878d0 100644 --- a/lib/private/Settings/Personal/PersonalInfo.php +++ b/lib/private/Settings/Personal/PersonalInfo.php @@ -232,14 +232,13 @@ class PersonalInfo implements ISettings { $userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage()); - $localeData = file_get_contents(__DIR__ . '/locales.json'); - $localeCodes = json_decode($localeData, true); + $localeCodes = $this->l10nFactory->findAvailableLocales(); $userLocale = array_filter($localeCodes, function($value) use ($userLocaleString) { return $userLocaleString === $value['code']; }); - if (count($userLocale) > 0) + if (!empty($userLocale)) { $userLocale = reset($userLocale); } @@ -248,14 +247,6 @@ class PersonalInfo implements ISettings { return 0 === strpos($localeCode['code'], $userLang); }); - /*$localesForLanguage = []; - - foreach (array_keys($localeCodes) as $localeCode) { - if (0 === strpos($localeCode, $userLang)) { - $localesForLanguage[] = $localeCode; - } - }*/ - return [ 'activelocaleLang' => $userLocaleString, 'activelocale' => $userLocale, diff --git a/lib/public/L10N/IFactory.php b/lib/public/L10N/IFactory.php index 9820082c72e..08e4f834168 100644 --- a/lib/public/L10N/IFactory.php +++ b/lib/public/L10N/IFactory.php @@ -54,10 +54,32 @@ interface IFactory { public function findAvailableLanguages($app = null); /** + * @return array an array of available + * @since 13.0.0 + */ + public function findAvailableLocales(); + + /** * @param string|null $app App id or null for core * @param string $lang * @return bool * @since 9.0.0 */ public function languageExists($app, $lang); + + /** + * @param string $locale + * @return bool + * @since 13.0.0 + */ + public function localeExists($locale); + + /** + * Creates a function from the plural string + * + * @param string $string + * @return string Unique function name + * @since 9.0.0 + */ + public function createPluralFunction($string); } |