]> git.p6c8.net - jirafeau_project.git/blob - lib/lang.php
[TASK] update release task list
[jirafeau_project.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 $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 $visitor_langs = t_parse_accept_language();
45 array_push($visitor_langs, "en");
46 return $visitor_langs;
47 }
48
49 function t_parse_accept_language() {
50 if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
51 return [];
52 }
53 // Example: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5
54 $langs = [];
55 $cols = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
56 foreach ($cols as $i => $semicols) {
57 $lang = explode(';', $semicols);
58 if (count($lang) === 0) {
59 continue;
60 }
61 array_push($langs, $lang[0]);
62 }
63 return $langs;
64 }
65
66 function t_in($string_id, $lang) {
67 $trans = t_get_json($lang);
68 if ($trans === false) {
69 return false;
70 }
71 if (!array_key_exists($string_id, $trans)) {
72 return false;
73 }
74 return $trans[$string_id];
75 }
76
77 function t_get_raw_json($lang) {
78 $filename = str_replace("-", "_", $lang);
79 if (preg_match('/[^A-Za-z_\w]/', $input)) {
80 return false;
81 }
82 $p = JIRAFEAU_ROOT . "lib/locales/$filename.json";
83 if (!file_exists($p)) {
84 return false;
85 }
86 $json = file_get_contents($p);
87 if ($json === false) {
88 return false;
89 }
90 return $json;
91 }
92
93 function t_get_json($lang) {
94 $raw_j = t_get_raw_json($lang);
95 $array = json_decode($raw_j, true);
96 if ($array === null) {
97 return false;
98 }
99 return $array;
100 }
101
102 function json_lang_generator($lang) {
103 $r = false;
104 if ($lang === null) {
105 $lang_config = $GLOBALS['cfg']['lang'];
106 if ($lang_config != "auto") {
107 $r = t_get_raw_json($lang_config);
108 } else {
109 foreach(t_visitor_langs() as $lang) {
110 $r = t_get_raw_json($lang);
111 if (!($r === false)) {
112 break;
113 }
114 }
115 }
116 } else {
117 $r = t_get_raw_json($lang);
118 }
119 return $r === false ? "{}" : $r;
120 }

patrick-canterino.de