]> git.p6c8.net - jirafeau_mojo42.git/blob - lib/lang.php
[TASK] Refactor lang engine
[jirafeau_mojo42.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 global $languages_list;
21 $languages_list = array('auto' => 'Automatic',
22 'de' => 'Deutsch',
23 'en' => 'English',
24 'el' => 'Ελληνικά',
25 'es' => 'Español',
26 'hu' => 'Magyar',
27 'fi' => 'Suomi',
28 'fr' => 'Français',
29 'it' => 'Italiano',
30 'nl' => 'Nederlands',
31 'pl' => 'Polszczyzna',
32 'pt' => 'português',
33 'pt_BR' => 'português (Brasil)',
34 'ro' => 'Limba română',
35 'ru' => 'ру́сский',
36 'sk' => 'Slovenčina',
37 'tr' => 'Türkçe',
38 'zh' => '汉语');
39
40 function t($string_id)
41 {
42 $r = t_in($string_id, t_select_lang());
43 if ($r === false) {
44 $r = t_in($string_id, "en");
45 if ($r === false) {
46 return "";
47 }
48 }
49 return $r;
50 }
51
52 function t_select_lang() {
53 $cfg = $GLOBALS['cfg'];
54 if (strcmp($cfg['lang'], 'auto') != 0) {
55 return $cfg['lang'];
56 }
57 else if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
58 return substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
59 } else {
60 return "en";
61 }
62 }
63
64 function t_in($string_id, $lang) {
65 $trans = t_get_json($lang);
66 if ($trans === false) {
67 return false;
68 }
69 if (!array_key_exists($string_id, $trans)) {
70 return false;
71 }
72 return $trans[$string_id];
73 }
74
75 function t_get_raw_json($lang) {
76 $p = JIRAFEAU_ROOT . "lib/locales/$lang.json";
77 if (!file_exists($p)) {
78 return false;
79 }
80 $json = file_get_contents($p);
81 if ($json === false) {
82 return false;
83 }
84 return $json;
85 }
86
87 function t_get_json($lang) {
88 $raw_j = t_get_raw_json($lang);
89 $array = json_decode($raw_j, true);
90 if ($array === null) {
91 return false;
92 }
93 return $array;
94 }
95
96 function json_lang_generator($lang) {
97 $r = "";
98 if ($lang === null) {
99 $r = t_get_raw_json(t_select_lang());
100 } else {
101 $r = t_get_raw_json($lang);
102 }
103 return $r === false ? "{}" : $r;
104 }

patrick-canterino.de