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

patrick-canterino.de