X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=56)
-   -   X5 - Setting the language based on user's browser language (https://forum.x-cart.com/showthread.php?t=72438)

xgarb 07-14-2015 09:33 AM

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?

totaltec 07-14-2015 11:09 AM

Re: X5 - Setting the language based on user's browser language
 
I would look in classes/XLite/View/LanguageSelector/Customer.php at method isActiveLanguage()

xgarb 07-15-2015 08:38 AM

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.

qualiteam 07-17-2015 01:25 AM

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 :-))


All times are GMT -8. The time now is 07:33 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.