]>
git.p6c8.net - jirafeau_mojo42.git/blob - lib/lang.php
3 * Jirafeau, your web file repository
4 * Copyright (C) 2015 Jerome Jutteau <jerome@jutteau.fr>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 function t($string_id)
22 $lang_config = $GLOBALS['cfg']['lang'];
23 if ($lang_config != "auto") {
24 $r = t_in($string_id, $lang_config);
25 if ($r === false ||
$r === "") {
31 $visitor_langs = t_visitor_langs();
32 foreach ($visitor_langs as $lang) {
33 $r = t_in($string_id, $lang);
34 if ($r === false ||
$r === "") {
43 function t_visitor_langs()
45 $visitor_langs = t_parse_accept_language();
46 array_push($visitor_langs, "en");
47 return $visitor_langs;
50 function t_parse_accept_language()
52 if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
55 // Example: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5
57 $cols = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
58 foreach ($cols as $i => $semicols) {
59 $lang = explode(';', $semicols);
60 if (count($lang) === 0) {
63 array_push($langs, $lang[0]);
68 function t_in($string_id, $lang)
70 $trans = t_get_json($lang);
71 if ($trans === false) {
74 if (!array_key_exists($string_id, $trans)) {
77 return $trans[$string_id];
80 function t_get_raw_json($lang)
82 $filename = str_replace("-", "_", $lang);
83 if (preg_match('/[^A-Za-z_\w]/', $filename)) {
86 $p = JIRAFEAU_ROOT
. "lib/locales/$filename.json";
87 if (!file_exists($p)) {
90 $json = file_get_contents($p);
91 if ($json === false) {
97 function t_get_json($lang)
99 $raw_j = t_get_raw_json($lang);
100 $array = json_decode($raw_j, true);
101 if ($array === null) {
107 function json_lang_generator($lang)
110 if ($lang === null) {
111 $lang_config = $GLOBALS['cfg']['lang'];
112 if ($lang_config != "auto") {
113 $r = t_get_raw_json($lang_config);
115 foreach (t_visitor_langs() as $lang) {
116 $r = t_get_raw_json($lang);
117 if (!($r === false)) {
123 $r = t_get_raw_json($lang);
125 return $r === false ?
"{}" : $r;
patrick-canterino.de