]> git.p6c8.net - jirafeau.git/blob - lib/lang.php
[FEATURE] Enhance Readme
[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 '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 {
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 /* 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 /* Don't translate english. */
60 if (!($found && strcmp ($l, "en")))
61 return $text;
62
63 /* Open translation file. */
64 $trans_j = file_get_contents (JIRAFEAU_ROOT . "lib/locales/$l.json");
65 if ($trans_j === FALSE)
66 return $text;
67
68 /* Decode JSON. */
69 $trans = json_decode ($trans_j, true);
70 if ($trans === NULL)
71 return $text;
72
73 /* Try to find translation. */
74 if (!array_key_exists ($text, $trans))
75 return $text;
76
77 return $trans[$text];
78 }
79
80 function json_lang_generator ()
81 {
82 $cfg = $GLOBALS['cfg'];
83 $languages_list = $GLOBALS['languages_list'];
84
85 /* Detect user's langage if we are in automatic mode. */
86 if (strcmp ($cfg['lang'], 'auto') == 0)
87 {
88 if (isset ($_SERVER['HTTP_ACCEPT_LANGUAGE']))
89 $l = substr ($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
90 else
91 $l = "en";
92 }
93 else
94 $l = $cfg['lang'];
95
96 /* Is the langage in the list ? */
97 $found = false;
98 foreach ($languages_list as $key => $v)
99 if (strcmp ($l, $key) == 0)
100 $found = true;
101
102 /* Don't translate english. */
103 if (!($found && strcmp ($l, "en")))
104 return "{}";
105
106 /* Open translation file. */
107 $trans_j = file_get_contents (JIRAFEAU_ROOT . "lib/locales/$l.json");
108 return $trans_j;
109 }
110
111 ?>

patrick-canterino.de