Jump to content

automatic language detection


Recommended Posts

Hi!

 

Here's my problem : I want that my Prestashop boutique can automaticaly detect client language base on his browser language. By now, if I set default language to english, the home page will always showing in english and, obviously, if set to french it will always shows the french page.

 

I need an automatic language detection base on browser for a simple reason : it's a new website and clients only see the maintenance page by now. So : 1- maintenance page does not offer language selection, 2- I want to use this page to collect email adresses, 3- I can't use language detection base on ip adress cause I'm in Canada and we have two officials languages.

 

I tried to find solution by myself but the only think I found who seem relevant is this tread :

https://github.com/P...taShop/pull/176

And I'm not shure of what they are taking about...

 

I tried to open classes/Tools.php and copy/paste the following code (line 326 to 372) in classes/Cookie.php. That doesn't seem to work. But, I admit it, I don't really know what I'm doing!

 

Thanks for your help!

PS : Sorry if I made a lot of spelling or grammatical mistake in this tread : I'm french talker and english is still a challenge for me... but I'm working hard on it!

 

Here's the code :

 

/**

* Change language in cookie while clicking on a flag

*

* @return string iso code

*/

public static function setCookieLanguage($cookie = null)

{

if (!$cookie)

$cookie = Context::getContext()->cookie;

/* If language does not exist or is disabled, erase it */

if ($cookie->id_lang)

{

//echo $cookie->id_lang;exit;

$lang = new Language((int)$cookie->id_lang);

if (!Validate::isLoadedObject($lang) || !$lang->active || !$lang->isAssociatedToShop())

$cookie->id_lang = null;

 

}

 

/* Automatically detect language if not already defined */

if (!$cookie->id_lang && isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))

{

$array = explode(',', Tools::strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));

if (Tools::strlen($array[0]) > 2)

{

$tab = explode('-', $array[0]);

$string = $tab[0];

}

else

$string = $array[0];

if (Validate::isLanguageIsoCode($string))

{

$lang = new Language(Language::getIdByIso($string));

if (Validate::isLoadedObject($lang) && $lang->active)

$cookie->id_lang = (int)$lang->id;

}

}

 

/* If language file not present, you must use default language file */

if (!$cookie->id_lang || !Validate::isUnsignedId($cookie->id_lang))

$cookie->id_lang = (int)Configuration::get('PS_LANG_DEFAULT');

 

$iso = Language::getIsoById((int)$cookie->id_lang);

@include_once(_PS_THEME_DIR_.'lang/'.$iso.'.php');

 

return $iso;

}

Link to comment
Share on other sites

Thank you piribipipi but I'm not ready to pay for this module because it's not its primary function.

And after a lot of research, I finally found!!!

 

Here's how :

 

Copy those lines (345 to 363) from classes/Tools.php

 

/* Automatically detect language if not already defined */

if (!$cookie->id_lang && isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))

{

$array = explode(',', Tools::strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));

if (Tools::strlen($array[0]) > 2)

{

$tab = explode('-', $array[0]);

$string = $tab[0];

}

else

$string = $array[0];

if (Validate::isLanguageIsoCode($string))

{

$lang = new Language(Language::getIdByIso($string));

if (Validate::isLoadedObject($lang) && $lang->active)

$cookie->id_lang = (int)$lang->id;

}

}

 

Paste it in classes/Cookie.php at line 299. Change line 314 $cookie for $this. Should look like this :

 

}

else

$this->_content['date_add'] = date('Y-m-d H:i:s');

 

/* Automatically detect language if not already defined */

if (!$cookie->id_lang && isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))

{

$array = explode(',', Tools::strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));

if (Tools::strlen($array[0]) > 2)

{

$tab = explode('-', $array[0]);

$string = $tab[0];

}

else

$string = $array[0];

if (Validate::isLanguageIsoCode($string))

{

$lang = new Language(Language::getIdByIso($string));

if (Validate::isLoadedObject($lang) && $lang->active)

$this->id_lang = (int)$lang->id;

}

}

 

//checks if the language exists, if not choose the default language

if (!Language::getLanguage((int)$this->id_lang))

$this->id_lang = Configuration::get('PS_LANG_DEFAULT');

 

