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

patrick-canterino.de