aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Minz/Request.php2
-rw-r--r--lib/Minz/Translate.php27
2 files changed, 28 insertions, 1 deletions
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/).
*/