Fix review & tests

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2017-07-07 11:17:54 +02:00 committed by Georg Ehrke
parent 93011579fe
commit 4a9f8623b5
No known key found for this signature in database
GPG Key ID: 9D98FD9380A1CB43
7 changed files with 97 additions and 17 deletions

View File

@ -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:

View File

@ -747,6 +747,10 @@ class UsersControllerTest extends TestCase {
->expects($this->once())
->method('getDisplayName')
->will($this->returnValue('Demo User'));
$targetUser
->expects($this->exactly(5))
->method('getUID')
->will($this->returnValue('UID'));
$targetUser
->expects($this->once())
->method('getHome')
@ -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']));
}

View File

@ -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
@ -251,6 +293,24 @@ class Factory implements IFactory {
return array_search($lang, $languages) !== false;
}
/**
* @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

View File

@ -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,

View File

@ -53,6 +53,12 @@ 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
@ -60,4 +66,20 @@ interface IFactory {
* @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);
}

View File

@ -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 } ?>