An error occurred while loading the file. Please try again.
-
Côme Chilliet authored
Language::isAvailable now also work if you pass a lang without charset. This fixes the language field of configuration screen. issue #6051
Unverifiedcfb5c8fe
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2017-2018 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*!
* \file class_Language.inc
* Source code for the class Language
*/
/*!
* \brief This class contains all the function needed to manage languages
*/
class Language
{
/*!
* \brief Initialize language configuration
*
* \param string $lang Language locale to use, defaults to self::detect()
*/
public static function init ($lang = NULL)
{
global $BASE_DIR;
if ($lang === NULL) {
$lang = static::detect();
}
list ($language, , $char) = parse_gettext_lang($lang);
putenv('LANGUAGE=');
putenv("LANG=$lang");
$langset = setlocale(LC_ALL, $lang, $language.'.'.$char);
if ($langset === FALSE) {
trigger_error('Setting locale to '.$lang.' failed');
} elseif ($langset != $lang) {
trigger_error('Setting locale to '.$lang.' failed, fell back to '.$langset);
}
$GLOBALS['t_language'] = $lang;
$GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
static::setHeaders($lang, 'text/html');
/* Set the text domain as 'fusiondirectory' */
$domain = 'fusiondirectory';
bindtextdomain($domain, LOCALE_DIR);
textdomain($domain);
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
logging::debug(DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, 'Setting language to');
}
$ret = FALSE;
/* Reset plist cache if language changed */
if ((!session::is_set('lang')) || (session::get('lang') != $lang)) {
$ret = TRUE;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
if (session::is_set('plist')) {
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
logging::debug(DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, session::get('lang'), 'Plist already loaded with language');
}
session::un_set('plist');
session::set('lang', $lang);
pluglist::load();
}
}
session::set('lang', $lang);
return $ret;
}
/*!
* \brief Determine which language to show to the user
*
* Determines which language should be used to present fusiondirectory content
* to the user. It does so by looking at several possibilites and returning
* the first setting that can be found.
*
* -# Language configured by the user
* -# Global configured language
* -# Language as returned by al2gt (as configured in the browser)
*
* \return string gettext locale string
*/
public static function detect ()
{
global $config;
/* Try to use users primary language */
$ui = get_userinfo();
if (isset($ui) && ($ui !== NULL) && ($ui->language != '')) {
return $ui->language.'.UTF-8';
}
/* Check for global language settings in configuration */
if (isset($config) && ($config->get_cfg_value('language') != '')) {
$lang = $config->get_cfg_value('language');
if (!preg_match('/utf/i', $lang)) {
$lang .= '.UTF-8';
}
return $lang;
}
/* Load supported languages */
$languages = static::getList();
/* Move supported languages to flat list */
$langs = [];
foreach (array_keys($languages) as $lang) {
$langs[] = $lang.'.UTF-8';
}
/* Return gettext based string */
return al2gt($langs);
}
/*!
* \brief Checks if a locale is available
*
* \param string $lang Language locale to use
*/
public static function isAvailable (string $lang): bool
{
if (strpos($lang, '.') === FALSE) {
$lang = $lang.'.UTF-8';
}
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
/* Store current locale */
$locale = setlocale(LC_ALL, 0);
/* Try to switch */
list ($language, , $char) = parse_gettext_lang($lang);
$langset = setlocale(LC_ALL, $lang, $language.'.'.$char);
/* Set current locale back */
setlocale(LC_ALL, $locale);
return ($langset !== FALSE);
}
/*!
* \brief Get the language for the user connecting
*
* \param boolean $ownLanguage Should language names be stated in their own language as well
*/
public static function getList ($ownLanguage = FALSE)
{
/* locales in english */
$tmp_english = [
'en_US' => 'English',
'af_ZA' => 'Afrikaans',
'ar_EG' => 'Arabic',
'ca_ES' => 'Catalan',
'cs_CZ' => 'Czech',
'de_DE' => 'German',
'el_GR' => 'Greek',
'es_CO' => 'Colombian Spanish',
'es_ES' => 'Spanish',
'es_VE' => 'Venezuelan',
'fa_IR' => 'Persian',
'fi_FI' => 'Finnish',
'fr_FR' => 'French',
'hu_HU' => 'Hungarian',
'id_ID' => 'Indonesian',
'it_IT' => 'Italian',
'ja_JP' => 'Japanese',
'ko_KR' => 'Korean',
'lv_LV' => 'Latvian',
'nb_NO' => 'Norwegian Bokmål',
'nl_NL' => 'Dutch',
'pl_PL' => 'Polish',
'pt_BR' => 'Brazilian',
'pt_PT' => 'Portuguese',
'ru_RU' => 'Russian',
'sv_SE' => 'Swedish',
'tr_TR' => 'Turkish',
'vi_VN' => 'Vietnamese',
'zh_TW' => 'Taiwanese',
'zh_CN' => 'Chinese',
];
$ret = [];
if ($ownLanguage) {
/* locales in their own language */
$tmp_ownlang = [
'en_US' => 'English',
'ar_EG' => 'عربية',
'af_ZA' => 'Afrikaans',
'ca_ES' => 'Català',
'cs_CZ' => 'Česky',
'de_DE' => 'Deutsch',
'el_GR' => 'ελληνικά',
'es_CO' => 'Español Colombiano',
'es_ES' => 'Español',
'es_VE' => 'Castellano',
'fa_IR' => 'پارسی',
'fi_FI' => 'Suomi',
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
'fr_FR' => 'Français',
'hu_HU' => 'Magyar',
'id_ID' => 'Bahasa Indonesia',
'it_IT' => 'Italiano',
'ja_JP' => '日本語',
'ko_KR' => '한국어',
'lv_LV' => 'Latviešu valoda',
'nb_NO' => 'Norsk bokmål',
'nl_NL' => 'Nederlands',
'pl_PL' => 'Polski',
'pt_BR' => 'Português (Brasil)',
'pt_PT' => 'Português',
'ru_RU' => 'русский язык',
'sv_SE' => 'Svenska',
'tr_TR' => 'Türkçe',
'vi_VN' => 'Tiếng Việt',
'zh_TW' => 'Taiwanese',
'zh_CN' => '中文, 汉语, 漢語',
];
foreach ($tmp_english as $key => $name) {
$ret[$key] = _($name).' ('.$tmp_ownlang[$key].')';
}
} else {
foreach ($tmp_english as $key => $name) {
$ret[$key] = _($name);
}
}
return $ret;
}
/*!
* \brief Returns TRUE if $lang is a right to left language
*/
public static function isRTL ($lang)
{
return preg_match('/^(fa_|ar_)/', $lang);
}
public static function setHeaders ($language, $mime)
{
list ($lang, $country, $char) = parse_gettext_lang($language);
if (!headers_sent()) {
header("Content-Language: $lang".(empty($country) ? '' : "-$country"));
if (!empty($char) && preg_match('|^text/|', $mime)) {
header("Content-Type: $mime; charset=$char");
} else {
header("Content-Type: $mime");
}
} else {
trigger_error('Could not set language '.$lang.' header, headers already sent');
}
}
}