[Scummvm-cvs-logs] scummvm-web master -> a5de04c699a9fda218e6ec3e0544617ed14c27f2

csnover csnover at users.noreply.github.com
Sun Apr 24 15:49:15 CEST 2016


This automated email contains information about 2 new commits which have been
pushed to the 'scummvm-web' repo located at https://github.com/scummvm/scummvm-web .

Summary:
6a44d9b54e WEB: Add support for HTTP Accept-Language
a5de04c699 WEB: Deduplicate allowed languages list


Commit: 6a44d9b54ec59e3ada7523d3cee0aba7b864a61f
    https://github.com/scummvm/scummvm-web/commit/6a44d9b54ec59e3ada7523d3cee0aba7b864a61f
Author: Colin Snover (csnover at users.noreply.github.com)
Date: 2016-04-24T13:47:49Z

Commit Message:
WEB: Add support for HTTP Accept-Language

Allows site to auto-detect language based on HTTP Accept-Language header.

Uses a lightly modified version of the parser from
http://www.thefutureoftheweb.com/blog/use-accept-language-header.

Also cleans up unnecessary direct use of $_COOKIE and $_GET, which
are both covered by $_REQUEST, and uses a single language fallback
instead of a default + fallback.

Changed paths:
    index.php



diff --git a/index.php b/index.php
index 3d2743f..94de95d 100644
--- a/index.php
+++ b/index.php
@@ -3,17 +3,47 @@
  * Multilingual support
  */
 global $lang;
-/* Default to English */
-$lang = 'en';
-/* Check if the user has set a language preference before (cookies) */
-if (!empty($_COOKIE['lang']))
-  $lang = $_COOKIE['lang'];
-/* The GET language parameter should override any stored setting */
-if (!empty($_GET['lang']))
-  $lang = $_GET['lang'];
-/* Make sure that the language is known, otherwise fall back to English */
-if (!preg_match("/^([a-z][a-z]|[a-z][a-z]_[a-z][a-z][a-z]?)$/", $lang))
-	$lang = "en";
+
+$available_languages = array(
+  'en' => true,
+  'de' => true,
+  'fr' => true,
+  'it' => true,
+  'ru' => true
+);
+
+function get_preferred_languages() {
+  $lang_parse = array();
+  preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
+
+  if (count($lang_parse[1])) {
+    $langs = array_combine($lang_parse[1], $lang_parse[4]);
+    foreach ($langs as $candidate => $quality) {
+      if ($quality === '') {
+        $langs[$candidate] = 1;
+      }
+    }
+    arsort($langs, SORT_NUMERIC);
+    return array_keys($langs);
+  }
+
+  return array();
+}
+
+if (!empty($_REQUEST['lang'])) {
+  $lang = $_REQUEST['lang'];
+} elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+  foreach (get_preferred_languages() as $candidate) {
+    $candidate_major = current(explode('-', $candidate, 1));
+    if (isset($available_languages[$candidate_major])) {
+      $lang = $candidate_major;
+      break;
+    }
+  }
+}
+
+if (!array_key_exists($lang, $available_languages))
+  $lang = 'en';
 
 /* We have to clean the mess introduced by double cookie at the wrong level */
 if (empty($_COOKIE['clear_lang'])) {


Commit: a5de04c699a9fda218e6ec3e0544617ed14c27f2
    https://github.com/scummvm/scummvm-web/commit/a5de04c699a9fda218e6ec3e0544617ed14c27f2
Author: Colin Snover (github.com at zetafleet.com)
Date: 2016-04-24T13:47:53Z

Commit Message:
WEB: Deduplicate allowed languages list

Changed paths:
    include/Controller.php
    index.php
    templates/lang_menu.tpl



diff --git a/include/Controller.php b/include/Controller.php
index 808839c..06796e2 100644
--- a/include/Controller.php
+++ b/include/Controller.php
@@ -29,7 +29,8 @@ class Controller {
 		$Smarty = $this->_smarty;
 
 		global $lang;
-		
+		global $available_languages;
+
 		/* Configure smarty. */
 		$this->_smarty->compile_dir = SMARTY_DIR_COMPILE;
 		$this->_smarty->cache_dir = SMARTY_DIR_CACHE;
@@ -92,6 +93,7 @@ class Controller {
 			'heroes_num' => HEROES_NUM,
 			'menus' => $menus,
 			'pageurl' => $pageurl,
+			'available_languages' => $available_languages,
 		);
 		$this->_smarty->assign($vars);
 	}
diff --git a/index.php b/index.php
index 94de95d..75c9cb7 100644
--- a/index.php
+++ b/index.php
@@ -3,13 +3,13 @@
  * Multilingual support
  */
 global $lang;
-
+global $available_languages;
 $available_languages = array(
-  'en' => true,
-  'de' => true,
-  'fr' => true,
-  'it' => true,
-  'ru' => true
+  'en' => 'English',
+  'de' => 'Deutsch',
+  'fr' => 'Français',
+  'it' => 'Italiano',
+  'ru' => 'Русский'
 );
 
 function get_preferred_languages() {
diff --git a/templates/lang_menu.tpl b/templates/lang_menu.tpl
index d607356..5466e34 100644
--- a/templates/lang_menu.tpl
+++ b/templates/lang_menu.tpl
@@ -3,11 +3,9 @@
         <ul id="langmenu">
             <li><a href=""><img src="/images/lang-icon.png" width="16" height="16" alt="Language">  Language</a>
                 <ul>
-                    <li><a href="{$pageurl}?lang=en"><code class="text-badge">EN</code>English</a></li>
-                    <li><a href="{$pageurl}?lang=de"><code class="text-badge">DE</code>Deutsch</a></li>
-                    <li><a href="{$pageurl}?lang=fr"><code class="text-badge">FR</code>Français</a></li>
-                    <li><a href="{$pageurl}?lang=it"><code class="text-badge">IT</code>Italiano</a></li>
-                    <li><a href="{$pageurl}?lang=ru"><code class="text-badge">RU</code>Русский</a></li>
+                    {foreach from=$available_languages key=key item=item}
+                    <li><a href="{$pageurl}?lang={$key}"><code class="text-badge">{$key|strtoupper}</code>{$item|escape}</a></li>
+                    {/foreach}
                 </ul>
             </li>
         </ul>






More information about the Scummvm-git-logs mailing list