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

patrick-canterino.de