]> git.p6c8.net - jirafeau_mojo42.git/blob - lib/lang.php
f851b12e910aa8cb405a7240f457341f06091bd0
[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 'ro' => 'Limba română',
32 'ru' => 'ру́сский',
33 'sk' => 'Slovenčina',
34 'tr' => 'Türkçe',
35 'zh' => '汉语');
36
37 /* Translation */
38 function t($text)
39 {
40 $cfg = $GLOBALS['cfg'];
41 $languages_list = $GLOBALS['languages_list'];
42
43 /* Detect user's langage if we are in automatic mode. */
44 if (strcmp($cfg['lang'], 'auto') == 0) {
45 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
46 $l = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
47 } else {
48 $l = "en";
49 }
50 } else {
51 $l = $cfg['lang'];
52 }
53
54 /* Is the langage in the list ? */
55 $found = false;
56 foreach ($languages_list as $key => $v) {
57 if (strcmp($l, $key) == 0) {
58 $found = true;
59 }
60 }
61
62 /* Don't translate english. */
63 if (!($found && strcmp($l, "en"))) {
64 return $text;
65 }
66
67 /* Open translation file. */
68 $trans_j = file_get_contents(JIRAFEAU_ROOT . "lib/locales/$l.json");
69 if ($trans_j === false) {
70 return $text;
71 }
72
73 /* Decode JSON. */
74 $trans = json_decode($trans_j, true);
75 if ($trans === null) {
76 return $text;
77 }
78
79 /* Try to find translation. */
80 if (!array_key_exists($text, $trans)) {
81 return $text;
82 }
83
84 return $trans[$text];
85 }
86
87 function json_lang_generator()
88 {
89 $cfg = $GLOBALS['cfg'];
90 $languages_list = $GLOBALS['languages_list'];
91
92 /* Detect user's langage if we are in automatic mode. */
93 if (strcmp($cfg['lang'], 'auto') == 0) {
94 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
95 $l = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
96 } else {
97 $l = "en";
98 }
99 } else {
100 $l = $cfg['lang'];
101 }
102
103 /* Is the langage in the list ? */
104 $found = false;
105 foreach ($languages_list as $key => $v) {
106 if (strcmp($l, $key) == 0) {
107 $found = true;
108 }
109 }
110
111 /* Don't translate english. */
112 if (!($found && strcmp($l, "en"))) {
113 return "{}";
114 }
115
116 /* Open translation file. */
117 $trans_j = file_get_contents(JIRAFEAU_ROOT . "lib/locales/$l.json");
118 return $trans_j;
119 }

patrick-canterino.de