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

patrick-canterino.de