]> git.p6c8.net - jirafeau_project.git/blob - lib/lang.php
4271c7f0e0c700075fad029bee2b51804df99927
[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 global $languages_list;
21 $languages_list = array ('auto' => 'Automatic',
22 'de' => 'Deutsch',
23 'en' => 'English',
24 'el' => 'Ελληνικά',
25 'hu' => 'Magyar',
26 'fi' => 'Suomi',
27 'fr' => 'Français',
28 'it' => 'Italiano',
29 'nl' => 'Nederlands',
30 'ro' => 'Limba română',
31 'sk' => 'Slovenčina',
32 'zh' => '汉语');
33
34 /* Translation */
35 function t ($text)
36 {
37 $cfg = $GLOBALS['cfg'];
38 $languages_list = $GLOBALS['languages_list'];
39
40 /* Detect user's langage if we are in automatic mode. */
41 if (strcmp ($cfg['lang'], 'auto') == 0)
42 {
43 if (isset ($_SERVER['HTTP_ACCEPT_LANGUAGE']))
44 $l = substr ($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
45 else
46 $l = "en";
47 }
48 else
49 $l = $cfg['lang'];
50
51 /* Is the langage in the list ? */
52 $found = false;
53 foreach ($languages_list as $key => $v)
54 if (strcmp ($l, $key) == 0)
55 $found = true;
56
57 /* Don't translate english. */
58 if (!($found && strcmp ($l, "en")))
59 return $text;
60
61 /* Open translation file. */
62 $trans_j = file_get_contents (JIRAFEAU_ROOT . "lib/locales/$l.json");
63 if ($trans_j === FALSE)
64 return $text;
65
66 /* Decode JSON. */
67 $trans = json_decode ($trans_j, true);
68 if ($trans === NULL)
69 return $text;
70
71 /* Try to find translation. */
72 if (!array_key_exists ($text, $trans))
73 return $text;
74
75 return $trans[$text];
76 }
77
78 function json_lang_generator ()
79 {
80 $cfg = $GLOBALS['cfg'];
81 $languages_list = $GLOBALS['languages_list'];
82
83 /* Detect user's langage if we are in automatic mode. */
84 if (strcmp ($cfg['lang'], 'auto') == 0)
85 {
86 if (isset ($_SERVER['HTTP_ACCEPT_LANGUAGE']))
87 $l = substr ($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
88 else
89 $l = "en";
90 }
91 else
92 $l = $cfg['lang'];
93
94 /* Is the langage in the list ? */
95 $found = false;
96 foreach ($languages_list as $key => $v)
97 if (strcmp ($l, $key) == 0)
98 $found = true;
99
100 /* Don't translate english. */
101 if (!($found && strcmp ($l, "en")))
102 return "{}";
103
104 /* Open translation file. */
105 $trans_j = file_get_contents (JIRAFEAU_ROOT . "lib/locales/$l.json");
106 return $trans_j;
107 }
108
109 ?>

patrick-canterino.de