aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/L10N
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/L10N')
-rw-r--r--lib/private/L10N/Factory.php79
-rw-r--r--lib/private/L10N/L10N.php20
-rw-r--r--lib/private/L10N/LanguageNotFoundException.php1
3 files changed, 75 insertions, 25 deletions
diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php
index 1825719fb75..6a747744829 100644
--- a/lib/private/L10N/Factory.php
+++ b/lib/private/L10N/Factory.php
@@ -58,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(
@@ -81,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;
}
@@ -96,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);
@@ -119,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
@@ -127,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;
}
@@ -184,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;
}
@@ -364,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) {
@@ -391,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) {
@@ -401,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');
}
/**
@@ -444,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;
}
}
@@ -472,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';
}
@@ -576,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 39d778f80d6..50db373a65d 100644
--- a/lib/private/L10N/L10N.php
+++ b/lib/private/L10N/L10N.php
@@ -83,7 +83,7 @@ class L10N implements IL10N {
$parameters = [$parameters];
}
- return (string) new L10NString($this, $text, $parameters);
+ return (string)new L10NString($this, $text, $parameters);
}
/**
@@ -104,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);
}
/**
@@ -146,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();
@@ -167,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/LanguageNotFoundException.php b/lib/private/L10N/LanguageNotFoundException.php
index 31b08b2ec14..087a384e00e 100644
--- a/lib/private/L10N/LanguageNotFoundException.php
+++ b/lib/private/L10N/LanguageNotFoundException.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later