aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--app/Controllers/authController.php5
-rw-r--r--app/FreshRSS.php11
-rw-r--r--app/views/auth/register.phtml10
-rw-r--r--lib/Minz/Request.php2
-rw-r--r--lib/Minz/Translate.php27
5 files changed, 50 insertions, 5 deletions
diff --git a/app/Controllers/authController.php b/app/Controllers/authController.php
index d61472e53..d158092bf 100644
--- a/app/Controllers/authController.php
+++ b/app/Controllers/authController.php
@@ -148,6 +148,8 @@ class FreshRSS_auth_Controller extends Minz_ActionController {
FreshRSS_FormAuth::deleteCookie();
}
+ Minz_Translate::init($conf->language);
+
// All is good, go back to the index.
Minz_Request::good(_t('feedback.auth.login.success'),
array('c' => 'index', 'a' => 'index'));
@@ -191,6 +193,8 @@ class FreshRSS_auth_Controller extends Minz_ActionController {
Minz_Session::_param('csrf');
FreshRSS_Auth::giveAccess();
+ Minz_Translate::init($conf->language);
+
Minz_Request::good(_t('feedback.auth.login.success'),
array('c' => 'index', 'a' => 'index'));
} else {
@@ -231,6 +235,7 @@ class FreshRSS_auth_Controller extends Minz_ActionController {
$this->view->show_tos_checkbox = file_exists(join_path(DATA_PATH, 'tos.html'));
$this->view->show_email_field = FreshRSS_Context::$system_conf->force_email_validation;
+ $this->view->preferred_language = Minz_Translate::getLanguage(null, Minz_Request::getPreferredLanguages(), FreshRSS_Context::$system_conf->language);
Minz_View::prependTitle(_t('gen.auth.registration.title') . ' ยท ');
}
}
diff --git a/app/FreshRSS.php b/app/FreshRSS.php
index 32c3c39d0..ee454d7fd 100644
--- a/app/FreshRSS.php
+++ b/app/FreshRSS.php
@@ -68,7 +68,7 @@ class FreshRSS extends Minz_FrontController {
// Basic protection against XSRF attacks
FreshRSS_Auth::removeAccess();
$http_referer = empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER'];
- Minz_Translate::init('en'); //TODO: Better choice of fallback language
+ self::initI18n();
Minz_Error::error(403, array('error' => array(
_t('feedback.access.denied'),
' [HTTP_REFERER=' . htmlspecialchars($http_referer, ENT_NOQUOTES, 'UTF-8') . ']'
@@ -80,7 +80,7 @@ class FreshRSS extends Minz_FrontController {
!FreshRSS_Auth::hasAccess('admin'))
)) {
// Token-based protection against XSRF attacks, except for the login or self-create user forms
- Minz_Translate::init('en'); //TODO: Better choice of fallback language
+ self::initI18n();
Minz_Error::error(403, array('error' => array(
_t('feedback.access.denied'),
' [CSRF]'
@@ -90,8 +90,11 @@ class FreshRSS extends Minz_FrontController {
}
private static function initI18n() {
- Minz_Session::_param('language', FreshRSS_Context::$user_conf->language);
- Minz_Translate::init(FreshRSS_Context::$user_conf->language);
+ $selected_language = FreshRSS_Auth::hasAccess() ? FreshRSS_Context::$user_conf->language : null;
+ $language = Minz_Translate::getLanguage($selected_language, Minz_Request::getPreferredLanguages(), FreshRSS_Context::$system_conf->language);
+
+ Minz_Session::_param('language', $language);
+ Minz_Translate::init($language);
}
public static function loadStylesAndScripts() {
diff --git a/app/views/auth/register.phtml b/app/views/auth/register.phtml
index 3ee53356f..b2494a8ab 100644
--- a/app/views/auth/register.phtml
+++ b/app/views/auth/register.phtml
@@ -5,6 +5,16 @@
<input type="hidden" name="_csrf" value="<?= FreshRSS_Auth::csrfToken() ?>" />
<div class="form-group">
+ <label for="new_user_language"><?= _t('admin.user.language') ?></label>
+ <select name="new_user_language" id="new_user_language">
+ <?php $languages = Minz_Translate::availableLanguages(); ?>
+ <?php foreach ($languages as $lang) { ?>
+ <option value="<?= $lang ?>"<?= $this->preferred_language === $lang ? ' selected="selected"' : '' ?>><?= _t('gen.lang.' . $lang) ?></option>
+ <?php } ?>
+ </select>
+ </div>
+
+ <div class="form-group">
<label for="new_user_name"><?= _t('gen.auth.username') ?></label>
<input id="new_user_name" name="new_user_name" type="text" size="16" required="required" autocomplete="off" pattern="<?= FreshRSS_user_Controller::USERNAME_PATTERN ?>" autocapitalize="off" />
<p class="help"><?= _i('help') ?> <?= _t('gen.auth.username.format') ?></p>
diff --git a/lib/Minz/Request.php b/lib/Minz/Request.php
index b294abe26..39d172c7e 100644
--- a/lib/Minz/Request.php
+++ b/lib/Minz/Request.php
@@ -395,7 +395,7 @@ class Minz_Request {
/**
* @return array
*/
- public static function getPreferredLanguage() {
+ public static function getPreferredLanguages() {
if (preg_match_all('/(^|,)\s*(?P<lang>[^;,]+)/', static::getHeader('HTTP_ACCEPT_LANGUAGE'), $matches)) {
return $matches['lang'];
}
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/).
*/