SilverCart Forum

We moderate this Forum and we're here to help. Have you already run a forum search to check if your problem has already been solved?

You can help us helping you by providing detailed error messages, screenshots and logfile entries.

SebastianRamon

Page: 1
Topic Automatic language selection 5472 Views

Automatic language selection

22 March 2012 at 7:41pm

Hi!

It would be interesting to include the functionality of automatic language selection depending on the browser, the client's language or a cookie.
This functionality could be enabled or disabled from the configuration of SilverCart.
Below I include a snippet of example:

static function get_choose_locale() {
$langsAvailable = Translatable::get_allowed_locales();
$locale = self::get_locale_from_get();
if (!isset($locale)) {
$locale = self::default_locale();
// 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;
}

Thanks,
Regards,
Jose

Re: Automatic language selection

23 March 2012 at 8:58am

Hi Josua,

we will consider your idea when we finish the development of the multilingual feature. Thank you!