r19109 - htdocs/trunk - Use client side prefered language by default

Geert Janssens gjanssens at code.gnucash.org
Wed May 5 09:39:09 EDT 2010


Author: gjanssens
Date: 2010-05-05 09:39:09 -0400 (Wed, 05 May 2010)
New Revision: 19109
Trac: http://svn.gnucash.org/trac/changeset/19109

Modified:
   htdocs/trunk/lang.php
   htdocs/trunk/local.php
Log:
Use client side prefered language by default
This integrates a patch by Tao Wang with some minor modifications

Modified: htdocs/trunk/lang.php
===================================================================
--- htdocs/trunk/lang.php	2010-05-05 13:10:10 UTC (rev 19108)
+++ htdocs/trunk/lang.php	2010-05-05 13:39:09 UTC (rev 19109)
@@ -1,7 +1,47 @@
 <?php
 # allow user override.
 if (array_key_exists('lang', $_GET)) { $locale = $_GET["lang"]; }
-if ($locale == "") { $locale = "en_US"; }
+
+# choose a default language based on the client browser's preferred
+# language list
+
+# key: locale, value: lang_dir
+$supported_languages = array(
+        'de_DE' => 'de', 'es_ES' => 'es', 'fr_FR' => 'fr',
+        'it_IT' => 'it', 'ja_JP' => 'ja', 'nb_NO' => 'nb',
+        'nl_NL' => 'nl', 'pl_PL' => 'pl', 'pt_PT' => 'pt_PT',
+        'zh_CN' => 'zh_CN', 'en_US' => 'en'
+        );
+
+if ($locale == "") {
+        # Get user prefered languages, and match agasint supported language
+        if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )
+        {
+                # tolower() => remove space => '-' -> '_'
+                # "fr-ch;q=0.3, en, zh-cn;q=0.7" => "fr_ch;q=0.3,en,zh_cn;q=0.7"
+                $languages = str_replace('-','_', str_replace(' ', '', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
+                $languages = explode(",", $languages);
+                foreach ($languages as $item)
+                {
+                        # "zh_cn;q=0.7" => "zh_cn"
+                        $lang = substr($item, 0, strcspn($item, ';'));
+                        $lang_short = substr($lang, 0, 2);
+                        # full match is prefer, but short match is acceptable.
+                        foreach ($supported_languages as $loc_lang => $loc_dir)
+                        {
+                                if ($lang == strtolower($loc_lang)) { $locale = $loc_lang; break; }
+                                if ($lang_short == substr($loc_lang, 0, 2 )) { $locale = $loc_lang; }
+                        }
+                        if ($locale != "") { break; }
+                }
+        }
+        # nothing matched, use default language
+        if ($locale == "") { $locale = "en_US"; }
+        $lang_dir = $supported_language[$locale];
+}
+
+# We should have a locale now, let's set up the required bits and pieces to show
+# the website in that language.
 putenv("LANG=$locale");
 putenv("LANGUAGE=$locale");
 $locale_res = setlocale(LC_ALL, "");

Modified: htdocs/trunk/local.php
===================================================================
--- htdocs/trunk/local.php	2010-05-05 13:10:10 UTC (rev 19108)
+++ htdocs/trunk/local.php	2010-05-05 13:39:09 UTC (rev 19109)
@@ -1,6 +1,5 @@
 <?php
 $top_dir = ".";
 $home = $top_dir;
-$locale = "en_US";
-$lang_dir = "";
+$locale = "";
 ?>



More information about the gnucash-changes mailing list