aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/L10N/Factory.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/L10N/Factory.php')
-rw-r--r--lib/private/L10N/Factory.php209
1 files changed, 110 insertions, 99 deletions
diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php
index 7fcfab37fa6..6a747744829 100644
--- a/lib/private/L10N/Factory.php
+++ b/lib/private/L10N/Factory.php
@@ -1,45 +1,17 @@
<?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;
use OCP\IRequest;
use OCP\IUser;
@@ -52,7 +24,6 @@ use function is_null;
* A factory that generates language instances
*/
class Factory implements IFactory {
-
/** @var string */
protected $requestLanguage = '';
@@ -87,34 +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 $userSession;
-
- /** @var string */
- protected $serverRoot;
-
/**
- * @param IConfig $config
- * @param IRequest $request
- * @param IUserSession $userSession
- * @param string $serverRoot
+ * 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;
+
public function __construct(
- IConfig $config,
- IRequest $request,
- IUserSession $userSession,
- $serverRoot
+ protected IConfig $config,
+ protected IRequest $request,
+ protected IUserSession $userSession,
+ ICacheFactory $cacheFactory,
+ protected string $serverRoot,
+ protected IAppManager $appManager,
) {
- $this->config = $config;
- $this->request = $request;
- $this->userSession = $userSession;
- $this->serverRoot = $serverRoot;
+ $this->cache = $cacheFactory->createLocal('L10NFactory');
}
/**
@@ -127,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;
}
@@ -142,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);
@@ -165,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
@@ -173,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;
}
@@ -190,7 +177,7 @@ class Factory implements IFactory {
*
* @link https://github.com/owncloud/core/issues/21955
*/
- if ($this->config->getSystemValue('installed', false)) {
+ if ($this->config->getSystemValueBool('installed', false)) {
$userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null;
if (!is_null($userId)) {
$userLang = $this->config->getUserValue($userId, 'core', 'lang', null);
@@ -228,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;
}
@@ -242,7 +229,7 @@ class Factory implements IFactory {
}
// Step 3.1: Check if Nextcloud is already installed before we try to access user info
- if (!$this->config->getSystemValue('installed', false)) {
+ if (!$this->config->getSystemValueBool('installed', false)) {
return 'en';
}
// Step 3.2: Check the current user (if any) for their preferred language
@@ -277,10 +264,10 @@ class Factory implements IFactory {
return $forceLocale;
}
- if ($this->config->getSystemValue('installed', false)) {
- $userId = null !== $this->userSession->getUser() ? $this->userSession->getUser()->getUID() : null;
+ if ($this->config->getSystemValueBool('installed', false)) {
+ $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 {
@@ -299,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;
}
@@ -314,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;
}
@@ -338,6 +325,10 @@ class Factory implements IFactory {
$key = 'null';
}
+ if ($availableLanguages = $this->cache->get($key)) {
+ $this->availableLanguages[$key] = $availableLanguages;
+ }
+
// also works with null as key
if (!empty($this->availableLanguages[$key])) {
return $this->availableLanguages[$key];
@@ -349,7 +340,7 @@ class Factory implements IFactory {
$files = scandir($dir);
if ($files !== false) {
foreach ($files as $file) {
- if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') {
+ if (str_ends_with($file, '.json') && !str_starts_with($file, 'l10n')) {
$available[] = substr($file, 0, -5);
}
}
@@ -357,7 +348,7 @@ class Factory implements IFactory {
}
// merge with translations from theme
- $theme = $this->config->getSystemValue('theme');
+ $theme = $this->config->getSystemValueString('theme');
if (!empty($theme)) {
$themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot));
@@ -365,7 +356,7 @@ class Factory implements IFactory {
$files = scandir($themeDir);
if ($files !== false) {
foreach ($files as $file) {
- if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') {
+ if (str_ends_with($file, '.json') && !str_starts_with($file, 'l10n')) {
$available[] = substr($file, 0, -5);
}
}
@@ -374,6 +365,7 @@ class Factory implements IFactory {
}
$this->availableLanguages[$key] = $available;
+ $this->cache->set($key, $available, 60);
return $available;
}
@@ -405,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');
@@ -420,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;
@@ -432,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) {
@@ -442,7 +446,7 @@ class Factory implements IFactory {
}
}
- return $this->config->getSystemValue('default_language', 'en');
+ return $this->request->getParam('forceLanguage') ?? $this->config->getSystemValueString('default_language', 'en');
}
/**
@@ -480,10 +484,14 @@ class Factory implements IFactory {
[$preferred_language] = explode(';', $preference);
$preferred_language = str_replace('-', '_', $preferred_language);
+ $preferred_language_parts = explode('_', $preferred_language);
foreach ($available as $available_language) {
if ($preferred_language === strtolower($available_language)) {
return $this->respectDefaultLanguage($app, $available_language);
}
+ if (strtolower($available_language) === $preferred_language_parts[0] . '_' . end($preferred_language_parts)) {
+ return $available_language;
+ }
}
// Fallback from de_De to de
@@ -509,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';
}
@@ -529,12 +537,12 @@ class Factory implements IFactory {
*/
private function isSubDirectory($sub, $parent) {
// Check whether $sub contains no ".."
- if (strpos($sub, '..') !== false) {
+ if (str_contains($sub, '..')) {
return false;
}
// Check whether $sub is a subdirectory of $parent
- if (strpos($sub, $parent) === 0) {
+ if (str_starts_with($sub, $parent)) {
return true;
}
@@ -544,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);
@@ -558,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
@@ -566,7 +570,7 @@ class Factory implements IFactory {
}
// merge with translations from theme
- $theme = $this->config->getSystemValue('theme');
+ $theme = $this->config->getSystemValueString('theme');
if (!empty($theme)) {
$transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot));
if (file_exists($transFile)) {
@@ -588,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/';
}
@@ -614,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 = [];