diff options
-rw-r--r-- | apps/provisioning_api/lib/Controller/UsersController.php | 4 | ||||
-rw-r--r-- | apps/provisioning_api/tests/Controller/UsersControllerTest.php | 13 | ||||
-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 | ||||
-rw-r--r-- | settings/templates/settings/personal/personal.info.php | 2 |
7 files changed, 97 insertions, 17 deletions
diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php index 0430226e12c..32967742bd2 100644 --- a/apps/provisioning_api/lib/Controller/UsersController.php +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -522,7 +522,9 @@ class UsersController extends AUserData { $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); break; case 'locale': - // do some stuff + if (!$this->l10nFactory->localeExists($value)) { + throw new OCSException('Invalid locale', 102); + } $this->config->setUserValue($targetUser->getUID(), 'core', 'locale', $value); break; case AccountManager::PROPERTY_EMAIL: diff --git a/apps/provisioning_api/tests/Controller/UsersControllerTest.php b/apps/provisioning_api/tests/Controller/UsersControllerTest.php index 114742de4f9..0de62bc0d15 100644 --- a/apps/provisioning_api/tests/Controller/UsersControllerTest.php +++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php @@ -748,6 +748,10 @@ class UsersControllerTest extends TestCase { ->method('getDisplayName') ->will($this->returnValue('Demo User')); $targetUser + ->expects($this->exactly(5)) + ->method('getUID') + ->will($this->returnValue('UID')); + $targetUser ->expects($this->once()) ->method('getHome') ->will($this->returnValue('/var/www/newtcloud/data/UID')); @@ -759,10 +763,6 @@ class UsersControllerTest extends TestCase { ->expects($this->once()) ->method('getBackendClassName') ->will($this->returnValue('Database')); - $targetUser - ->expects($this->exactly(5)) - ->method('getUID') - ->will($this->returnValue('UID')); $expected = [ 'id' => 'UID', @@ -780,6 +780,7 @@ class UsersControllerTest extends TestCase { 'twitter' => 'twitter', 'groups' => ['group0', 'group1', 'group2'], 'language' => 'de', + 'locale' => null, ]; $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UID'])); } @@ -865,7 +866,7 @@ class UsersControllerTest extends TestCase { ->method('getBackendClassName') ->will($this->returnValue('Database')); $targetUser - ->expects($this->exactly(5)) + ->expects($this->exactly(6)) ->method('getUID') ->will($this->returnValue('UID')); $this->accountManager->expects($this->any())->method('getUser') @@ -895,6 +896,7 @@ class UsersControllerTest extends TestCase { 'twitter' => 'twitter', 'groups' => [], 'language' => 'da', + 'locale' => null, ]; $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UID'])); } @@ -1050,6 +1052,7 @@ class UsersControllerTest extends TestCase { 'twitter' => 'twitter', 'groups' => [], 'language' => 'ru', + 'locale' => null, ]; $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UID'])); } 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); } diff --git a/settings/templates/settings/personal/personal.info.php b/settings/templates/settings/personal/personal.info.php index 959650cf2f9..20e346c867a 100644 --- a/settings/templates/settings/personal/personal.info.php +++ b/settings/templates/settings/personal/personal.info.php @@ -381,6 +381,7 @@ vendor_style('jcrop/css/jquery.Jcrop'); </div> +<?php if (isset($_['activelocale'])) { ?> <form id="locale" class="section"> <h2> <label for="localeinput"><?php p($l->t('Locale'));?></label> @@ -406,3 +407,4 @@ vendor_style('jcrop/css/jquery.Jcrop'); <?php endforeach;?> </select> </form> +<?php } ?> |