我正在建立一个REST API(这支持国际化)的前端网站,我不知道哪条路要走国际化。我已经研究过js和html解决方案,但它们似乎都不如服务器端选项。i18n静态HTML内容
鉴于大多数页面包含只需要本地化支持的静态内容,jsp将是一个很好的解决方案吗? jsf看起来好像过火了。
我正在建立一个REST API(这支持国际化)的前端网站,我不知道哪条路要走国际化。我已经研究过js和html解决方案,但它们似乎都不如服务器端选项。i18n静态HTML内容
鉴于大多数页面包含只需要本地化支持的静态内容,jsp将是一个很好的解决方案吗? jsf看起来好像过火了。
1)如果你只有静态内容创建不同的HTML文件与 不同的语言,并把它放在单独的文件夹,并访问该网站 像
for English
http://yourdomain.com/en/english_index.html
for French
http://yourdomain.com/fr/french_index.html
2)JSP也如果你有动态操作,这是一个不错的选择有一个 好的选择来维护国际资源界限。
我建议使用选项1
我去真的不能推荐具有不同的HTML文件。可定位性最佳实践建议将代码与翻译分离。
我知道的最快,最简单,阻碍性最小的方法是使用Google ARB。考虑以下示例HTML:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Testing ARB...</title>
</head>
<body>
<h2>This is a test.</h2>
</body>
</html>
现在需要提取可本地化的内容。这是可能做到这一点无论是使用extractor tool ARB提供,或者如果你的网页是非常简单的,你甚至可以做手工:
<html>
<head arb:namespace="test">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title arb:id="MSG_HTML_TITLE">Testing ARB...</title>
</head>
<body>
<h2 arb:id="MSG_BODY_TEST">This is a test.</h2>
</body>
</html>
然后让我们创建这些消息的资源文件,还提供翻译:
arb.register(
"test",
{
"MSG_HTML_TITLE": "Testing ARB",
"MSG_BODY_TEST": "This is a test.",
"MSG_CURR_LOCALE": "...and the selected language is \"{currentLocale}\".",
"@MSG_CURR_LOCALE": {
"placeholders": {
"0": {
"description": "This variable would show the current locale.",
"example": "fr"
}
}
}
}
);
arb.register(
"test:de",
{
"MSG_HTML_TITLE": "ARB auf Probe",
"MSG_BODY_TEST": "Das ist ein Test.",
"MSG_CURR_LOCALE": "...und die ausgewählte Sprache ist \"{currentLocale}\".",
"@MSG_CURR_LOCALE": {
"placeholders": {
"0": {
"description": "This variable would show the current locale.",
"example": "fr"
}
}
}
}
);
最后,将JS添加到HTML中。另外,提供一种简单的方法从URL中获取选定的语言环境;即./index.html?locale=de
<html>
<head arb:namespace="test">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title arb:id="MSG_HTML_TITLE">Testing ARB...</title>
<script src="arb/lib/arbcore.js"></script>
<script src="test.arb"></script> <!-- ARB file w/ translations. -->
</head>
<body>
<h2 arb:id="MSG_BODY_TEST">This is a test.</h2>
<!-- Get locale from URL and translate page HTML -->
<script>
function main(){
var locale = arb.getParamFromUrl('locale');
if (!locale){
locale = 'en';
}
arb.setResourceSelector(locale);
// JS localization
var r$ = arb.getResource("test");
document.write(arb.msg(r$.MSG_CURR_LOCALE, {'currentLocale': locale}));
// This should appear after all the translatable HTML content
arb.localizeHtml();
}
main();
</script>
</body>
</html>
此示例的代码可以发现here。
这是一个前端解决方案吗?它的性能与服务器端解决方案相比如何? – 2013-04-26 01:28:53
通过每种语言的翻译编译的HTML优于此。假设,动态获取翻译将是最慢的。显然,这些是猜测而非基准的事实结果。 – Shervin 2013-04-26 01:37:22
您能否告诉我如何导入它?我应该输入一个ascript吗? – 2017-03-30 15:54:29
选项1的问题在于维护工作量对于每种附加语言呈指数级增长,并且没有任何措施可以在文件的不同语言版本之间实施一致性。 – 2013-04-24 06:05:54
嗯,选择选项1)还是2)取决于开发商的情况,要求和舒适性 – Madhu 2013-04-24 06:32:32