diff options
author | Joas Schilling <coding@schilljs.com> | 2016-09-05 12:37:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-05 12:37:44 +0200 |
commit | db6a3367adeb4a0f4ca8873bd6ecbf1ea498705d (patch) | |
tree | 06a6f58321d8bdf0909680a53db094983f2dc164 /lib | |
parent | f8eb7be7b1b0e1052e56b35e7bb450ad5ac68547 (diff) | |
parent | 933c892a62c120c93502f283ce7d7842e2a30692 (diff) | |
download | nextcloud-server-db6a3367adeb4a0f4ca8873bd6ecbf1ea498705d.tar.gz nextcloud-server-db6a3367adeb4a0f4ca8873bd6ecbf1ea498705d.zip |
Merge pull request #1259 from nextcloud/language_order
ACCEPT_LANGUAGE goes before default_langauge
Diffstat (limited to 'lib')
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | lib/private/L10N/Factory.php | 57 | ||||
-rw-r--r-- | lib/private/L10N/LanguageNotFoundException.php | 26 |
4 files changed, 65 insertions, 20 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 1363d96349e..2700c87ec00 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -550,6 +550,7 @@ return array( 'OC\\IntegrityCheck\\Iterator\\ExcludeFoldersByPathFilterIterator' => $baseDir . '/lib/private/IntegrityCheck/Iterator/ExcludeFoldersByPathFilterIterator.php', 'OC\\L10N\\Factory' => $baseDir . '/lib/private/L10N/Factory.php', 'OC\\L10N\\L10N' => $baseDir . '/lib/private/L10N/L10N.php', + 'OC\\L10N\\LanguageNotFoundException' => $baseDir . '/lib/private/L10N/LanguageNotFoundException.php', 'OC\\LargeFileHelper' => $baseDir . '/lib/private/LargeFileHelper.php', 'OC\\Lock\\AbstractLockingProvider' => $baseDir . '/lib/private/Lock/AbstractLockingProvider.php', 'OC\\Lock\\DBLockingProvider' => $baseDir . '/lib/private/Lock/DBLockingProvider.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 42b6fc83217..3797711f914 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -580,6 +580,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\IntegrityCheck\\Iterator\\ExcludeFoldersByPathFilterIterator' => __DIR__ . '/../../..' . '/lib/private/IntegrityCheck/Iterator/ExcludeFoldersByPathFilterIterator.php', 'OC\\L10N\\Factory' => __DIR__ . '/../../..' . '/lib/private/L10N/Factory.php', 'OC\\L10N\\L10N' => __DIR__ . '/../../..' . '/lib/private/L10N/L10N.php', + 'OC\\L10N\\LanguageNotFoundException' => __DIR__ . '/../../..' . '/lib/private/L10N/LanguageNotFoundException.php', 'OC\\LargeFileHelper' => __DIR__ . '/../../..' . '/lib/private/LargeFileHelper.php', 'OC\\Lock\\AbstractLockingProvider' => __DIR__ . '/../../..' . '/lib/private/Lock/AbstractLockingProvider.php', 'OC\\Lock\\DBLockingProvider' => __DIR__ . '/../../..' . '/lib/private/Lock/DBLockingProvider.php', diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php index 74f759a4e0d..91233a0c4a7 100644 --- a/lib/private/L10N/Factory.php +++ b/lib/private/L10N/Factory.php @@ -1,6 +1,7 @@ <?php /** * @copyright Copyright (c) 2016, ownCloud, Inc. + * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> * * @author Bart Visscher <bartv@thisnet.nl> * @author Joas Schilling <coding@schilljs.com> @@ -8,6 +9,7 @@ * @author Morris Jobke <hey@morrisjobke.de> * @author Robin Appelman <robin@icewind.nl> * @author Robin McCorkell <robin@mccorkell.me.uk> + * @author Roeland Jago Douma <roeland@famdouma.nl> * * @license AGPL-3.0 * @@ -147,18 +149,23 @@ class Factory implements IFactory { } } - $defaultLanguage = $this->config->getSystemValue('default_language', false); - - if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { - return $defaultLanguage; - } - - $lang = $this->setLanguageFromRequest($app); - if ($userId !== null && $app === null && !$userLang) { - $this->config->setUserValue($userId, 'core', 'lang', $lang); + try { + // Try to get the language from the Request + $lang = $this->getLanguageFromRequest($app); + if ($userId !== null && $app === null && !$userLang) { + $this->config->setUserValue($userId, 'core', 'lang', $lang); + } + return $lang; + } catch (LanguageNotFoundException $e) { + // Finding language from request failed fall back to default language + $defaultLanguage = $this->config->getSystemValue('default_language', false); + if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { + return $defaultLanguage; + } } - return $lang; + // We could not find any language so fall back to english + return 'en'; } /** @@ -227,10 +234,11 @@ class Factory implements IFactory { } /** - * @param string|null $app App id or null for core + * @param string|null $app * @return string + * @throws LanguageNotFoundException */ - public function setLanguageFromRequest($app = null) { + private function getLanguageFromRequest($app) { $header = $this->request->getHeader('ACCEPT_LANGUAGE'); if ($header) { $available = $this->findAvailableLanguages($app); @@ -245,9 +253,6 @@ class Factory implements IFactory { foreach ($available as $available_language) { if ($preferred_language === strtolower($available_language)) { - if ($app === null && !$this->requestLanguage) { - $this->requestLanguage = $available_language; - } return $available_language; } } @@ -255,19 +260,31 @@ class Factory implements IFactory { // 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; } } } } + throw new LanguageNotFoundException(); + } + + /** + * @param string|null $app App id or null for core + * @return string + */ + public function setLanguageFromRequest($app = null) { + + try { + $requestLanguage = $this->getLanguageFromRequest($app); + } catch (LanguageNotFoundException $e) { + $requestLanguage = 'en'; + } + if ($app === null && !$this->requestLanguage) { - $this->requestLanguage = 'en'; + $this->requestLanguage = $requestLanguage; } - return 'en'; // Last try: English + return $requestLanguage; } /** diff --git a/lib/private/L10N/LanguageNotFoundException.php b/lib/private/L10N/LanguageNotFoundException.php new file mode 100644 index 00000000000..175f6ab3483 --- /dev/null +++ b/lib/private/L10N/LanguageNotFoundException.php @@ -0,0 +1,26 @@ +<?php +/** + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OC\L10N; + +class LanguageNotFoundException extends \Exception { + +} |