Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

X5 - Setting the language based on user's browser language

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 07-14-2015, 09:33 AM
 
xgarb xgarb is offline
 

eXpert
  
Join Date: Jul 2004
Location: UK
Posts: 263
 

Default X5 - Setting the language based on user's browser language

In x4 I had some code that identified the user's language preference (using weighted values) and then displayed the site in the closest language possible with English as the default.

I did this by adding code before the get_language.php include that followed the logic below to set the store language.

1) Check user is not a robot and hasn't previously chosen a language
2) Set language based on browser setting and available languages

The user could set their language to override the auto detect using a cookie that lasted a year.

I want to do this in x5 but I can't work out which class/method to override so I can add in my extra detection code.

Any pointers?
__________________
Core version: 5.5.xx
Reply With Quote
  #2  
Old 07-14-2015, 11:09 AM
  totaltec's Avatar 
totaltec totaltec is offline
 

X-Guru
  
Join Date: Jan 2007
Location: Louisville, KY USA
Posts: 5,823
 

Default Re: X5 - Setting the language based on user's browser language

I would look in classes/XLite/View/LanguageSelector/Customer.php at method isActiveLanguage()
__________________
Mike White - Now Accepting new clients and projects! Work with the best, get a US based development team for just $125 an hour. Call 1-502-773-6454, email mike at babymonkeystudios.com, or skype b8bym0nkey

XcartGuru
X-cart Tutorials | X-cart 5 Tutorials

Check out the responsive template for X-cart.
Reply With Quote
  #3  
Old 07-15-2015, 08:38 AM
 
xgarb xgarb is offline
 

eXpert
  
Join Date: Jul 2004
Location: UK
Posts: 263
 

Default Re: X5 - Setting the language based on user's browser language

I think I have this working. It's just hacks to files in var/ at the moment.

In var/run/classes/XLite/Controller/AControllerAbstract.php
Code:
protected function doActionChangeLanguage() { $code = strval(\XLite\Core\Request::getInstance()->language); if (!empty($code)) { $language = \XLite\Core\Database::getRepo('\XLite\Model\Language')->findOneByCode($code); setcookie('store_language', $code, time()+31536000,"/"); // new line $_COOKIE['store_language'] = $code; // new line if (isset($language) && $language->getEnabled()) { \XLite\Core\Session::getInstance()->setLanguage($language->getCode()); if (\XLite\Core\Auth::getInstance()->isLogged()) { \XLite\Core\Auth::getInstance()->getProfile()->setLanguage($language->getCode()); \XLite\Core\Database::getEM()->flush(); } } } $this->setReturnURL($this->getReferrerURL()); }


In... var/run/classes/XLite/Core/Session.php
New Method:
Code:
/** * Browser language preferences * */ public function browserLang($l=['en'],$u){return $l[array_keys($l,substr(explode(',',$u?:$_SERVER['HTTP_ACCEPT_LANGUAGE'])[0],0,2))[0]]?:$l[0];}

New method:
Code:
/** * language cooookies! * */ public function getLanguageCookie() { $store_language_codes = array('en','es','fr','de','it') ; $cookieLanguageCode = NULL; // -- If cookie is set if ( in_array(strtolower($_COOKIE[store_language]), $store_language_codes) ) { $cookieLanguageCode = strtolower($_COOKIE[store_language]) ; } return $cookieLanguageCode; }

Totally rewritten method...

Code:
/** * Get language * * @return \XLite\Model\Language */ public function getLanguage() { if (!isset($this->language)){ $langCookie = $this->getLanguageCookie(); if (!empty($langCookie)){ $this->language = \XLite\Core\Database::getRepo('XLite\Model\Language') ->findOneByCode($this->getLanguageCookie()); } elseif (empty($langCookie)) { //no cookie set so best guess from browser $bestLanguageOption = $this->browserLang(['en','es','fr','de','it']); $this->language = \XLite\Core\Database::getRepo('XLite\Model\Language') ->findOneByCode($bestLanguageOption); } } return $this->language; }

Probably could be made nicer.
__________________
Core version: 5.5.xx
Reply With Quote

The following user thanks xgarb for this useful post:
qualiteam (07-17-2015)
  #4  
Old 07-17-2015, 01:25 AM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: X5 - Setting the language based on user's browser language

Good job!

You are to wrap up your changes as a custom module as all changes to files in the var/ directory will disappear once you upgrade to a newer version, install a new module, or just run the redeploy routine.

You will be able to re-use the module for other X-Cart 5 stores and even list it at http://www.x-cart.com/extensions/addons (if you want )
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 11:15 PM.

   

 
X-Cart forums © 2001-2020