prestashop根据访问者ip所在国家调用语言包

客户第一次访问你的网站,你就显示出客户所在国家的语言包,这样会给用户体验大大加分。

其实做到这点很简单。

1.判断用户ip的国家

2.调用prestashop对应的语言包

 

代码修改classes/tools.php 文件里的一个方法就能做到。
public static function setCookieLanguage()
{
global $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) OR !$lang->active)
$cookie->id_lang = NULL;
}

/* Automatically detect language if not already defined */
if (!$cookie->id_lang AND isset($_SERVER[‘HTTP_ACCEPT_LANGUAGE’]))
{
$array = explode(‘,’, self::strtolower($_SERVER[‘HTTP_ACCEPT_LANGUAGE’]));
if (self::strlen($array[0]) > 2)
{
$tab = explode(‘-‘, $array[0]);
$string = $tab[0];
}
else
$string = $array[0];
if (Validate::isLanguageIsoCode($string))
{
$lang = new Language((int)(Language::getIdByIso($string)));
if (Validate::isLoadedObject($lang) AND $lang->active)
$cookie->id_lang = (int)($lang->id);
}
}

/* If language file not present, you must use default language file */
if (!$cookie->id_lang OR !Validate::isUnsignedId($cookie->id_lang))
{
//客户第一次访问网站时根据客户来源ip 设置默认语言  $cookie->id_lang 值需要根据你网站的语言包来修改
//update begin paul 20120727
$ip = $_SERVER[‘REMOTE_ADDR’];
$iptxt = file_get_contents(“http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=txt&ip=”.$ip);
if(preg_match(‘|美国|’, $iptxt))
$cookie->id_lang = 1;
elseif(preg_match(‘|法国|’, $iptxt))
$cookie->id_lang = 2;
elseif(preg_match(‘|西班牙|’, $iptxt))
$cookie->id_lang = 3;
elseif(preg_match(‘|德国|’, $iptxt))
$cookie->id_lang = 4;
elseif(preg_match(‘|意大利|’, $iptxt))
$cookie->id_lang = 5;
elseif(preg_match(‘|中国|’, $iptxt))
$cookie->id_lang = 6;
else
$cookie->id_lang = (int)(Configuration::get(‘PS_LANG_DEFAULT’));
//update end paul 20120727
}

$iso = Language::getIsoById((int)$cookie->id_lang);
@include_once(_PS_THEME_DIR_.’lang/’.$iso.’.php’);

return $iso;
}

return $iso;
}

prestashop根据访问者ip所在国家调用语言包》有128个想法

  1. antalya haber

    We absolutely love your blog and find nearly all of your post’s to be what
    precisely I’m looking for. Do you offer guest writers to write content for yourself?

    I wouldn’t mind publishing a post or elaborating on a
    lot of the subjects you write concerning here.

    Again, awesome site!

    回复

发表评论

电子邮件地址不会被公开。 必填项已用*标注