Hi Chris!!
For the moment I have created a function that is called from _config.php to select the locale.
static function set_global_locale($locale) {
i18n::set_default_locale($locale);
i18n::set_locale($locale);
Cookie::set('senda_lang', $locale, 365);
}
static function get_choose_locale($langsAvailable = array()) {
$locale = self::default_locale();
if((isset($_GET['locale']) && !$langsAvailable) || (isset($_GET['locale']) && in_array($_GET['locale'], $langsAvailable))) {
// get from GET parameter
$locale = $_GET['locale'];
} else {
// Si es la homepage del locale por defecto o es una página de tipo multilenguaje
if (Director::baseURL() == $_REQUEST["url"] || self::is_page_multilanguage()) {
// if (!(isset($_SERVER["HTTP_REFERER"]) && (Director::is_site_url($_SERVER["HTTP_REFERER"])))) {
$locale = Cookie::get('senda_lang');
if (!isset($locale)) {
$uid = Cookie::get('senda_mem');
if (isset($uid) && ($uid != 0)) {
$member = Member::get_by_id("Member", $uid);
if ($member) {
$locale = $member->locale;
}
}
if (!isset($locale) || !(in_array($locale, $langsAvailable))) {
$locale = self::default_locale();
// First try to detect browser preferred language
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$locales = array();
$list = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($list as $part) {
$priority = '';
@list($_locale, $priority) = explode(';', $part);
if ($priority) {
$priority = substr($priority, 2);
} else {
$priority = 1.0;
}
if (preg_match('/^([a-zA-Z]+)$/', $_locale, $match)) {
if (isset(i18n::$likely_subtags[$match[1]])) {
$_locale = i18n::$likely_subtags[$match[1]];
} else {
$_locale = self::default_locale();
}
} else if (preg_match('/^([a-zA-Z]+)-([a-zA-Z]+)$/', $_locale, $match)) {
$_locale = $match[1] . '_' . strtoupper($match[2]);
} else {
$_locale = self::default_locale();
}
if (in_array($_locale, $locales) == false) {
$locales[$priority] = $_locale;
}
}
// Sort browser locales by priority
krsort($locales);
// Try to find best matching browser locale
foreach($locales as $_locale) {
if ($langsAvailable) {
if (isset($langsAvailable) && in_array($_locale, $langsAvailable)) {
$locale = $_locale;
break;
}
} else if (self::$allowed_locales) {
if (in_array($_locale, self::$allowed_locales)) {
$locale = $_locale;
break;
}
} else {
if (isset(i18n::$all_locales[$_locale])) {
$locale = $_locale;
break;
}
}
}
}
}
}
// }
}
}
self::set_current_locale($locale);
return self::$current_locale;
}
In _config.php
Translatable::set_allowed_locales(array(
'en_US', // US English
'es_ES' // Español
));
TranslatableExtension::set_pages_multilanguage(array(
'/Security/login',
'/Security/lostpassword',
'/Security/passwordsent'
));
$cu_locale = TranslatableExtension::get_choose_locale(array(
'en_US',
'es_ES'
));
TranslatableExtension::set_global_locale($cu_locale);
I call it from _config.php because I have seen problems if the locale is specified elsewhere.
There are problems with automated forms :
'/Security/login',
'/Security/lostpassword',
'/Security/passwordsent'
It's very interesting your translatable-domains module.
I am going to do tests with your code to see if a better implementation could be done.
If you do other inquiries tell me.
Thanks for everything and good weekend. :)
Regards,
Jose