]> git.p6c8.net - jirafeau.git/blob - lib/lang.php
lib/lang: move translations to json based files
[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 <http://www.gnu.org/licenses/>.
18 */
19
20 global $languages_list;
21 $languages_list = array ('auto' => 'Automatic',
22 'en' => 'English',
23 'fr' => 'Français');
24
25 /* Translation */
26 function t ($text)
27 {
28 $cfg = $GLOBALS['cfg'];
29 $languages_list = $GLOBALS['languages_list'];
30
31 /* Detect user's langage if we are in automatic mode. */
32 if (strcmp ($cfg['lang'], 'auto') == 0)
33 $l = substr ($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
34 else
35 $l = $cfg['lang'];
36
37 /* Is the langage in the list ? */
38 $found = false;
39 foreach ($languages_list as $key => $v)
40 if (strcmp ($l, $key) == 0)
41 $found = true;
42
43 /* Don't translate english. */
44 if (!($found && strcmp ($l, "en")))
45 return $text;
46
47 /* Open translation file. */
48 $trans_j = file_get_contents (JIRAFEAU_ROOT . "lib/locales/$l.json");
49 if ($trans_j === FALSE)
50 return $text;
51
52 /* Decode JSON. */
53 $trans = json_decode ($trans_j, true);
54 error_log(print_r($trans, true));
55 if ($trans === NULL)
56 return $text;
57
58 /* Try to find translation. */
59 $translation = $trans[$text];
60 if (empty ($translation))
61 return $text;
62
63 return $translation;
64 }
65
66 ?>

patrick-canterino.de