]>
git.p6c8.net - jirafeau_project.git/blob - lib/lang.php
3 * Jirafeau, your web file repository
4 * Copyright (C) 2008 Julien "axolotl" BERNARD <axolotl@magieeternelle.org>
5 * Copyright (C) 2015 Jerome Jutteau <jerome@jutteau.fr>
6 * Copyright (C) 2024 Jirafeau project <https://gitlab.com/jirafeau> (see AUTHORS.md)
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 function t($string_id)
24 $lang_config = $GLOBALS['cfg']['lang'];
25 if ($lang_config != "auto") {
26 $r = t_in($string_id, $lang_config);
27 if ($r === false ||
$r === "") {
33 $visitor_langs = t_visitor_langs();
34 foreach ($visitor_langs as $lang) {
35 $r = t_in($string_id, $lang);
36 if ($r === false ||
$r === "") {
45 function t_visitor_langs()
47 $visitor_langs = t_parse_accept_language();
48 array_push($visitor_langs, "en");
49 return $visitor_langs;
52 function t_parse_accept_language()
54 if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
57 // Example: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5
59 $cols = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
60 foreach ($cols as $i => $semicols) {
61 $lang = explode(';', $semicols);
62 if (count($lang) === 0) {
65 array_push($langs, $lang[0]);
70 function t_in($string_id, $lang)
72 $trans = t_get_json($lang);
73 if ($trans === false) {
76 if (!array_key_exists($string_id, $trans)) {
79 return $trans[$string_id];
82 function t_get_raw_json($lang)
84 $filename = str_replace("-", "_", $lang);
85 if (preg_match('/[^A-Za-z_\w]/', $filename)) {
88 $p = JIRAFEAU_ROOT
. "lib/locales/$filename.json";
89 if (!file_exists($p)) {
92 $json = file_get_contents($p);
93 if ($json === false) {
99 function t_get_json($lang)
101 $raw_j = t_get_raw_json($lang);
102 $array = json_decode($raw_j, true);
103 if ($array === null) {
109 function json_lang_generator($lang)
112 if ($lang === null) {
113 $lang_config = $GLOBALS['cfg']['lang'];
114 if ($lang_config != "auto") {
115 $r = t_get_raw_json($lang_config);
117 foreach (t_visitor_langs() as $lang) {
118 $r = t_get_raw_json($lang);
119 if (!($r === false)) {
125 $r = t_get_raw_json($lang);
127 return $r === false ?
"{}" : $r;
patrick-canterino.de