Found this solution here : https://github.com/PrestaShop/PrestaShop/pull/178

  • Like 3
Link to comment
Share on other sites

  • 2 weeks later...

hey grandzorglub.

 

thanks for this. i was looking for something like this since i cannot get geolocation to work on my prestashop :(

 

i have my shop default lang set to spanish.

 

i made the changes you posted above and to test it i went and installed firefox in spanish, deleted my historial and cache and went to my store and unfortunately it is showing my site in english and not spanish. google chrome and safari are in enlish on my computer and they show the site in english so i don't know what is going on.

 

is you fix compatible with firefox?

 

here is code lines 299-321 from my Cookie.php file. any ideas?

 

thanks,

jer...

 

----

 

//checks if the language exists, if not choose the default language

if (!$cookie->id_lang && isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))

{

$array = explode(',', Tools::strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));

if (Tools::strlen($array[0]) > 2)

{

$tab = explode('-', $array[0]);

$string = $tab[0];

}

else

$string = $array[0];

if (Validate::isLanguageIsoCode($string))

{

$lang = new Language(Language::getIdByIso($string));

if (Validate::isLoadedObject($lang) && $lang->active)

$this->id_lang = (int)$lang->id;

}

}

//checks if the language exists, if not choose the default language

if (!Language::getLanguage((int)$this->id_lang))

$this->id_lang = Configuration::get('PS_LANG_DEFAULT');

 

}

 

----

Link to comment
Share on other sites

Hi Fixgear!

First, don't forget to delete your cookies in your browser (in my firefox : preference/privacy), not only the historical and cache. With this fix, Prestashop detect language by cookie first and, if it doesn't see any cookie, it will check browser language.

 

Second, you can choose language order preference in firefox, It's in preference/content.

 

Finally, maybe the caching methode of the website have something to do with your issue. On this, I can't help you.

 

I just can't go any further in solution because I'm really a newbie in all this stuff. I found this fix and was able to applying it by trial-and-error method. I'm really far away to understand what I did exactly with this code! The only think I can say it's, for my part, it's working with about 4 different users with different browsers (firefox include) in different locations.

 

Good luck!

Edited by grandzorglub (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
  • 4 months later...

Sorry to ask, but that's it? I just delete the class_index.php and upload the Cookies.php and then what? Sorry to ask but I'm a newbie with Prestashop and better safe than sorry. 

 

BTW do I need to lowercase the Cookies.php to cookies.php perhaps since my hosting is based on an unix system?

 

Cheers,

BlackLotus

Link to comment
Share on other sites

Yes, I sure. I used a new installation.

 

Anyway check the end of class Cookie.php, there is a new property "detect_language":

...
// set detect_language to force going through Tools::setCookieLanguage to figure out browser lang
$this->detect_language = true;
...
And then in method "setCookieLanguage"  Tools.php :
 
public static function setCookieLanguage($cookie = null)
{
if (!$cookie)
$cookie = Context::getContext()->cookie;
/* If language does not exist or is disabled, erase it */
if ($cookie->id_lang)
{
$lang = new Language((int)$cookie->id_lang);
if (!Validate::isLoadedObject($lang) || !$lang->active || !$lang->isAssociatedToShop())
$cookie->id_lang = null;
}

/* Automatically detect language if not already defined, detect_language is set in Cookie::update */ 

if ((!$cookie->id_lang || isset($cookie->detect_language)) && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { 
$array = explode(',', Tools::strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])
); 
...

So by default prestashop 1.5.5 detect the browser language, I didn't change anything.

Edited by mkemink (see edit history)
Link to comment
Share on other sites

  • 4 months later...

v1.5.6.1 also does it by default.
But in my case I dont want PS to change the language for a specific shop
 

  • Should I modify something like top of header.tpl in template folder?
  • Or should I add some  "if (shop==X)" in that Tools.php?

I  tried first. -better to play with template files-. But writing {assign var="lang_iso" value="es"}, doesnt change the language of the page. 

I should also change another variable?  I am wrong with this way? I dont know to deal with smarty

PS: I also found {$cart->id_lang} {$img_lang_dir} wich I think I should change too.

Link to comment
Share on other sites

  • 6 months later...
  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...