Warning: Undefined array key "night" in /www/wwwroot/dhaomu.com/wp-content/themes/b2/header.php on line 18

EcShop网站中英双语自动切换判断的方案

ecshop教程:ecshop网站中英双语自动切换判断的方案!个人思路是根据浏览器语言自动判断,如果是中文浏览器,就使用中文模板,英文的就使用英文模板。

ecshop教程:ecshop网站中英双语自动切换判断的方案!个人思路是根据浏览器语言自动判断,如果是中文浏览器,就使用中文模板,英文的就使用英文模板。

中、英模板的区别是调用了不同的产品分类、文章分类,而模板中相同的固定文字可以设定language,这样就不用去建立两个网站了!

整个逻辑流程如下:

  • PHP判断浏览器语言
  • 将判断写入Cookie
  • 同时改变_CFG['lang']的值
  • _CFG['template']=_CFG['lang'],便于模板自动切换
  • 当用户人工选择?lang=cn/en时,进行切换

1、新建两个模板 zh_cn 和 en_us

2、includes\lib_common.php -> load_config()

最后加入

$lang = array(
    'cn'=>'zh_cn',
    'en'=>'en_us'
  );
  if(isset($_REQUEST['lang']) && isset($lang[$_REQUEST['lang']])){
    $arr['lang'] = $lang[$_REQUEST['lang']];
    setcookie('ACCEPT_LANGUAGE', $arr['lang']);
  }elseif(isset($_COOKIE['ACCEPT_LANGUAGE']) && in_array($_COOKIE['ACCEPT_LANGUAGE'], array('zh_cn', 'en_us'))){
    $arr['lang'] = $_COOKIE['ACCEPT_LANGUAGE'];
  }else{
    $accept_language = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']);
    if(strpos($accept_language, 'zh-c')!==false){
      $arr['lang'] = 'zh_cn';
    }else{
      $arr['lang'] = 'en_us';
    }
    setcookie('ACCEPT_LANGUAGE', $arr['lang']);
  }
  $arr['template'] = $arr['lang'];
 
  return $arr;

3、includes\cls_template.php -> fetch()

//$cachename = basename($filename, strrchr($filename, '.')) . '_' . $cache_id;

修改为

$cachename = basename($filename, strrchr($filename, '.')) . '_' . $cache_id.'_'.$GLOBALS['_CFG']['template'<span style="font-family: Arial, Helvetica, sans-serif;">];</span>

4、includes\cls_template.php -> make_compiled()

//$name = $this->compile_dir . '/' . basename($filename) .'.php';

修改为

$name = $this->compile_dir . '/' . basename($filename) .$GLOBALS['_CFG']['template'].'.php';

5、includes\cls_template.php -> is_cached()

//$cachename = basename($filename, strrchr($filename, '.')) . '_' . $cache_id;

修改为

$cachename = basename($filename, strrchr($filename, '.')) . '_' . $cache_id.'_'.$GLOBALS['_CFG']['template'];

修改模板解析类是为了保证不同的中、英模板之间的模板变异问没有冲突

6、在模板的任意位置加上

<a href="?lang=cn">Chinese</a><a href="?lang=en">English</a>

最后修改的是languages\zh_cn\common.phplanguages\en_us\common.php,用

{$lang.xx}

这个方法也适用多语言的设定

温馨提示:
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:duhaomu@163.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

给TA赞赏
共{{data.count}}人
人已赞赏
编程技巧

织梦根据特定需要调用文章的标签代码

2023-1-7 0:00:00

编程技巧

织梦文章内容body图文分离调用代码

2023-1-10 0:00:00

0 条回复 A文章作者 M管理员

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914
----《》
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索