Private GIT

Skip to content
Snippets Groups Projects
Unverified Commit 61674a3b authored by Gary Allan's avatar Gary Allan
Browse files

i18n: Update set_ui_language()

Set phpIPAM UI locale in order of preference
  1) $_SESSION['ipamlanguage']
  2) Administration -> phpIPAM settings -> Default language
  3) LC_ALL environment
  4) HTTP_ACCEPT_LANGUAGE header

Set LANGUAGE environment for Ubuntu.
parent 2677ffa1
Branches
Tags
No related merge requests found
...@@ -115,15 +115,10 @@ if(@$config['requests_public']===false) { ...@@ -115,15 +115,10 @@ if(@$config['requests_public']===false) {
<?php <?php
# set default language # set default language
if(isset($User->settings->defaultLang) && !is_null($User->settings->defaultLang) ) { if(isset($User->settings->defaultLang) && !is_null($User->settings->defaultLang) ) {
# get language # get global default language
$lang = $User->get_default_lang(); $lang = $User->get_default_lang();
if (is_object($lang))
if (is_object($lang)) { set_ui_language($lang->l_code);
putenv("LC_ALL=".$lang->l_code);
setlocale(LC_ALL, $lang->l_code); // set language
}
bindtextdomain("phpipam", "./functions/locale"); // Specify location of translation tables
textdomain("phpipam"); // Choose domain
} }
?> ?>
......
...@@ -303,7 +303,7 @@ class User extends Common_functions { ...@@ -303,7 +303,7 @@ class User extends Common_functions {
$this->reset_inactivity_time(); $this->reset_inactivity_time();
$this->update_activity_time (); $this->update_activity_time ();
# bind language # bind language
$this->set_ui_language(); set_ui_language();
} }
} }
} }
...@@ -460,21 +460,6 @@ class User extends Common_functions { ...@@ -460,21 +460,6 @@ class User extends Common_functions {
setcookie("phpipamredirect", preg_replace('/^\/+/', '/', $_SERVER['REQUEST_URI']), time()+10, "/", null, null, true); setcookie("phpipamredirect", preg_replace('/^\/+/', '/', $_SERVER['REQUEST_URI']), time()+10, "/", null, null, true);
} }
/**
* Sets translation for logged in user
*
* @access private
* @return void
*/
private function set_ui_language () {
if(strlen($_SESSION['ipamlanguage'])>0) {
putenv("LC_ALL=$_SESSION[ipamlanguage]");
bindtextdomain("phpipam", dirname(__FILE__)."/../locale"); // Specify location of translation tables
setlocale(LC_ALL, $_SESSION['ipamlanguage']); // set language
textdomain("phpipam"); // Choose domain
}
}
/** /**
* Checks if system is in maintaneance mode and exits if it is * Checks if system is in maintaneance mode and exits if it is
* *
......
...@@ -28,6 +28,9 @@ if (Config::ValueOf('proxy_enabled') == true) { ...@@ -28,6 +28,9 @@ if (Config::ValueOf('proxy_enabled') == true) {
/* global and missing functions */ /* global and missing functions */
require('global_functions.php'); require('global_functions.php');
/* Set UI language */
set_ui_language();
/* @http only cookies ------------------- */ /* @http only cookies ------------------- */
if(php_sapi_name()!="cli") if(php_sapi_name()!="cli")
ini_set('session.cookie_httponly', 1); ini_set('session.cookie_httponly', 1);
......
...@@ -138,3 +138,48 @@ function php_feature_missing($required_extensions = null, $required_functions = ...@@ -138,3 +138,48 @@ function php_feature_missing($required_extensions = null, $required_functions =
return false; return false;
} }
/**
* Set phpIPAM UI locale in order of preference
* 1) $_SESSION['ipamlanguage']
* 2) Administration -> phpIPAM settings -> Default language
* 3) LC_ALL environment
* 4) HTTP_ACCEPT_LANGUAGE header
*/
function set_ui_language($default_lang = null) {
if (php_feature_missing(["gettext", "pcre"]))
return;
$application_langs = [$_SESSION['ipamlanguage'], $default_lang, getenv("LC_ALL")];
$http_accept_langs = preg_replace("/;.*$/", "", explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']));
// Try each langage in order of preference
$langs = array_merge($application_langs, $http_accept_langs);
foreach($langs as $lang) {
if (!is_string($lang) || strlen($lang)==0)
continue;
if (!file_exists(dirname(__FILE__)."/locale/$lang/LC_MESSAGES/phpipam.mo"))
continue;
putenv("LC_ALL=".$lang);
// https://help.ubuntu.com/community/EnvironmentVariables
// Unlike "LANG" and "LC_*", "LANGUAGE" should not be assigned a complete locale name including the encoding part (e.g. ".UTF-8").
putenv("LANG=".$lang);
putenv("LANGUAGE=".preg_replace("/\.utf-?8/i", "", $lang));
setlocale(LC_ALL, $lang);
bind_textdomain_codeset('phpipam', 'UTF-8');
bindtextdomain("phpipam", dirname(__FILE__)."/locale");
textdomain("phpipam");
return true;
}
return false;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment