Post by kite » Fri Nov 08, 2013 6:59 pm

Automatic laguage detection in 1.5.6 works only for the first language configured in the browser. The reason for that is that the browser attaches the "quality factor" to the additional languages, like this "en-us;q=0.5"

Here's the proposed fix of the appropriate section in /index.php:

Code: Select all

if (isset($request->server['HTTP_ACCEPT_LANGUAGE']) && $request->server['HTTP_ACCEPT_LANGUAGE']) { 
	$browser_languages = explode(',', $request->server['HTTP_ACCEPT_LANGUAGE']);
	
	foreach ($browser_languages as $browser_language) {
		$pos = strpos($browser_language, ';');
		if ($pos === false) {
			$bl = $browser_language;
		} else {
			$bl = substr($browser_language, 0, $pos );
		}
		foreach ($languages as $key => $value) {
			if ($value['status']) {
				$locale = explode(',', $value['locale']);

				if (in_array($bl, $locale)) {
					$detect = $key;
					break 2;
				}
			}
		}
	}
}

Newbie

Posts

Joined
Fri Nov 08, 2013 1:03 am

Post by Qphoria » Fri Nov 08, 2013 9:17 pm

So to clarify.. what you are saying is..

If a site offers
English (default)
Spanish
German

and your browser has
Chinese
German

Are you saying it won't switch to German because Chinese is not found and it will default to the site default language?

But with your fix, it will choose German?

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by pablofiumara » Sun Nov 10, 2013 10:58 am

This is called relative quality factor. For more information, visit: http://stackoverflow.com/questions/8552 ... tp-headers

User avatar
New member

Posts

Joined
Sun Nov 10, 2013 6:29 am


Post by kite » Fri Nov 15, 2013 5:38 pm

Qphoria wrote:Are you saying it won't switch to German because Chinese is not found and it will default to the site default language?

But with your fix, it will choose German?
That's exactly what I'm saying.

Newbie

Posts

Joined
Fri Nov 08, 2013 1:03 am

Post by markmarkus » Wed May 14, 2014 5:06 am

It doesn't seem to be working for me on 1.5.6

I have just added the code but the language stays in Spanish even if the browser is set only in English.

I can switch manually to English and it works fine.

What could be wrong?

Code: Select all

// Language Detection
$languages = array();

$query = $db->query("SELECT * FROM `" . DB_PREFIX . "language` WHERE status = '1'"); 

foreach ($query->rows as $result) {
	$languages[$result['code']] = $result;
}

$detect = '';

if (isset($request->server['HTTP_ACCEPT_LANGUAGE']) && $request->server['HTTP_ACCEPT_LANGUAGE']) { 
   $browser_languages = explode(',', $request->server['HTTP_ACCEPT_LANGUAGE']);
   
   foreach ($browser_languages as $browser_language) {
      $pos = strpos($browser_language, ';');
      if ($pos === false) {
         $bl = $browser_language;
      } else {
         $bl = substr($browser_language, 0, $pos );
      }
      foreach ($languages as $key => $value) {
         if ($value['status']) {
            $locale = explode(',', $value['locale']);

            if (in_array($bl, $locale)) {
               $detect = $key;
               break 2;
            }
         }
      }
   }
}

if (isset($session->data['language']) && array_key_exists($session->data['language'], $languages) && $languages[$session->data['language']]['status']) {
	$code = $session->data['language'];
} elseif (isset($request->cookie['language']) && array_key_exists($request->cookie['language'], $languages) && $languages[$request->cookie['language']]['status']) {
	$code = $request->cookie['language'];
} elseif ($detect) {
	$code = $detect;
} else {
	$code = $config->get('config_language');
}

if (!isset($session->data['language']) || $session->data['language'] != $code) {
	$session->data['language'] = $code;
}

if (!isset($request->cookie['language']) || $request->cookie['language'] != $code) {	  
	setcookie('language', $code, time() + 60 * 60 * 24 * 30, '/', $request->server['HTTP_HOST']);
}			

$config->set('config_language_id', $languages[$code]['language_id']);
$config->set('config_language', $languages[$code]['code']);

// Language	
$language = new Language($languages[$code]['directory']);
$language->load($languages[$code]['filename']);	
$registry->set('language', $language); 

New member

Posts

Joined
Thu Dec 26, 2013 7:51 am

Post by nebesniq » Wed Aug 27, 2014 2:02 pm

It's not working on oc 1.5.6
Can this fix override Geolocation?
Is this better than Geolocation plugins for oc?

Newbie

Posts

Joined
Sun Aug 17, 2014 10:53 pm

Post by markmarkus » Wed Aug 27, 2014 6:21 pm

At the end I bought an extension that autoswitch language and currency based on IP.

The bad thing is that my website has Spanish as default language, and I set OC to change to English and USD for United States, and it works fine, the big problem is that Google is only showing results in English, even if I search my website using and Spanish PC, Spanish browser, Spanish version of google and Spanish version of OC!

I have just disabled English for the US, I hope google shows correct results again soon.

New member

Posts

Joined
Thu Dec 26, 2013 7:51 am
Who is online

Users browsing this forum: No registered users and 12 guests