]>
git.p6c8.net - jirafeau_project.git/blob - lib/lang.php
3 * Jirafeau, your web file repository
4 * Copyright (C) 2015 Jerome Jutteau <j.jutteau@gmail.com>
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.
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.
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/>.
20 function t($string_id)
22 $lang_config = $GLOBALS['cfg']['lang'];
23 if ($lang_config != "auto") {
24 $r = t_in($string_id, $lang_config);
25 if ($r === false ||
$r === "") {
31 $visitor_langs = t_visitor_langs();
32 foreach ($visitor_langs as $lang) {
33 $r = t_in($string_id, $lang);
34 if ($r === false ||
$r === "") {
43 function t_visitor_langs() {
44 $visitor_langs = t_parse_accept_language();
45 array_push($visitor_langs, "en");
46 return $visitor_langs;
49 function t_parse_accept_language() {
50 if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
53 // Example: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5
55 $cols = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
56 foreach ($cols as $i => $semicols) {
57 $lang = explode(';', $semicols);
58 if (count($lang) === 0) {
61 array_push($langs, $lang[0]);
66 function t_in($string_id, $lang) {
67 $trans = t_get_json($lang);
68 if ($trans === false) {
71 if (!array_key_exists($string_id, $trans)) {
74 return $trans[$string_id];
77 function t_get_raw_json($lang) {
78 $filename = str_replace("-", "_", $lang);
79 if (preg_match('/[^A-Za-z_\w]/', $filename)) {
82 $p = JIRAFEAU_ROOT
. "lib/locales/$filename.json";
83 if (!file_exists($p)) {
86 $json = file_get_contents($p);
87 if ($json === false) {
93 function t_get_json($lang) {
94 $raw_j = t_get_raw_json($lang);
95 $array = json_decode($raw_j, true);
96 if ($array === null) {
102 function json_lang_generator($lang) {
104 if ($lang === null) {
105 $lang_config = $GLOBALS['cfg']['lang'];
106 if ($lang_config != "auto") {
107 $r = t_get_raw_json($lang_config);
109 foreach(t_visitor_langs() as $lang) {
110 $r = t_get_raw_json($lang);
111 if (!($r === false)) {
117 $r = t_get_raw_json($lang);
119 return $r === false ?
"{}" : $r;
patrick-canterino.de