网站语言英文解决方案通过改变缓冲区来实现
/*
网站语言英文解决方案——通过改变缓冲区来实现
功能:根据cookie判断用户选择的语言,默认为中文(不执行替换),当用户在页面中选择英语时候,则通过JS写入cookie语言状态码,此页面通过判断改cookie来判断是否进行语言翻译.
说明:在前端通过JS判断用户选择的语言,用户选择英文时候,JS写入cookie语言状态为‘EN’,后台程序通过判断cookie保存的语言编码执行语言翻译操作.
作者:aibhsc
使用方法:在下面文件中加入要替换的中英文,然后在程序入口文件引用该文件即可.
应用场景:对于企业网站英文不常使用,修改程序与数据库代价太高,但又必须要英文的网站,可以尝试使用此程序.
*/
<?php //通过一维数组存储语言包,节省空间,方便键的处理,注意:①键值别错位;②长字符串优先 //语言包,把要翻译的内容放在此处——start $cn[]='用户登录';$en[]='UserLogin'; $cn[]='登录';$en[]='Login'; $cn[]='注册';$en[]='Reg'; $cn[]='首页';$en[]='Home'; $cn[]='查看';$en[]='View'; $cn[]='去向';$en[]='Local'; $cn[]='钻重';$en[]='Weight'; $cn[]='国检';$en[]='NGTC'; $cn[]='';$en[]=''; //语言包——end //全局定义,引用语言包. $GLOBALS['global_CN']=&$cn; $GLOBALS['global_EN']=&$en; $GLOBALS['global_lang']=$_COOKIE['user_lang']?$_COOKIE['user_lang']:'CN'; cache(); //执行语言翻译操作 function cache(){ ob_start(array($this, "AutoCache")); //不懂这行请查看PHP手册call_user_func里面第四个例子“Example #4” } function cn2en($body){ //参数:$body 要翻译的内容 ;$arr_cn 中文语言包;$arr_en 英文语言包; 返回值:替换后的字符串 //当两个语言包键值不对,则返回原值不进行任何操作. if(count($GLOBALS['global_CN'])<>count($GLOBALS['global_EN'])){return $body;} //当用户选择英文时,则将文档中的中文根据语言包来替换为英文 if($GLOBALS['global_lang']=='EN'){$body=str_ireplace($GLOBALS['global_CN'],$GLOBALS['global_EN'],$body);} return $body; } function AutoCache($contents){ $contents = cn2en($contents); //执行语言翻译 return $contents; } ?>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
1 months ago
By DDD
R.E.P.O. Best Graphic Settings
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
1 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1359
52

