]> dev.renevier.net Git - syp.git/blob - inc/langutils.php
8d458fa53e08c21be76a89803d845c14ca30ab81
[syp.git] / inc / langutils.php
1 <?php
2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
3    license. */
4
5     $language_list = Array ();
6
7 function ptrans ($str) {
8     echo trans ($str);
9 }
10
11 function trans ($str) {
12     global $translations, $lang;
13
14     $res = $translations[$lang][$str];
15     if (!isset ($res) || strlen ($res) == 0) {
16         return $str;
17     } else {
18         return $res;
19     }
20 }
21
22 function parse_accept_language () {
23     global $translations;
24     $possibilities = array();
25     if (isset ($_SERVER ['HTTP_ACCEPT_LANGUAGE'])) {
26         // set to lower case now
27         $accepts = explode (',',
28                         strtolower ($_SERVER ['HTTP_ACCEPT_LANGUAGE']));
29         foreach ($accepts as $acc) {
30             if (preg_match ('/^\s*([a-zA-Z]+)(-[a-zA-Z]+)?\s*$/', $acc, $matches)) {
31                 $possibilities [$matches [1]] = 1.0;
32             }
33             if (preg_match ('/^\s*([a-zA-Z]+)(-[a-zA-Z]+)?\s*;\s*q\s*=\s*([0-9\.]+)\s*$/',
34                             $acc, $matches)) {
35                 $val = floatval ($matches [3]);
36                 if ($val > 1.0) {
37                     $val = 1.0;
38                 }
39                 $possibilities [$matches [1]] = max ($val, $possibilities [$matches [1]]);
40             }
41         }
42         arsort ($possibilities);
43         foreach ($possibilities as $lang => $value) {
44             if (isset ($translations [$lang])) {
45                 return $lang;
46             }
47         }
48     }
49     return "en"; // nothing found; default to english
50 }
51
52 function other_languages ($current_lang) {
53  $script = pathinfo ($_SERVER ["SCRIPT_NAME"], PATHINFO_FILENAME);
54     $dotpos = strpos ($script, '.');
55     if ($dotpos !== false) {
56         $script = substr ($script, 0, $dotpos);
57     }
58
59     global $translations;
60     $links = Array ();
61     foreach ($translations as $lang => $obj) {
62         $lang_name = $obj ["_language_name"];
63         if ($lang == $current_lang) {
64             array_push ($links, "<a>$lang_name</a>");
65         } else  {
66             array_push ($links, "<a href=\"$script.$lang.php\" title=\"$lang_name\" lang=\"$lang\" hreflang=\"$lang\">$lang_name</a>");
67         }
68     }                                                                                                                      
69     echo "<div id=\"other-language\">" . join("", $links) . "</div>\n";
70 }
71
72 // load languages
73 foreach (scandir ("inc/i10n/") as $entry) {
74     if (is_dir ("inc/i10n/$entry") && ($entry [0] != ".")) {
75         $target = "inc/i10n/$entry/syp.php";
76         if (is_file ($target)) {
77             include $target;
78         }
79     }
80 }
81
82 // detects language                                                                                                         
83 $fname = pathinfo ($_SERVER ["SCRIPT_NAME"], PATHINFO_FILENAME);                                                            
84 $lang = ltrim (strstr ($fname, '.'), '.');
85 if ((!isset ($lang)) ||
86     (strlen ($lang) == 0) ||
87     (!isset ($translations [$lang]))) {
88     $lang = parse_accept_language ();
89 }