]> git.p6c8.net - jirafeau.git/blob - lib/lang.php
[FEATURE] Advertise javascript license for LibreJS
[jirafeau.git] / lib / lang.php
1 <?php
2 /*
3 * Jirafeau, your web file repository
4 * Copyright (C) 2015 Jerome Jutteau <j.jutteau@gmail.com>
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 $r = t_in($string_id, t_select_lang());
23 if ($r === false || $r === "") {
24 $r = t_in($string_id, "en");
25 if ($r === false) {
26 return "FIX ME: " . $string_id;
27 }
28 }
29 return $r;
30 }
31
32 function t_select_lang() {
33 $cfg = $GLOBALS['cfg'];
34 if (strcmp($cfg['lang'], 'auto') != 0) {
35 return $cfg['lang'];
36 }
37 else if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
38 return substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
39 } else {
40 return "en";
41 }
42 }
43
44 function t_in($string_id, $lang) {
45 $trans = t_get_json($lang);
46 if ($trans === false) {
47 return false;
48 }
49 if (!array_key_exists($string_id, $trans)) {
50 return false;
51 }
52 return $trans[$string_id];
53 }
54
55 function t_get_raw_json($lang) {
56 $p = JIRAFEAU_ROOT . "lib/locales/$lang.json";
57 if (!file_exists($p)) {
58 return false;
59 }
60 $json = file_get_contents($p);
61 if ($json === false) {
62 return false;
63 }
64 return $json;
65 }
66
67 function t_get_json($lang) {
68 $raw_j = t_get_raw_json($lang);
69 $array = json_decode($raw_j, true);
70 if ($array === null) {
71 return false;
72 }
73 return $array;
74 }
75
76 function json_lang_generator($lang) {
77 $r = "";
78 if ($lang === null) {
79 $r = t_get_raw_json(t_select_lang());
80 } else {
81 $r = t_get_raw_json($lang);
82 }
83 return $r === false ? "{}" : $r;
84 }

patrick-canterino.de