root/alternc/tags/0.9.7/bureau/class/lang_env.php

Revision 1997, 1.5 kB (checked in by anarcat, 1 year ago)

fix a longstanding issue in AlternC: don't show translation links for
which AlternC is not translated

this is done by checking if the locales/LANG directory exists, so it's
not foolproof, but it's better than simply checking if the locale is
configured (which is still done).

a bit of code refactoring was done while i'm here

Line 
1 <?php
2
3 function update_locale($langpath) {
4   $locales=array();
5   $f=@fopen("/etc/locale.gen","rb");
6   if ($f) {
7     while ($s=fgets($f,1024)) {
8       if (preg_match("/^([a-z][a-z]_[A-Z][A-Z])/",trim($s),$mat) && file_exists($langpath . '/' . $mat[1])) {
9     $locales[$mat[1]]=$mat[1];
10       }
11     }
12     fclose($f);
13   }
14   return $locales;
15 }
16
17 // setlang is on the link at the login page
18 if (isset($_REQUEST["setlang"])) {
19   $lang=$_REQUEST["setlang"];
20 }
21
22 $langpath = bindtextdomain("alternc", "/var/alternc/bureau/locales");
23
24 // Create or update a locale.php file if it is outdated.
25 $locales = update_locale($langpath);
26
27 if (!(isset($lang))) {  // Use the browser first preferred language
28   $lang=strtolower(substr(trim($_SERVER["HTTP_ACCEPT_LANGUAGE"]),0,5));
29 }
30
31
32 if (!$locales[$lang]) { // Requested language not found in locales
33   // treat special cases such as en_AU or fr_BF : use the language only, not the country.
34   $ll=substr($lang,0,2);
35   foreach($locales as $l) {
36     if (substr($l,0,2)==$ll) {
37       $lang=$l;
38       break;
39     }
40   }
41 }
42
43 if (!isset($locales[$lang])) $lang=$locales[0];
44
45 if (isset($setlang) && isset($lang)) {
46   setcookie("lang",$lang);
47 }
48
49 // User chose a non existent language, select the first available one
50 if ($lang == NULL) {
51   $lang = "en_US";
52 }
53
54 /* Language ok, set the locale environment */
55 putenv("LC_MESSAGES=".$lang);
56 putenv("LANG=".$lang);
57 putenv("LANGUAGE=".$lang);
58 // this locale MUST be selected in "dpkg-reconfigure locales"
59 setlocale(LC_ALL,$lang);
60 textdomain("alternc");
61
62 ?>
63
Note: See TracBrowser for help on using the browser.