aboutsummaryrefslogtreecommitdiff
path: root/lib/Minz/Translate.php
diff options
context:
space:
mode:
authorGravatar Alexis Degrugillier <aledeg@users.noreply.github.com> 2020-06-05 10:10:46 +0200
committerGravatar GitHub <noreply@github.com> 2020-06-05 10:10:46 +0200
commit36bda2e715ed822cc495ff419ad565084e241f43 (patch)
tree6a4fd79cd42ab76cab2338eedb02f787e4e2c3a8 /lib/Minz/Translate.php
parentd4554fa087f9057610085ca685cd8fb79d8f2bd0 (diff)
Add language detection when the user is not logged in (#3022)
Before, when the user was not logged in, pages where translated with the '_' user language. Now, they are translated with the user preferred language if there is one supported by FreshRSS or with the system default language.
Diffstat (limited to 'lib/Minz/Translate.php')
-rw-r--r--lib/Minz/Translate.php27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/Minz/Translate.php b/lib/Minz/Translate.php
index db76e9214..6d5d05c6f 100644
--- a/lib/Minz/Translate.php
+++ b/lib/Minz/Translate.php
@@ -63,6 +63,8 @@ class Minz_Translate {
public static function availableLanguages() {
$list_langs = array();
+ self::registerPath(APP_PATH . '/i18n');
+
foreach (self::$path_list as $path) {
$scan = scandir($path);
if (is_array($scan)) {
@@ -80,6 +82,31 @@ class Minz_Translate {
}
/**
+ * Return the language to use in the application.
+ * It returns the connected language if it exists then returns the first match from the
+ * preferred languages then returns the default language
+ * @param $user the connected user language (nullable)
+ * @param $preferred an array of the preferred languages
+ * @param $default the preferred language to use
+ * @return a string containing the language to use
+ */
+ public static function getLanguage($user, $preferred, $default) {
+ if (null !== $user) {
+ return $user;
+ }
+
+ $languages = Minz_Translate::availableLanguages();
+ foreach ($preferred as $language) {
+ $language = strtolower($language);
+ if (in_array($language, $languages, true)) {
+ return $language;
+ }
+ }
+
+ return $default;
+ }
+
+ /**
* Register a new path.
* @param $path a path containing i18n directories (e.g. ./en/, ./fr/).
*/