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

patrick-canterino.de