Alternc  3.2
Alternc logiel libre pour l'hébergement
 All Data Structures Namespaces Files Functions Variables Pages
lang_env.php
Go to the documentation of this file.
1 <?php
2 
3 $lang_translation=array( # If you comment lang here, it won't be displayed.
4  "fr_FR" => "Français",
5  "en_US" => "English",
6 # "es_ES" => "Español",
7 # "it_IT" => "Italiano",
8 # "de_DE" => "Deutsch",
9 # "pt_BR" => "Portuguese",
10  "nl_NL" => "Dutch",
11  );
12 
13 global $arr_lang_translation ;
14 $arr_lang_translation = $lang_translation; // not pretty but I don't want side effect right now
15 
16 function update_locale($langpath) {
17  global $arr_lang_translation;
18  $locales=array();
19  $file=file("/etc/locale.gen", FILE_SKIP_EMPTY_LINES);
20  if (! is_array($file) ) return $locales;
21  foreach ($file as $v ) {
22  if ( (preg_match("/^([a-z][a-z]_[A-Z][A-Z])/",trim($v),$mat) && file_exists($langpath . '/' . $mat[1]) ) ) {
23  if (! array_key_exists($mat[1], $arr_lang_translation) ) continue;
24  $locales[$mat[1]]=$mat[1];
25  }
26  }
27  if (!count($locales)) {
28  $locales=array("en_US"=>"en_US");
29  }
30  return $locales;
31 }
32 
33 // setlang is on the link at the login page
34 if (isset($_REQUEST["setlang"])) {
35  $lang=$_REQUEST["setlang"];
36  $setlang=$_REQUEST["setlang"];
37 } elseif (isset($_COOKIE['lang'])) {
38  $lang=$_COOKIE['lang'];
39 }
40 
41 $langpath = bindtextdomain("alternc", ALTERNC_LOCALES);
42 
43 // Create or update a locale.php file if it is outdated.
44 $locales = update_locale($langpath);
45 
46 // Default to en_US :
47 if (!isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
48  $_SERVER["HTTP_ACCEPT_LANGUAGE"]="en_US";
49 }
50 
51 if (!(isset($lang))) { // Use the browser first preferred language
52  $lang=strtolower(substr(trim($_SERVER["HTTP_ACCEPT_LANGUAGE"]),0,5));
53 }
54 
55 
56 if (! isset($locales[$lang])) { // Requested language not found in locales
57  // treat special cases such as en_AU or fr_BF : use the language only, not the country.
58  $ll=substr($lang,0,2);
59  foreach($locales as $l) {
60  if (substr($l,0,2)==$ll) {
61  $lang=$l;
62  break;
63  }
64  }
65 }
66 
67 if (!isset($locales[$lang])) list($lang)=each($locales);
68 
69 if (isset($setlang) && isset($lang)) {
70  setcookie("lang",$lang);
71 }
72 
73 // User chose a non existent language, select the first available one
74 if ($lang == NULL) {
75  $lang = "en_US";
76 }
77 
78 /* Language ok, set the locale environment */
79 putenv("LC_MESSAGES=".$lang);
80 putenv("LANG=".$lang);
81 putenv("LANGUAGE=".$lang);
82 // this locale MUST be selected in "dpkg-reconfigure locales"
83 setlocale(LC_ALL,$lang);
84 textdomain("alternc");
85 
86 $empty="";
87 if (_($empty) && preg_match("#charset=([A-Za-z0-9\.-]*)#",_($empty),$mat)) {
88  $charset=$mat[1];
89  }
90 if (! isset($charset) || !$charset) $charset="UTF-8";
91 bind_textdomain_codeset("alternc","$charset");
92 
93 ?>