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

patrick-canterino.de