-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add user locale/region setting #5623
Changes from 17 commits
d8921cc
ea380b2
9301157
4a9f862
abab7eb
7d8124a
bf6bfad
9af8229
fd41192
0cacdd3
3ffeb4a
51a35c2
b3921c6
9157086
b17d256
8c73b13
325403a
4c1e581
c1682f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,11 @@ class Factory implements IFactory { | |
*/ | ||
protected $availableLanguages = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $availableLocales = []; | ||
|
||
/** | ||
* @var array Structure: string => callable | ||
*/ | ||
|
@@ -97,9 +102,10 @@ public function __construct(IConfig $config, | |
* | ||
* @param string $app | ||
* @param string|null $lang | ||
* @param string|null $locale | ||
* @return \OCP\IL10N | ||
*/ | ||
public function get($app, $lang = null) { | ||
public function get($app, $lang = null, $locale = null) { | ||
$app = \OC_App::cleanAppId($app); | ||
if ($lang !== null) { | ||
$lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang); | ||
|
@@ -110,13 +116,22 @@ public function get($app, $lang = null) { | |
$lang = $forceLang; | ||
} | ||
|
||
$forceLocale = $this->config->getSystemValue('force_locale', false); | ||
if (is_string($forceLocale)) { | ||
$locale = $forceLocale; | ||
} | ||
|
||
if ($lang === null || !$this->languageExists($app, $lang)) { | ||
$lang = $this->findLanguage($app); | ||
} | ||
|
||
if ($locale === null || !$this->localeExists($locale)) { | ||
$locale = $this->findLocale($lang); | ||
} | ||
|
||
if (!isset($this->instances[$lang][$app])) { | ||
$this->instances[$lang][$app] = new L10N( | ||
$this, $app, $lang, | ||
$this, $app, $lang, $locale, | ||
$this->getL10nFilesForApp($app, $lang) | ||
); | ||
} | ||
|
@@ -185,6 +200,48 @@ public function findLanguage($app = null) { | |
return 'en'; | ||
} | ||
|
||
/** | ||
* find the best locale | ||
* | ||
* @param string $lang | ||
* @return null|string | ||
*/ | ||
public function findLocale($lang = null) { | ||
$forceLocale = $this->config->getSystemValue('force_locale', false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also document it in the sample config. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's already the case :) |
||
if (is_string($forceLocale) && $this->localeExists($forceLocale)) { | ||
return $forceLocale; | ||
} | ||
|
||
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; | ||
} | ||
|
||
// Default : use system default locale | ||
$defaultLocale = $this->config->getSystemValue('default_locale', false); | ||
if ($defaultLocale !== false && $this->localeExists($defaultLocale)) { | ||
return $defaultLocale; | ||
} | ||
|
||
// If no user locale set, use lang as locale | ||
if (null !== $lang && $this->localeExists($lang)) { | ||
return $lang; | ||
} | ||
|
||
// At last, return USA | ||
return 'en_US'; | ||
} | ||
|
||
/** | ||
* Find all available languages for an app | ||
* | ||
|
@@ -236,6 +293,20 @@ public function findAvailableLanguages($app = null) { | |
return $available; | ||
} | ||
|
||
/** | ||
* @return array|mixed | ||
*/ | ||
public function findAvailableLocales() { | ||
if (!empty($this->availableLocales)) { | ||
return $this->availableLocales; | ||
} | ||
|
||
$localeData = file_get_contents(\OC::$SERVERROOT . '/resources/locales.json'); | ||
$this->availableLocales = \json_decode($localeData, true); | ||
|
||
return $this->availableLocales; | ||
} | ||
|
||
/** | ||
* @param string|null $app App id or null for core | ||
* @param string $lang | ||
|
@@ -250,6 +321,23 @@ public function languageExists($app, $lang) { | |
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
locale is not filled with the users config?!