Nouvelle gestion des langues
i18n_setup.php remplace localize.php
This commit is contained in:
97
i18n_setup.php
Normal file
97
i18n_setup.php
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Verifies if the given $locale is supported in the project
|
||||||
|
* @param string $locale
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
|
||||||
|
function list_locale_server($dir) {
|
||||||
|
$lange = array();
|
||||||
|
$iterator = new DirectoryIterator($dir);
|
||||||
|
foreach ($iterator as $ff) {
|
||||||
|
if ($ff->isDot() OR $ff->isFile()) { continue; }
|
||||||
|
if ($ff->isDir()) {
|
||||||
|
$d = $ff->getFilename();
|
||||||
|
if (($d != "nocache") AND (strpos($d, "_"))) {
|
||||||
|
$e = strtok($d, "_");
|
||||||
|
$lange[] = $d;
|
||||||
|
$lange[] = $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//print_r($lange);
|
||||||
|
return $lange;
|
||||||
|
}
|
||||||
|
|
||||||
|
function valid($locale) {
|
||||||
|
//return in_array($locale, ['en_US', 'en', 'fr_FR', 'fr', 'de_DE', 'de', 'es_ES', 'es']);
|
||||||
|
global $dir_locales;
|
||||||
|
$l = list_locale_server($dir_locales);
|
||||||
|
return in_array($locale, $l);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------*/
|
||||||
|
/* Settings */
|
||||||
|
/* ------------------------------------*/
|
||||||
|
|
||||||
|
$dir_locales = __DIR__ . '/Locale';
|
||||||
|
|
||||||
|
//setting the source/default locale, for informational purposes
|
||||||
|
$lang = 'en_US';
|
||||||
|
|
||||||
|
/*
|
||||||
|
if (isset($_GET['lang']) && valid($_GET['lang'])) {
|
||||||
|
// the locale can be changed through the query-string
|
||||||
|
$lang = $_GET['lang']; //you should sanitize this!
|
||||||
|
//setcookie('lang', $lang); //it's stored in a cookie so it can be reused
|
||||||
|
setcookie('lang', $lang, time() + (86400 * 30), "/"); //it's stored in a cookie so it can be reused
|
||||||
|
*/
|
||||||
|
if (isset($_POST['lang']) && valid($_POST['lang'])) {
|
||||||
|
// the locale can be changed through the query-string
|
||||||
|
$lang = $_POST['lang']; //you should sanitize this!
|
||||||
|
//setcookie('lang', $lang); //it's stored in a cookie so it can be reused
|
||||||
|
setcookie('lang', $lang, time() + (86400 * 30), "/"); //it's stored in a cookie so it can be reused
|
||||||
|
|
||||||
|
} elseif (isset($_SESSION['language']) && valid($_SESSION['language'])) {
|
||||||
|
// if the cookie is present instead, let's just keep it
|
||||||
|
$lang = $_SESSION['language']; //you should sanitize this!
|
||||||
|
|
||||||
|
} elseif (isset($_COOKIE['lang']) && valid($_COOKIE['lang'])) {
|
||||||
|
// if the cookie is present instead, let's just keep it
|
||||||
|
$lang = $_COOKIE['lang']; //you should sanitize this!
|
||||||
|
|
||||||
|
} elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
|
||||||
|
// default: look for the languages the browser says the user accepts
|
||||||
|
$langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
|
||||||
|
array_walk($langs, function (&$lang) { $lang = strtr(strtok($lang, ';'), ['-' => '_']); });
|
||||||
|
foreach ($langs as $browser_lang) {
|
||||||
|
if (valid($browser_lang)) {
|
||||||
|
$lang = $browser_lang;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// here we define the global system locale given the found language
|
||||||
|
putenv("LANG=$lang");
|
||||||
|
|
||||||
|
// this might be useful for date functions (LC_TIME) or money formatting (LC_MONETARY), for instance
|
||||||
|
setlocale(LC_ALL, $lang);
|
||||||
|
|
||||||
|
// this will make Gettext look for ../locales/<lang>/LC_MESSAGES/main.mo
|
||||||
|
bindtextdomain('sentier', __DIR__ . '/Locale/');
|
||||||
|
|
||||||
|
// indicates in what encoding the file should be read
|
||||||
|
bind_textdomain_codeset('sentier', 'UTF-8');
|
||||||
|
|
||||||
|
// if your application has additional domains, as cited before, you should bind them here as well
|
||||||
|
//bindtextdomain('forum', '../locales');
|
||||||
|
//bind_textdomain_codeset('forum', 'UTF-8');
|
||||||
|
|
||||||
|
// here we indicate the default domain the gettext() calls will respond to
|
||||||
|
textdomain('sentier');
|
||||||
|
|
||||||
|
// this would look for the string in forum.mo instead of main.mo
|
||||||
|
// echo dgettext('forum', 'Welcome back!');
|
||||||
|
?>
|
||||||
Reference in New Issue
Block a user