]> git.p6c8.net - jirafeau.git/blob - lib/lang.php
Merge branch 'bug_admin_download_encrypted' into 'next-release'
[jirafeau.git] / lib / lang.php
1 <?php
2 /*
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)
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
22 function t($string_id)
23 {
24 $lang_config = $GLOBALS['cfg']['lang'];
25 if ($lang_config != "auto") {
26 $r = t_in($string_id, $lang_config);
27 if ($r === false || $r === "") {
28 return "FIX ME";
29 }
30 return $r;
31 }
32
33 $visitor_langs = t_visitor_langs();
34 foreach ($visitor_langs as $lang) {
35 $r = t_in($string_id, $lang);
36 if ($r === false || $r === "") {
37 continue;
38 } else {
39 return $r;
40 }
41 }
42 return "FIX ME";
43 }
44
45 function t_visitor_langs()
46 {
47 $visitor_langs = t_parse_accept_language();
48 array_push($visitor_langs, "en");
49 return $visitor_langs;
50 }
51
52 function t_parse_accept_language()
53 {
54 if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
55 return [];
56 }
57 // Example: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5
58 $langs = [];
59 $cols = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
60 foreach ($cols as $i => $semicols) {
61 $lang = explode(';', $semicols);
62 if (count($lang) === 0) {
63 continue;
64 }
65 array_push($langs, $lang[0]);
66 }
67 return $langs;
68 }
69
70 function t_in($string_id, $lang)
71 {
72 $trans = t_get_json($lang);
73 if ($trans === false) {
74 return false;
75 }
76 if (!array_key_exists($string_id, $trans)) {
77 return false;
78 }
79 return $trans[$string_id];
80 }
81
82 function t_get_raw_json($lang)
83 {
84 $filename = str_replace("-", "_", $lang);
85 if (preg_match('/[^A-Za-z_\w]/', $filename)) {
86 return false;
87 }
88 $p = JIRAFEAU_ROOT . "lib/locales/$filename.json";
89 if (!file_exists($p)) {
90 return false;
91 }
92 $json = file_get_contents($p);
93 if ($json === false) {
94 return false;
95 }
96 return $json;
97 }
98
99 function t_get_json($lang)
100 {
101 $raw_j = t_get_raw_json($lang);
102 $array = json_decode($raw_j, true);
103 if ($array === null) {
104 return false;
105 }
106 return $array;
107 }
108
109 function json_lang_generator($lang)
110 {
111 $r = false;
112 if ($lang === null) {
113 $lang_config = $GLOBALS['cfg']['lang'];
114 if ($lang_config != "auto") {
115 $r = t_get_raw_json($lang_config);
116 } else {
117 foreach (t_visitor_langs() as $lang) {
118 $r = t_get_raw_json($lang);
119 if (!($r === false)) {
120 break;
121 }
122 }
123 }
124 } else {
125 $r = t_get_raw_json($lang);
126 }
127 return $r === false ? "{}" : $r;
128 }

patrick-canterino.de