chpw/include/localization.php
2016-09-11 17:48:55 +02:00

115 lines
2.7 KiB
PHP
Executable file

<?php
/* CHANGELOG:
@1.1.1
- sostituita la funzione deprecata split() con explode()
@1.1:
- supporta locale con forma la_LA, la, la_LA.utf-8, la_LA.UTF-8, la.UTF-8, la.utf-8
*/
$version="1.1.1";
$_td_stack = array(); // text domains stack
$langarray = array('it_IT','en_US','es_ES','de_DE','eo_EO');
//$lang = "eo_EO";
If (isSet($_GET["lang"]))
$lang = $_GET["lang"];
else {
$lang = $langarray[0];
/* vedi le preferenze del browser */
$user_langs = explode(",", getenv("HTTP_ACCEPT_LANGUAGE"));
foreach ($user_langs as $user_lang) {
$user_lang_items = explode(";", $user_lang);
if (in_array($user_lang_items[0], $langarray)) {
$lang = $user_lang_items[0];
break;
}
}
}
$charset="UTF-8";
$gettext_domain="messages";
$locale_dir="res/locale";
/**
* Il locale deve essere presente nel sistema usa "locale -a" per
* verificarlo, non ovunque si chiama allo stesso modo e non tutti
* i locali si chiamano allo stesso modo, l'esperanto ad esempio
* non rispetta la nomenclatura classica
*/
putenv("LC_ALL=$lang");
$l=explode("_",$lang);
if(! setlocale(LC_ALL, $lang.".".$charset))
if(! setlocale(LC_ALL, $lang))
if(! setlocale(LC_ALL,$l[0].".".$charset))
setlocale(LC_ALL,$l[0]);
bindtextdomain($gettext_domain, "res/locale");
textdomain($gettext_domain);
bind_textdomain_codeset($gettext_domain, $charset);
function printLangSelector() {
global $lang;
global $langarray;
$targetname = $_SERVER['PHP_SELF'];
$l_arg="lang=";
$newQueryString="";
$queryString= $_SERVER['QUERY_STRING'];
$argS = explode("&",$queryString);
if(strpos($queryString,$l_arg)!==FALSE){
foreach ($argS as $arg){
if(strpos($arg,$l_arg)===FALSE)
$newQueryString.=$arg."&";
}
}
echo "<ul class='langsel'>";
foreach ($langarray as $l) {
$label=split("_",$l);
$class="";
if ($l == $lang)
$class="class='it'";
echo "<li ".$class."><a href='".$targetname."?".$newQueryString.$l_arg.$l."'>".$label[0]."</a></li>";
}
$foo = getenv("HTTP_ACCEPT_LANGUAGE");
echo "</ul>\n";
}
/**
* Sets a new text domain after recording the current one
* so it can be restored later with restore_textdomain().
*
* It's possible to nest calls to these two functions.
* @param string the new text domain to set
*/
function set_textdomain($td)
{
global $_td_stack;
$old_td = textdomain(NULL);
if ($old_td) {
if (!strcmp($old_td, $td))
array_push($_td_stack, false);
else
array_push($_td_stack, $old_td);
}
textdomain($td);
}
/**
* Restore the text domain active before the last call to
* set_textdomain().
*/
function restore_textdomain()
{
global $_td_stack;
$old_td = array_pop($_td_stack);
if ($old_td)
textdomain($old_td);
}
?>