我使用以下代码来使用 JS Gettext如果语言是德语。
if (lang == "de") {
var gt = new Gettext({"domain": "tag_cloud", "locale_data": json_de_data});
function _(ident) {
console.debug('gt.gettext("'+ident+'")');
return gt.gettext(ident);
}
console.debug("Using Gettext.");
}
else {
function _(ident) {
console.debug('return "'+ident+'"');
return ident;
}
console.debug("Using no translation.");
}
在 Firefox 中,控制台显示:
Using Gettext.
gt.gettext("Ubiqitous, but effective.")
在所有其他浏览器(Chromiun、Opera、IE、rekonq、Safari)中,我得到以下结果:
Using Gettext.
return "Ubiqitous, but effective."
我尝试删除 else
block ,然后它在所有浏览器中都有效,只是不适用于英语。
那么,尽管 else
block 没有执行,但后者的 _()
是否已定义?我如何才能在所有浏览器中实现此功能?
最佳答案
function _(ident) {}
函数声明将在运行时(即解析时)之前进行评估。
您可以更改为 var _ = function(ident) {}
,这样 _
将在运行时分配。
关于javascript - 尽管代码块未执行,但函数已定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9586582/