grandzorglub Posted March 14, 2013 Share Posted March 14, 2013 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 More sharing options...
piribipipi Posted March 15, 2013 Share Posted March 15, 2013 I think that this could help you: http://www.prestashop.com/forums/viewthread/105240/ Link to comment Share on other sites More sharing options...
grandzorglub Posted March 20, 2013 Author Share Posted March 20, 2013 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 3 Link to comment Share on other sites More sharing options...
piribipipi Posted March 20, 2013 Share Posted March 20, 2013 Thank youu!!! it workss!!! :D:D 1 Link to comment Share on other sites More sharing options...
fixgear Posted April 1, 2013 Share Posted April 1, 2013 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 More sharing options...
grandzorglub Posted April 2, 2013 Author Share Posted April 2, 2013 (edited) 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 April 2, 2013 by grandzorglub (see edit history) 1 Link to comment Share on other sites More sharing options...
fixgear Posted April 2, 2013 Share Posted April 2, 2013 THANKS! that "choose language order preference in firefox, It's in preference/content." worked!!! i had not done that and english was first jeremy... Link to comment Share on other sites More sharing options...
eleazar Posted April 13, 2013 Share Posted April 13, 2013 Thank you so much, grandzorglub. Works like a charm. For those who don't want to modify core files, I wrote an override for /override/classes/Cookie.php with the above modifications. Link to comment Share on other sites More sharing options...
blacklotus Posted September 3, 2013 Share Posted September 3, 2013 Ok an old but golden topic, does this still work in the new version?eleazar, so I just put this file in the /override/classes/ and that's it? Awesome. Link to comment Share on other sites More sharing options...
eleazar Posted September 3, 2013 Share Posted September 3, 2013 You mean 1.5.5? I guess it should work! But don't forget to delete /cache/class_index.php to make the ovveride happen. Link to comment Share on other sites More sharing options...
blacklotus Posted September 4, 2013 Share Posted September 4, 2013 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 More sharing options...
eleazar Posted September 4, 2013 Share Posted September 4, 2013 Yep, and... don't, Unix is case sensitive. Link to comment Share on other sites More sharing options...
blacklotus Posted September 4, 2013 Share Posted September 4, 2013 Yeah exactly why I asked =). Woha, this is easy, thought I was going to ahve to include the new file somewhere I had no idea it woks just by uploading and deleting. Awesome. Will try it today and report back . Once again thank you Link to comment Share on other sites More sharing options...
mkemink Posted September 5, 2013 Share Posted September 5, 2013 Hi, thanks eleazar. But now I have problem with admin panel, when I put Cookie.php in override, I can't log in on admin panel. Any idea? Link to comment Share on other sites More sharing options...
mkemink Posted September 5, 2013 Share Posted September 5, 2013 Hi, thanks eleazar. But now I have problem with admin panel, when I put Cookie.php in override, I can't log in on admin panel. Any idea? I solved it modifying the original Link to comment Share on other sites More sharing options...
eleazar Posted September 6, 2013 Share Posted September 6, 2013 Dammit, you are right! They changed the function! This is the modified ovrride for PrestaShop 1.5.5: Link to comment Share on other sites More sharing options...
mkemink Posted September 6, 2013 Share Posted September 6, 2013 Dammit, you are right! They changed the function! This is the modified ovrride for PrestaShop 1.5.5: Override_Cookie.php_for_v155.zip I updated to 1.5.5 and it's working withouth override class. Link to comment Share on other sites More sharing options...
eleazar Posted September 6, 2013 Share Posted September 6, 2013 You sure that this isn't because you modified the original file? Link to comment Share on other sites More sharing options...
mkemink Posted September 6, 2013 Share Posted September 6, 2013 (edited) 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 September 6, 2013 by mkemink (see edit history) Link to comment Share on other sites More sharing options...
palermuke Posted February 2, 2014 Share Posted February 2, 2014 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 smartyPS: I also found {$cart->id_lang} {$img_lang_dir} wich I think I should change too. Link to comment Share on other sites More sharing options...
palermuke Posted February 2, 2014 Share Posted February 2, 2014 (edited) so far I redirect ussing .htaccess to http://mysite.com/index.php?id_lang=3 as I dont know to do it another way Edited February 2, 2014 by palermuke (see edit history) Link to comment Share on other sites More sharing options...
El Patron Posted August 10, 2014 Share Posted August 10, 2014 I've finally gotten around to this pet project. For community review: [Module] Geo Localization - Advanced Browser Language Detect Link to comment Share on other sites More sharing options...
Gipielle Posted June 10, 2016 Share Posted June 10, 2016 Thanks Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now