js通过googleAIP翻译PHP系统的语言配置的实现代码_javascript技巧
程序员文章站
2022-04-02 13:56:52
...
因为php的配置一般用数据,正好和js里的对象表示一般,所以可以这样:
var $strings={};
$strings['hide']='hide';
$strings['show']='show';
google.load("language", "1");
function translate(name,oldValue){
var fromlang = "en";
var tolang ="zh-CN";
var str="$strings['"+name+"']="+"'"+oldValue+"';
";
var div=document.getElementById ("divResult");
google.language.translate(oldValue, fromlang, tolang, function(result) {
if (!result.error) {
str="$strings['"+name+"']="+"'"+result.translation+"';
";
}
div.innerHTML+=str;
});
}
setTimeout(function(){
for(var name in $strings){
translate(name,$strings[name]);
}
},2000)
复制代码 代码如下:
复制代码 代码如下:
var $strings={};
$strings['hide']='hide';
$strings['show']='show';
google.load("language", "1");
function translate(name,oldValue){
var fromlang = "en";
var tolang ="zh-CN";
var str="$strings['"+name+"']="+"'"+oldValue+"';
";
var div=document.getElementById ("divResult");
google.language.translate(oldValue, fromlang, tolang, function(result) {
if (!result.error) {
str="$strings['"+name+"']="+"'"+result.translation+"';
";
}
div.innerHTML+=str;
});
}
setTimeout(function(){
for(var name in $strings){
translate(name,$strings[name]);
}
},2000)