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

patrick-canterino.de