Jump to content

How to set Guest default language?


Recommended Posts

Hi all,

 

Please show me how to set the default language for my website?

 

I tried to change from English to Vietnamese but the first time I come to my website it always choose English as default.

 

My website url: www.adorastore.com

 

Please help me.

 

Thanks :)

Link to comment
Share on other sites

ps does a couple things that may be confusing you.

 

1. it will detect the browsers first language in the http_accept_language and if present it will serve that, i.e. if browser says english, then ps will serve english

 

2. ps will remember lang from last visit (if cookie not expired), so when testing and you have set browser to viet lang, clear cookie before doing test.

 

tip, those entering shop with viet as browser lang should see viet lang.

  • Like 1
Link to comment
Share on other sites

ps does a couple things that may be confusing you.

 

1. it will detect the browsers first language in the http_accept_language and if present it will serve that, i.e. if browser says english, then ps will serve english

 

2. ps will remember lang from last visit (if cookie not expired), so when testing and you have set browser to viet lang, clear cookie before doing test.

 

tip, those entering shop with viet as browser lang should see viet lang.

 

Is there any way to set the Vietnamese language as default even if the browser lang is English (at the first time coming to my web) ?

Link to comment
Share on other sites

Is there any way to set the Vietnamese language as default even if the browser lang is English (at the first time coming to my web) ?

 

Why do you want that? If your visitor is from different country, there's no reason to show him Vietnamese. But if the problem is that even though your visitor comes from Vietnam, but it sees english because of browser language, you can change language based on IP. I can help with that - I did this yesterday for my own shop. Because a lot of users in my country have browser language set to english, and it's not a good way to determine a user language based on browser language.

  • Like 1
Link to comment
Share on other sites

Why do you want that? If your visitor is from different country, there's no reason to show him Vietnamese. But if the problem is that even though your visitor comes from Vietnam, but it sees english because of browser language, you can change language based on IP. I can help with that - I did this yesterday for my own shop. Because a lot of users in my country have browser language set to english, and it's not a good way to determine a user language based on browser language.

 

most of my customer come from Vietnam, so that I need to set Vietnamese as default. Can you please help me?

Link to comment
Share on other sites

most of my customer come from Vietnam, so that I need to set Vietnamese as default. Can you please help me?

 

Create a file your_ps_install_dir/override/classes/Tools.php and put this code in it:

<?php

class Tools extends ToolsCore
{
	/**
	* 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)
		{
			$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']))
		{
			$ip = $_REQUEST['REMOTE_ADDR'];
			$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
			if($query && $query['status'] == 'success') {
				$string = Tools::strtolower($query['countryCode']);
			} else {
				$array  = explode(',', Tools::strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
				$string = $array[0];
			}
			
			if (Validate::isLanguageCode($string))
			{
				$lang = Language::getLanguageByIETFCode($string);
				if (Validate::isLoadedObject($lang) && $lang->active && $lang->isAssociatedToShop())
				{
					Context::getContext()->language = $lang;
					$cookie->id_lang = (int)$lang->id;
				}
			}
		}

		if (isset($cookie->detect_language))
			unset($cookie->detect_language);

		/* 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;
	}
}

From now on, it will choose language based on IP, and not on browser language. But then, from now on, it's better to set default language to English. This way, users from Vietnam will see vietnamese language, and other users - english.

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

Create a file your_ps_install_dir/override/classes/Tools.php and put this code in it:

<?php

class Tools extends ToolsCore
{
	/**
	* 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)
		{
			$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']))
		{
			$ip = $_REQUEST['REMOTE_ADDR'];
			$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
			if($query && $query['status'] == 'success') {
				$string = Tools::strtolower($query['countryCode']);
			} else {
				$array  = explode(',', Tools::strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
				$string = $array[0];
			}
			
			if (Validate::isLanguageCode($string))
			{
				$lang = Language::getLanguageByIETFCode($string);
				if (Validate::isLoadedObject($lang) && $lang->active && $lang->isAssociatedToShop())
				{
					Context::getContext()->language = $lang;
					$cookie->id_lang = (int)$lang->id;
				}
			}
		}

		if (isset($cookie->detect_language))
			unset($cookie->detect_language);

		/* 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;
	}
}

From now on, it will choose language based on IP, and not on browser language. But then, from now on, it's better to set default language to English. This way, users from Vietnam will see vietnamese language, and other users - english.

Works like a charm. Thankssssssss :)

Link to comment
Share on other sites

thanks for sharing drunkbum

 

from 

 

http://ip-api.com/docs/

 

Usage limits
Our system will automatically ban any IP addresses doing over 250 requests per minute.
You are free to use ip-api.com for non-commercial use.
 
I certainly like remote IP look up as native PrestaShop geolocalization uses free geolitecity.dat which at best is 85% accurate and non-resolved IP's can not shop.  PrestaShop supports the 'paid' maxmind but it's expensive and still have issue of unresolved not being able to shop. Module Geo Targeting Pro though allows one to set the default country when IP is not resolved so visitor can shop.
 
happy prestashopping.
 
note: to use drunkbum override and your shop is 1.5-1.5.6.0 add the following into override/classes/cookie.php.  
//Override: Set detect language indicator for 1.5-->1.5.6.0
class Cookie extends CookieCore
{
		public function update($nullValues = false)
		{
			parent::update($nullValues = false);
			if ((substr(_PS_VERSION_, 0, 7) < '1.5.6.1'))
				$this->detect_language = true;
		}
}

also drunkbum, consider changing

$ip = $_REQUEST['REMOTE_ADDR'];

with 

$ip = Tools::getRemoteAddr();
  • Like 1
Link to comment
Share on other sites

 

thanks for sharing drunkbum

 

from 

 

http://ip-api.com/docs/

 

Usage limits
Our system will automatically ban any IP addresses doing over 250 requests per minute.
You are free to use ip-api.com for non-commercial use.
 
I certainly like remote IP look up as native PrestaShop geolocalization uses free geolitecity.dat which at best is 85% accurate and non-resolved IP's can not shop.  PrestaShop supports the 'paid' maxmind but it's expensive and still have issue of unresolved not being able to shop. Module Geo Targeting Pro though allows one to set the default country when IP is not resolved so visitor can shop.
 
happy prestashopping.
 
note: to use drunkbum override and your shop is 1.5-1.5.6.0 add the following into override/classes/cookie.php.  
//Override: Set detect language indicator for 1.5-->1.5.6.0
class Cookie extends CookieCore
{
		public function update($nullValues = false)
		{
			parent::update($nullValues = false);
			if ((substr(_PS_VERSION_, 0, 7) < '1.5.6.1'))
				$this->detect_language = true;
		}
}

also drunkbum, consider changing

$ip = $_REQUEST['REMOTE_ADDR'];

with 

$ip = Tools::getRemoteAddr();

 

I think it's very unlikely to reach those request limits. At least, for average user. There's other online services where you can look up IP's, It just was my personal preference to use this particular service. Maybe some day I will create a module for this, with possibility to use local ip database or remote look up, but I needed easy and fast solution, so I chose this one I posted. And I forgot to mention that I only tested this on PS 1.6.0.9, so I didn't knew the compatability with other versions.

Thank you for the tip to use Tools::getRemoteAddr(); I didn't knew about this native PS function.

Link to comment
Share on other sites

×
×
  • Create New...