diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2016-01-15 11:09:37 +0100 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2016-01-26 14:02:31 +0100 |
commit | 043625ee522357e25bb786c4b439c90ab21bbfd1 (patch) | |
tree | 633c64268cb5a302436ba61d20eeb7eaf7a5e516 /lib/private/l10n | |
parent | 29a93064299f57584c6aadc8a6c56b51715d7269 (diff) | |
download | nextcloud-server-043625ee522357e25bb786c4b439c90ab21bbfd1.tar.gz nextcloud-server-043625ee522357e25bb786c4b439c90ab21bbfd1.zip |
Move findLanguage() and setLanguageFromRequest() to factory
Diffstat (limited to 'lib/private/l10n')
-rw-r--r-- | lib/private/l10n/factory.php | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/lib/private/l10n/factory.php b/lib/private/l10n/factory.php index b1304727606..ecc2318893a 100644 --- a/lib/private/l10n/factory.php +++ b/lib/private/l10n/factory.php @@ -25,12 +25,18 @@ namespace OC\L10N; +use OCP\IConfig; +use OCP\IRequest; use OCP\L10N\IFactory; /** * A factory that generates language instances */ class Factory implements IFactory { + + /** @var string */ + protected $requestLanguage = ''; + /** * cached instances * @var array Structure: Lang => App => \OCP\IL10N @@ -42,6 +48,21 @@ class Factory implements IFactory { */ protected $availableLanguages = []; + /** @var IConfig */ + protected $config; + + /** @var IRequest */ + protected $request; + + /** + * @param IConfig $config + * @param IRequest $request + */ + public function __construct(IConfig $config, IRequest $request) { + $this->config = $config; + $this->request = $request; + } + /** * Get a language instance * @@ -63,6 +84,41 @@ class Factory implements IFactory { } /** + * Find the best language + * + * @param string|null $app App id or null for core + * @return string language If nothing works it returns 'en' + */ + public function findLanguage($app = null) { + if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { + return $this->requestLanguage; + } + + $userId = \OC_User::getUser(); // FIXME not available in non-static? + + if ($userId && $this->config->getUserValue($userId, 'core', 'lang')) { + $lang = $this->config->getUserValue($userId, 'core', 'lang'); + $this->requestLanguage = $lang; + if ($this->languageExists($app, $lang)) { + return $lang; + } + } + + $defaultLanguage = $this->config->getSystemValue('default_language', false); + + if ($defaultLanguage !== false) { + return $defaultLanguage; + } + + $lang = $this->setLanguageFromRequest($app); + if ($userId && $app === null && !$this->config->getUserValue($userId, 'core', 'lang')) { + $this->config->setUserValue($userId, 'core', 'lang', $lang); + } + + return $lang; + } + + /** * Find all available languages for an app * * @param string|null $app App id or null for core @@ -111,6 +167,50 @@ class Factory implements IFactory { } /** + * @param string|null $app App id or null for core + * @return string + */ + public function setLanguageFromRequest($app = null) { + $header = $this->request->getHeader('ACCEPT_LANGUAGE'); + if ($header) { + $available = $this->findAvailableLanguages($app); + + // E.g. make sure that 'de' is before 'de_DE'. + sort($available); + + $preferences = preg_split('/,\s*/', strtolower($header)); + foreach ($preferences as $preference) { + list($preferred_language) = explode(';', $preference); + $preferred_language = str_replace('-', '_', $preferred_language); + + foreach ($available as $available_language) { + if ($preferred_language === strtolower($available_language)) { + if ($app === null && !$this->requestLanguage) { + $this->requestLanguage = $available_language; + } + return $available_language; + } + } + + // Fallback from de_De to de + foreach ($available as $available_language) { + if (substr($preferred_language, 0, 2) === $available_language) { + if ($app === null && !$this->requestLanguage) { + $this->requestLanguage = $available_language; + } + return $available_language; + } + } + } + } + + if (!$this->requestLanguage) { + $this->requestLanguage = 'en'; + } + return 'en'; // Last try: English + } + + /** * find the l10n directory * * @param string $app App id or empty string for core |