diff options
Diffstat (limited to 'lib/private/L10N/Factory.php')
-rw-r--r-- | lib/private/L10N/Factory.php | 179 |
1 files changed, 88 insertions, 91 deletions
diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php index 6de620e7ec7..6a747744829 100644 --- a/lib/private/L10N/Factory.php +++ b/lib/private/L10N/Factory.php @@ -1,45 +1,15 @@ <?php declare(strict_types=1); - /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> - * @copyright 2016 Lukas Reschke <lukas@statuscode.ch> - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Bart Visscher <bartv@thisnet.nl> - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Georg Ehrke <oc.list@georgehrke.com> - * @author GretaD <gretadoci@gmail.com> - * @author Joas Schilling <coding@schilljs.com> - * @author John Molakvoæ <skjnldsv@protonmail.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @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> - * @author Thomas Citharel <nextcloud@tcit.fr> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OC\L10N; +use OCP\App\AppPathNotFoundException; +use OCP\App\IAppManager; use OCP\ICache; use OCP\ICacheFactory; use OCP\IConfig; @@ -88,38 +58,29 @@ class Factory implements IFactory { 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' ]; - /** @var IConfig */ - protected $config; - - /** @var IRequest */ - protected $request; - - /** @var IUserSession */ - protected IUserSession $userSession; + /** + * Keep in sync with `build/translation-checker.php` + */ + public const RTL_LANGUAGES = [ + 'ar', // Arabic + 'fa', // Persian + 'he', // Hebrew + 'ps', // Pashto, + 'ug', // 'Uyghurche / Uyghur + 'ur_PK', // Urdu + ]; private ICache $cache; - /** @var string */ - protected $serverRoot; - - /** - * @param IConfig $config - * @param IRequest $request - * @param IUserSession $userSession - * @param string $serverRoot - */ public function __construct( - IConfig $config, - IRequest $request, - IUserSession $userSession, + protected IConfig $config, + protected IRequest $request, + protected IUserSession $userSession, ICacheFactory $cacheFactory, - $serverRoot + protected string $serverRoot, + protected IAppManager $appManager, ) { - $this->config = $config; - $this->request = $request; - $this->userSession = $userSession; $this->cache = $cacheFactory->createLocal('L10NFactory'); - $this->serverRoot = $serverRoot; } /** @@ -132,12 +93,12 @@ class Factory implements IFactory { */ public function get($app, $lang = null, $locale = null) { return new LazyL10N(function () use ($app, $lang, $locale) { - $app = \OC_App::cleanAppId($app); + $app = $this->appManager->cleanAppId($app); if ($lang !== null) { $lang = str_replace(['\0', '/', '\\', '..'], '', $lang); } - $forceLang = $this->config->getSystemValue('force_language', false); + $forceLang = $this->request->getParam('forceLanguage') ?? $this->config->getSystemValue('force_language', false); if (is_string($forceLang)) { $lang = $forceLang; } @@ -147,9 +108,7 @@ class Factory implements IFactory { $locale = $forceLocale; } - if ($lang === null || !$this->languageExists($app, $lang)) { - $lang = $this->findLanguage($app); - } + $lang = $this->validateLanguage($app, $lang); if ($locale === null || !$this->localeExists($locale)) { $locale = $this->findLocale($lang); @@ -170,6 +129,29 @@ class Factory implements IFactory { } /** + * Check that $lang is an existing language and not null, otherwise return the language to use instead + * + * @psalm-taint-escape callable + * @psalm-taint-escape cookie + * @psalm-taint-escape file + * @psalm-taint-escape has_quotes + * @psalm-taint-escape header + * @psalm-taint-escape html + * @psalm-taint-escape include + * @psalm-taint-escape ldap + * @psalm-taint-escape shell + * @psalm-taint-escape sql + * @psalm-taint-escape unserialize + */ + private function validateLanguage(string $app, ?string $lang): string { + if ($lang === null || !$this->languageExists($app, $lang)) { + return $this->findLanguage($app); + } else { + return $lang; + } + } + + /** * Find the best language * * @param string|null $appId App id or null for core @@ -178,7 +160,7 @@ class Factory implements IFactory { */ public function findLanguage(?string $appId = null): string { // Step 1: Forced language always has precedence over anything else - $forceLang = $this->config->getSystemValue('force_language', false); + $forceLang = $this->request->getParam('forceLanguage') ?? $this->config->getSystemValue('force_language', false); if (is_string($forceLang)) { $this->requestLanguage = $forceLang; } @@ -233,9 +215,9 @@ class Factory implements IFactory { return 'en'; } - public function findGenericLanguage(string $appId = null): string { + public function findGenericLanguage(?string $appId = null): string { // Step 1: Forced language always has precedence over anything else - $forcedLanguage = $this->config->getSystemValue('force_language', false); + $forcedLanguage = $this->request->getParam('forceLanguage') ?? $this->config->getSystemValue('force_language', false); if ($forcedLanguage !== false) { return $forcedLanguage; } @@ -283,9 +265,9 @@ class Factory implements IFactory { } if ($this->config->getSystemValueBool('installed', false)) { - $userId = null !== $this->userSession->getUser() ? $this->userSession->getUser()->getUID() : null; + $userId = $this->userSession->getUser() !== null ? $this->userSession->getUser()->getUID() : null; $userLocale = null; - if (null !== $userId) { + if ($userId !== null) { $userLocale = $this->config->getUserValue($userId, 'core', 'locale', null); } } else { @@ -304,7 +286,7 @@ class Factory implements IFactory { } // If no user locale set, use lang as locale - if (null !== $lang && $this->localeExists($lang)) { + if ($lang !== null && $this->localeExists($lang)) { return $lang; } @@ -319,7 +301,7 @@ class Factory implements IFactory { * @param string $locale * @return null|string */ - public function findLanguageFromLocale(string $app = 'core', string $locale = null) { + public function findLanguageFromLocale(string $app = 'core', ?string $locale = null) { if ($this->languageExists($app, $locale)) { return $locale; } @@ -415,7 +397,15 @@ class Factory implements IFactory { return in_array($lang, $languages); } - public function getLanguageIterator(IUser $user = null): ILanguageIterator { + public function getLanguageDirection(string $language): string { + if (in_array($language, self::RTL_LANGUAGES, true)) { + return 'rtl'; + } + + return 'ltr'; + } + + public function getLanguageIterator(?IUser $user = null): ILanguageIterator { $user = $user ?? $this->userSession->getUser(); if ($user === null) { throw new \RuntimeException('Failed to get an IUser instance'); @@ -430,7 +420,7 @@ class Factory implements IFactory { * @return string * @since 20.0.0 */ - public function getUserLanguage(IUser $user = null): string { + public function getUserLanguage(?IUser $user = null): string { $language = $this->config->getSystemValue('force_language', false); if ($language !== false) { return $language; @@ -442,9 +432,13 @@ class Factory implements IFactory { return $language; } + if (($forcedLanguage = $this->request->getParam('forceLanguage')) !== null) { + return $forcedLanguage; + } + // Use language from request - if ($this->userSession->getUser() instanceof IUser && - $user->getUID() === $this->userSession->getUser()->getUID()) { + if ($this->userSession->getUser() instanceof IUser + && $user->getUID() === $this->userSession->getUser()->getUID()) { try { return $this->getLanguageFromRequest(); } catch (LanguageNotFoundException $e) { @@ -452,7 +446,7 @@ class Factory implements IFactory { } } - return $this->config->getSystemValueString('default_language', 'en'); + return $this->request->getParam('forceLanguage') ?? $this->config->getSystemValueString('default_language', 'en'); } /** @@ -495,7 +489,7 @@ class Factory implements IFactory { if ($preferred_language === strtolower($available_language)) { return $this->respectDefaultLanguage($app, $available_language); } - if ($preferred_language_parts[0].'_'.end($preferred_language_parts) === strtolower($available_language)) { + if (strtolower($available_language) === $preferred_language_parts[0] . '_' . end($preferred_language_parts)) { return $available_language; } } @@ -523,10 +517,10 @@ class Factory implements IFactory { // use formal version of german ("Sie" instead of "Du") if the default // language is set to 'de_DE' if possible if ( - is_string($defaultLanguage) && - strtolower($lang) === 'de' && - strtolower($defaultLanguage) === 'de_de' && - $this->languageExists($app, 'de_DE') + is_string($defaultLanguage) + && strtolower($lang) === 'de' + && strtolower($defaultLanguage) === 'de_de' + && $this->languageExists($app, 'de_DE') ) { $result = 'de_DE'; } @@ -558,13 +552,9 @@ class Factory implements IFactory { /** * Get a list of language files that should be loaded * - * @param string $app - * @param string $lang * @return string[] */ - // FIXME This method is only public, until OC_L10N does not need it anymore, - // FIXME This is also the reason, why it is not in the public interface - public function getL10nFilesForApp($app, $lang) { + private function getL10nFilesForApp(string $app, string $lang): array { $languageFiles = []; $i18nDir = $this->findL10nDir($app); @@ -572,7 +562,7 @@ class Factory implements IFactory { if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') - || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/')) + || $this->isSubDirectory($transFile, $this->appManager->getAppPath($app) . '/l10n/')) && file_exists($transFile) ) { // load the translations file @@ -602,9 +592,12 @@ class Factory implements IFactory { if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { return $this->serverRoot . '/' . $app . '/l10n/'; } - } elseif ($app && \OC_App::getAppPath($app) !== false) { - // Check if the app is in the app folder - return \OC_App::getAppPath($app) . '/l10n/'; + } elseif ($app) { + try { + return $this->appManager->getAppPath($app) . '/l10n/'; + } catch (AppPathNotFoundException) { + /* App not found, continue */ + } } return $this->serverRoot . '/core/l10n/'; } @@ -628,6 +621,10 @@ class Factory implements IFactory { } $languageCodes = $this->findAvailableLanguages(); + $reduceToLanguages = $this->config->getSystemValue('reduce_to_languages', []); + if (!empty($reduceToLanguages)) { + $languageCodes = array_intersect($languageCodes, $reduceToLanguages); + } $commonLanguages = []; $otherLanguages = []; |