FusionDirectory
class_Language.inc
Go to the documentation of this file.
1 <?php
2 
3 /*
4  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
5  Copyright (C) 2017-2018 FusionDirectory
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
30 class Language
31 {
37  public static function init ($lang = NULL)
38  {
39  global $BASE_DIR;
40 
41  if ($lang === NULL) {
42  $lang = static::detect();
43  }
44 
45  list ($language, , $char) = parse_gettext_lang($lang);
46  putenv('LANGUAGE=');
47  putenv("LANG=$lang");
48  $langset = setlocale(LC_ALL, $lang, $language.'.'.$char);
49  if ($langset === FALSE) {
50  trigger_error('Setting locale to '.$lang.' failed');
51  } elseif ($langset != $lang) {
52  trigger_error('Setting locale to '.$lang.' failed, fell back to '.$langset);
53  }
54  $GLOBALS['t_language'] = $lang;
55  $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
56  static::setHeaders($lang, 'text/html');
57 
58  /* Set the text domain as 'fusiondirectory' */
59  $domain = 'fusiondirectory';
60  bindtextdomain($domain, LOCALE_DIR);
61  textdomain($domain);
62  if ($_SERVER['REQUEST_METHOD'] != 'POST') {
63  logging::debug(DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, 'Setting language to');
64  }
65 
66  $ret = FALSE;
67 
68  /* Reset plist cache if language changed */
69  if ((!session::is_set('lang')) || (session::get('lang') != $lang)) {
70  $ret = TRUE;
71  if (session::is_set('plist')) {
72  if ($_SERVER['REQUEST_METHOD'] != 'POST') {
73  logging::debug(DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, session::get('lang'), 'Plist already loaded with language');
74  }
75  session::un_set('plist');
76  session::set('lang', $lang);
78  }
79  }
80 
81  session::set('lang', $lang);
82  return $ret;
83  }
84 
98  public static function detect ()
99  {
100  global $config;
101 
102  /* Try to use users primary language */
103  $ui = get_userinfo();
104  if (isset($ui) && ($ui !== NULL) && ($ui->language != '')) {
105  return $ui->language.'.UTF-8';
106  }
107 
108  /* Check for global language settings in configuration */
109  if (isset($config) && ($config->get_cfg_value('language') != '')) {
110  $lang = $config->get_cfg_value('language');
111  if (!preg_match('/utf/i', $lang)) {
112  $lang .= '.UTF-8';
113  }
114  return $lang;
115  }
116 
117  /* Load supported languages */
118  $languages = static::getList();
119 
120  /* Move supported languages to flat list */
121  $langs = [];
122  foreach (array_keys($languages) as $lang) {
123  $langs[] = $lang.'.UTF-8';
124  }
125 
126  /* Return gettext based string */
127  return al2gt($langs);
128  }
129 
135  public static function isAvailable (string $lang): bool
136  {
137  if (strpos($lang, '.') === FALSE) {
138  $lang = $lang.'.UTF-8';
139  }
140 
141  /* Store current locale */
142  $locale = setlocale(LC_ALL, 0);
143 
144  /* Try to switch */
145  list ($language, , $char) = parse_gettext_lang($lang);
146  $langset = setlocale(LC_ALL, $lang, $language.'.'.$char);
147 
148  /* Set current locale back */
149  setlocale(LC_ALL, $locale);
150 
151  return ($langset !== FALSE);
152  }
153 
159  public static function getList ($ownLanguage = FALSE)
160  {
161  /* locales in english */
162  $tmp_english = [
163  'en_US' => 'English',
164  'af_ZA' => 'Afrikaans',
165  'ar_EG' => 'Arabic',
166  'ca_ES' => 'Catalan',
167  'cs_CZ' => 'Czech',
168  'de_DE' => 'German',
169  'el_GR' => 'Greek',
170  'es_CO' => 'Colombian Spanish',
171  'es_ES' => 'Spanish',
172  'es_VE' => 'Venezuelan',
173  'fa_IR' => 'Persian',
174  'fi_FI' => 'Finnish',
175  'fr_FR' => 'French',
176  'hu_HU' => 'Hungarian',
177  'id_ID' => 'Indonesian',
178  'it_IT' => 'Italian',
179  'ja_JP' => 'Japanese',
180  'ko_KR' => 'Korean',
181  'lv_LV' => 'Latvian',
182  'nb_NO' => 'Norwegian Bokmål',
183  'nl_NL' => 'Dutch',
184  'pl_PL' => 'Polish',
185  'pt_BR' => 'Brazilian',
186  'pt_PT' => 'Portuguese',
187  'ru_RU' => 'Russian',
188  'sv_SE' => 'Swedish',
189  'tr_TR' => 'Turkish',
190  'vi_VN' => 'Vietnamese',
191  'zh_TW' => 'Taiwanese',
192  'zh_CN' => 'Chinese',
193  ];
194 
195  $ret = [];
196  if ($ownLanguage) {
197  /* locales in their own language */
198  $tmp_ownlang = [
199  'en_US' => 'English',
200  'ar_EG' => 'عربية',
201  'af_ZA' => 'Afrikaans',
202  'ca_ES' => 'Català',
203  'cs_CZ' => 'Česky',
204  'de_DE' => 'Deutsch',
205  'el_GR' => 'ελληνικά',
206  'es_CO' => 'Español Colombiano',
207  'es_ES' => 'Español',
208  'es_VE' => 'Castellano',
209  'fa_IR' => 'پارسی',
210  'fi_FI' => 'Suomi',
211  'fr_FR' => 'Français',
212  'hu_HU' => 'Magyar',
213  'id_ID' => 'Bahasa Indonesia',
214  'it_IT' => 'Italiano',
215  'ja_JP' => '日本語',
216  'ko_KR' => '한국어',
217  'lv_LV' => 'Latviešu valoda',
218  'nb_NO' => 'Norsk bokmål',
219  'nl_NL' => 'Nederlands',
220  'pl_PL' => 'Polski',
221  'pt_BR' => 'Português (Brasil)',
222  'pt_PT' => 'Português',
223  'ru_RU' => 'русский язык',
224  'sv_SE' => 'Svenska',
225  'tr_TR' => 'Türkçe',
226  'vi_VN' => 'Tiếng Việt',
227  'zh_TW' => 'Taiwanese',
228  'zh_CN' => '中文, 汉语, 漢語',
229  ];
230 
231  foreach ($tmp_english as $key => $name) {
232  $ret[$key] = _($name).' ('.$tmp_ownlang[$key].')';
233  }
234  } else {
235  foreach ($tmp_english as $key => $name) {
236  $ret[$key] = _($name);
237  }
238  }
239 
240  return $ret;
241  }
242 
246  public static function isRTL ($lang)
247  {
248  return preg_match('/^(fa_|ar_)/', $lang);
249  }
250 
251  public static function setHeaders ($language, $mime)
252  {
253  list ($lang, $country, $char) = parse_gettext_lang($language);
254 
255  if (!headers_sent()) {
256  header("Content-Language: $lang".(empty($country) ? '' : "-$country"));
257  if (!empty($char) && preg_match('|^text/|', $mime)) {
258  header("Content-Type: $mime; charset=$char");
259  } else {
260  header("Content-Type: $mime");
261  }
262  } else {
263  trigger_error('Could not set language '.$lang.' header, headers already sent');
264  }
265  }
266 }
static detect()
Determine which language to show to the user.
static load()
Loads plist and load it in config object.
static get($name)
Accessor of a session var.
& get_userinfo()
Return the current userinfo object.
Definition: functions.inc:312
static getList($ownLanguage=FALSE)
Get the language for the user connecting.
static set($name, $value)
Set a value in a session.
static init($lang=NULL)
Initialize language configuration.
This class contains all the function needed to manage languages.
static debug(int $level, int $line, string $function, string $file, $data, string $info='')
Debug output method.
static isAvailable(string $lang)
Checks if a locale is available.
static un_set($name)
Unset a session.
const LOCALE_DIR
Global locale cache dir.
Definition: variables.inc:60
static isRTL($lang)
Returns TRUE if $lang is a right to left language.
static is_set($name)
Check if the name of the session is set.