javascript 如何开发网站多语言切换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15970948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to develop the site to switch between multiple languages?
提问by user2274193
I have developed the website in html5. In this I have an language selection option, now it is in English. If I select any other language, then it should show the contents in that language. I don't want to use the Google translator to translate the content. I have the content in all languages. If any tags or methods in html5 or HTML to get and display the contents specific to that language?
我已经在 html5 中开发了该网站。在这个我有一个语言选择选项,现在它是英语。如果我选择任何其他语言,那么它应该以该语言显示内容。我不想使用 Google 翻译器来翻译内容。我有所有语言的内容。如果 html5 或 HTML 中有任何标签或方法来获取和显示特定于该语言的内容?
回答by 19h
You can use Javascript localization (this is one of your tags, so I guess you'd use it).
您可以使用 Javascript 本地化(这是您的标签之一,所以我猜您会使用它)。
What you can do is a quick-check for the locale of the user, thus filling the page with a matching localization of your contents.
您可以做的是快速检查用户的语言环境,从而用您的内容的匹配本地化来填充页面。
Here's a quick and dirty example. (note: this is purely dynamic)
这是一个快速而肮脏的例子。(注意:这纯粹是动态的)
locale = (navigator["language"] || "en").split("-")[0];
localization = {
"de": "Hallo Benutzer!",
"en": "Hey user!"
};
text = localization[locale] || localization["en"];
e = document.createElement("span");
e.innerHTML = text;
document.body.appendChild(e);
回答by Ion Sapoval
If your website is a static one, then you can clone your site for each language, by using language specific content, and access each version with a different base url ( domain/en/..., domain/es/....).
如果您的网站是静态网站,那么您可以通过使用特定于语言的内容为每种语言克隆您的网站,并使用不同的基本 url ( domain/en/..., domain/es/.... )。
回答by John D
You will need to get your pages translated into whatever languages you want.
您需要将您的页面翻译成您想要的任何语言。
Duplicate your entire web structure per language so you end up with
复制每种语言的整个网络结构,这样你最终
www.example.com/en/.. www.example.com/fr/..
www.example.com/en/.. www.example.com/fr/..
etc
等等
Generally people give use a flag to indicate the language and redirect to the corresponding language page.
通常人们会使用标志来指示语言并重定向到相应的语言页面。
回答by Quentin
No.
不。
HTML allows you to specify the language you are working in with lang
attributes, but it provides no specific features for (manually or automatically) translation.
HTML 允许您使用lang
属性指定您正在使用的语言,但它不提供(手动或自动)翻译的特定功能。