aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/L10N
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/L10N')
-rw-r--r--lib/private/L10N/Factory.php117
-rw-r--r--lib/private/L10N/L10N.php46
-rw-r--r--lib/private/L10N/L10NString.php28
-rw-r--r--lib/private/L10N/LanguageIterator.php22
-rw-r--r--lib/private/L10N/LanguageNotFoundException.php22
-rw-r--r--lib/private/L10N/LazyL10N.php21
6 files changed, 91 insertions, 165 deletions
diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php
index 785915fc82b..6a747744829 100644
--- a/lib/private/L10N/Factory.php
+++ b/lib/private/L10N/Factory.php
@@ -1,43 +1,11 @@
<?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;
@@ -90,6 +58,18 @@ class Factory implements IFactory {
'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko'
];
+ /**
+ * 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(
@@ -113,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;
}
@@ -128,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);
@@ -151,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
@@ -159,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;
}
@@ -216,7 +217,7 @@ class Factory implements IFactory {
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;
}
@@ -396,6 +397,14 @@ class Factory implements IFactory {
return in_array($lang, $languages);
}
+ 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) {
@@ -423,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) {
@@ -433,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');
}
/**
@@ -476,7 +489,7 @@ class Factory implements IFactory {
if ($preferred_language === strtolower($available_language)) {
return $this->respectDefaultLanguage($app, $available_language);
}
- if (strtolower($available_language) === $preferred_language_parts[0].'_'.end($preferred_language_parts)) {
+ if (strtolower($available_language) === $preferred_language_parts[0] . '_' . end($preferred_language_parts)) {
return $available_language;
}
}
@@ -504,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';
}
@@ -608,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 = [];
diff --git a/lib/private/L10N/L10N.php b/lib/private/L10N/L10N.php
index b701d059835..50db373a65d 100644
--- a/lib/private/L10N/L10N.php
+++ b/lib/private/L10N/L10N.php
@@ -1,30 +1,10 @@
<?php
declare(strict_types=1);
-
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Georg Ehrke <oc.list@georgehrke.com>
- * @author Joas Schilling <coding@schilljs.com>
- * @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-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\L10N;
@@ -103,7 +83,7 @@ class L10N implements IL10N {
$parameters = [$parameters];
}
- return (string) new L10NString($this, $text, $parameters);
+ return (string)new L10NString($this, $text, $parameters);
}
/**
@@ -124,14 +104,14 @@ class L10N implements IL10N {
public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string {
$identifier = "_{$text_singular}_::_{$text_plural}_";
if (isset($this->translations[$identifier])) {
- return (string) new L10NString($this, $identifier, $parameters, $count);
+ return (string)new L10NString($this, $identifier, $parameters, $count);
}
if ($count === 1) {
- return (string) new L10NString($this, $text_singular, $parameters, $count);
+ return (string)new L10NString($this, $text_singular, $parameters, $count);
}
- return (string) new L10NString($this, $text_plural, $parameters, $count);
+ return (string)new L10NString($this, $text_plural, $parameters, $count);
}
/**
@@ -166,10 +146,10 @@ class L10N implements IL10N {
}
if ($type === 'firstday') {
- return (int) Calendar::getFirstWeekday($this->locale);
+ return (int)Calendar::getFirstWeekday($this->locale);
}
if ($type === 'jsdate') {
- return (string) Calendar::getDateFormat('short', $this->locale);
+ return (string)Calendar::getDateFormat('short', $this->locale);
}
$value = new \DateTime();
@@ -187,13 +167,13 @@ class L10N implements IL10N {
$width = $options['width'];
switch ($type) {
case 'date':
- return (string) Calendar::formatDate($value, $width, $this->locale);
+ return (string)Calendar::formatDate($value, $width, $this->locale);
case 'datetime':
- return (string) Calendar::formatDatetime($value, $width, $this->locale);
+ return (string)Calendar::formatDatetime($value, $width, $this->locale);
case 'time':
- return (string) Calendar::formatTime($value, $width, $this->locale);
+ return (string)Calendar::formatTime($value, $width, $this->locale);
case 'weekdayName':
- return (string) Calendar::getWeekdayName($value, $width, $this->locale);
+ return (string)Calendar::getWeekdayName($value, $width, $this->locale);
default:
return false;
}
diff --git a/lib/private/L10N/L10NString.php b/lib/private/L10N/L10NString.php
index ea64f17eff3..a6515e78f24 100644
--- a/lib/private/L10N/L10NString.php
+++ b/lib/private/L10N/L10NString.php
@@ -1,29 +1,9 @@
<?php
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bart Visscher <bartv@thisnet.nl>
- * @author Bernhard Posselt <dev@bernhard-posselt.com>
- * @author Jakob Sack <mail@jakobsack.de>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Jörn Friedrich Dreyer <jfd@butonic.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- *
- * @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-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\L10N;
diff --git a/lib/private/L10N/LanguageIterator.php b/lib/private/L10N/LanguageIterator.php
index 8b3aaf10210..531eea5866b 100644
--- a/lib/private/L10N/LanguageIterator.php
+++ b/lib/private/L10N/LanguageIterator.php
@@ -3,26 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- *
- * @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/>.
- *
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\L10N;
diff --git a/lib/private/L10N/LanguageNotFoundException.php b/lib/private/L10N/LanguageNotFoundException.php
index d483fd799dd..087a384e00e 100644
--- a/lib/private/L10N/LanguageNotFoundException.php
+++ b/lib/private/L10N/LanguageNotFoundException.php
@@ -1,24 +1,8 @@
<?php
+
/**
- * @copyright Copyright (c) 2016 Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @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/>.
- *
+ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\L10N;
diff --git a/lib/private/L10N/LazyL10N.php b/lib/private/L10N/LazyL10N.php
index 3226c1a604b..0d72f638632 100644
--- a/lib/private/L10N/LazyL10N.php
+++ b/lib/private/L10N/LazyL10N.php
@@ -3,25 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @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/>.
- *
+ * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\L10N;