为什么代码不起作用,您有一些问题。弄清楚发生了什么的最好方法是查看控制台。在您的浏览器中,按 F12 以获取开发人员工具。Chrome/Firefox 和 IE10+ 拥有最好的工具。
第一个问题是 Dropbox 不是托管 Web 文件的好地方。当您对该文件发出请求时,它实际上是返回所有 html 以呈现页面,如果您转到 url 则可以看到它。所以它不是有效的 JavaScript。您确实需要一些适当的网络空间来测试它,或者您可以使用网络服务器(IIS、Apache、XAMP 等)在本地进行

HTML 中没有 id 为“text”或“title”的元素,因此 fordocument.getElementById("text").firstChild
和title = document.getElementById("title").firstChild
返回 null 的代码,这意味着稍后设置 i18n 文本的代码失败。
最接近我可以让他在jsFiddle DEMO中工作但我无法在我的托管空间上设置 Access-Control-Allow-Origin 标头以允许跨站点 json 请求。
我的主机上有一个演示页面供您查看它的工作原理:http ://www.forward-slash.co.uk/demo/i18ntest.htm
HTML:
<p id="text">Should be set by js</p>
<p id="title">Should be set by js</p>
<p id="instruction1">Should be set by js</p>
<input id="num" type="text" size="5" maxlength="5" value="2" />
<p id="instruction2">Should be set by js</p>
<select id="lang" size="1">
<option>select language</option>
<option value="de">German</option>
<option value="en">English</option>
</select>
JavaScript:
// location of the translation files go here
i18n.init({ resGetPath: '/lib/__ns__-__lng__.json.txt' });
var text = document.getElementById("text").firstChild,
title = document.getElementById("title").firstChild,
instruction1 = document.getElementById("instruction1").firstChild,
instruction2 = document.getElementById("instruction2").firstChild,
num = document.getElementById("num"),
lang = document.getElementById("lang");
i18n.init(function(t) {
title.nodeValue = i18n.t("title");
text.nodeValue = i18n.t("ball", { count: 2 });
instruction1.nodeValue = i18n.t("chooser.ball");
instruction2.nodeValue = i18n.t("chooser.lang");
});
num.onchange = function (e) {
var t =i18n.t("ball", { count: parseInt(num.value) });
text.nodeValue=t;
};
lang.onchange = function (e) {
i18n.setLng(lang.value);
// it appears the above sets a language cookie with the selected language
// we can refresh the page for the correct translations, but e need to put in
// a bit of a delay to allow the cookie to be set
setTimeout("location.reload(true);",500);;
};