3 require_once("include/dba.php");
12 * Get the language setting directly from system variables, bypassing get_config()
13 * as database may not yet be configured.
15 * If possible, we use the value from the browser.
20 if(! function_exists('get_browser_language')) {
21 function get_browser_language() {
23 if (x($_SERVER,'HTTP_ACCEPT_LANGUAGE')) {
24 // break up string into pieces (languages and q factors)
25 preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
26 $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
28 if (count($lang_parse[1])) {
29 // create a list like "en" => 0.8
30 $langs = array_combine($lang_parse[1], $lang_parse[4]);
32 // set default to 1 for any without q factor
33 foreach ($langs as $lang => $val) {
34 if ($val === '') $langs[$lang] = 1;
37 // sort list based on value
38 arsort($langs, SORT_NUMERIC);
42 if(isset($langs) && count($langs)) {
43 foreach ($langs as $lang => $v) {
44 if(file_exists("view/$lang") && is_dir("view/$lang")) {
55 return ((isset($a->config['system']['language'])) ? $a->config['system']['language'] : 'en');
59 function push_lang($language) {
64 if($language === $lang)
67 if(isset($a->strings) && count($a->strings)) {
68 $a->stringsave = $a->strings;
70 $a->strings = array();
71 load_translation_table($language);
78 if($lang === $a->langsave)
81 if(isset($a->stringsave))
82 $a->strings = $a->stringsave;
84 $a->strings = array();
92 if(! function_exists('load_translation_table')) {
94 * load string translation table for alternate language
96 * first plugin strings are loaded, then globals
98 * @param string $lang language code to load
100 function load_translation_table($lang) {
103 $a->strings = array();
104 // load enabled plugins strings
105 $plugins = q("SELECT name FROM addon WHERE installed=1;");
106 if ($plugins!==false) {
107 foreach($plugins as $p) {
109 if(file_exists("addon/$name/lang/$lang/strings.php")) {
110 include("addon/$name/lang/$lang/strings.php");
115 if(file_exists("view/$lang/strings.php")) {
116 include("view/$lang/strings.php");
121 // translate string if translation exists
123 if(! function_exists('t')) {
128 if(x($a->strings,$s)) {
129 $t = $a->strings[$s];
130 return is_array($t)?$t[0]:$t;
135 if(! function_exists('tt')){
136 function tt($singular, $plural, $count){
140 if(x($a->strings,$singular)) {
141 $t = $a->strings[$singular];
142 $f = 'string_plural_select_' . str_replace('-','_',$lang);
143 if(! function_exists($f))
144 $f = 'string_plural_select_default';
146 return is_array($t)?$t[$k]:$t;
156 // provide a fallback which will not collide with
157 // a function defined in any language file
159 if(! function_exists('string_plural_select_default')) {
160 function string_plural_select_default($n) {
166 * Return installed languages as associative array
172 function get_avaiable_languages() {
173 $lang_choices = array();
174 $langs = glob('view/*/strings.php'); /**/
176 if(is_array($langs) && count($langs)) {
177 if(! in_array('view/en/strings.php',$langs))
178 $langs[] = 'view/en/';
180 foreach($langs as $l) {
181 $t = explode("/",$l);
182 $lang_choices[$t[1]] = $t[1];
185 return $lang_choices;