在 html 中定义多种语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12150369/
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
define multiple languages in html
提问by swd_fs
I want to determine more than one language for a document, because it's available in more than one language. If I use:
我想为一个文档确定不止一种语言,因为它有不止一种语言。如果我使用:
<meta http-equiv="content-language" content="en,de,fr" />
this is not W3C valid and the validator says I should define it in the root's lang
attribute, but this attribute only supports one language:
这不是 W3C 有效的,验证器说我应该在 root 的lang
属性中定义它,但是这个属性只支持一种语言:
<html lang="en">
works, but not
有效,但不是
<html lang="de,en,fr">
So where should I define it?
那么我应该在哪里定义它呢?
回答by jagb
All attributes support only one language, so I believe you should define only one language; the most important language should be set. This can't be done if you have multiple languages on a single document, so here is the info to solve your problem:
所有属性只支持一种语言,所以我相信你应该只定义一种语言;应该设置最重要的语言。如果您在一个文档中有多种语言,则无法执行此操作,因此以下信息可以解决您的问题:
The lang and xml:lang attributes do not allow you to assign multiple languages to a single document. So if you're writing a Web page with multiple languages you have two options:
lang 和 xml:lang 属性不允许您为单个文档分配多种语言。因此,如果您使用多种语言编写网页,您有两种选择:
- Define a primary language with the
lang
attribute, and then call out the secondary language(s) withlang
attributes on elements in the document Define
lang
in the specific sections of the document as needed:<div lang="fr-CA" xml:lang="fr-CA"> Canadian French content... </div> <div lang="en-CA" xml:lang="en-CA"> Canadian English content... </div> <div lang="nl-NL" xml:lang="nl-NL"> Netherlands, Dutch content... </div>
- 用
lang
属性定义主要语言,然后用lang
文档中元素的属性调用次要语言 lang
根据需要在文档的特定部分中定义:<div lang="fr-CA" xml:lang="fr-CA"> Canadian French content... </div> <div lang="en-CA" xml:lang="en-CA"> Canadian English content... </div> <div lang="nl-NL" xml:lang="nl-NL"> Netherlands, Dutch content... </div>
I have some multiple-language pages and I do use the 2nd option.
我有一些多语言页面,我确实使用了第二个选项。
You might want to read http://www.w3.org/TR/2007/NOTE-i18n-html-tech-lang-20070412/#ri20060630.133619987
您可能想阅读http://www.w3.org/TR/2007/NOTE-i18n-html-tech-lang-20070412/#ri20060630.133619987
回答by Jukka K. Korpela
The meaning of the Content-Language HTTP header, and hence its meta
tag surrogate, is that it declares the languages of the document, or the languages of the intended audience (the relevant RFCs are contradicting), notthe languages of some other documents (like translations of the current document). The practical effect of header is small, probably limited to using the first language named as the language of the document, ifthere is no language information in HTML markup.
Content-Language HTTP 标头及其meta
标记代理的含义是它声明了文档的语言或目标受众的语言(相关的 RFC 是矛盾的),而不是其他一些文档的语言(例如当前文档的翻译)。header 的实际作用很小,如果HTML 标记中没有语言信息,可能仅限于使用命名为文档语言的第一种语言。
To indicate that a document is available in other languages, you can use tags like
要指示文档有其他语言版本,您可以使用标签,例如
<link rel="alternate" hreflang="de" href="foobar.de.html">
See 12.3.3 Links and search enginesin HTML 4.01 spec.
请参阅HTML 4.01 规范中的12.3.3 链接和搜索引擎。
There is no guarantee that this will have any effect. It might affect search engines, but not more than a normal link would do. Some old browser versions had commands for selecting alternate versions of a document, based on elements like this, but the feature seems to have been dropped.
不能保证这会产生任何影响。它可能会影响搜索引擎,但不会超过普通链接。一些旧的浏览器版本有用于选择文档的替代版本的命令,基于这样的元素,但该功能似乎已被删除。
回答by unor
What HTML version do you use? In HTML 4.01, your use of Content-Language
with multiple languages is valid. In HTML5, it's not.
你使用什么 HTML 版本?在HTML 4.01 中,您对Content-Language
多种语言的使用是有效的。在HTML5 中,它不是.
But even for HTML 4.01, the use of Content-Language
for the meta
element is not recommended: HTTP headers, meta elements and language information (W3C)
但即使对于 HTML 4.01,也不推荐使用Content-Language
formeta
元素:HTTP 标头、元元素和语言信息 (W3C)
回答by MaVRoSCy
You can't use it like this.
你不能这样使用它。
You'll have to either use an encoding that encompasses all desired chars (e.g UTF-8) which supports the entire Unicode range), or else use named entities or numeric references to include characters outside the encoding in use.
您必须使用包含所有所需字符的编码(例如,支持整个 Unicode 范围的 UTF-8),或者使用命名实体或数字引用来包含使用的编码之外的字符。
http://bytes.com/topic/html-css/answers/154652-multiple-languages-one-document
http://bytes.com/topic/html-css/answers/154652-multiple-languages-one-document
UPDATE
更新
If using HTML5 then you can use lang for each element. That means if you have a div that contains Mandarin Chinese in it, just define an attribute lang="zh-CN" for that div, like . ( What is the HTML5 alternative to the obsolete meta http-equiv=content-language.)
如果使用 HTML5,那么您可以对每个元素使用 lang。这意味着如果您有一个包含普通话的 div,只需为该 div 定义一个属性 lang="zh-CN",例如 . (过时的元 http-equiv=content-language 的 HTML5 替代品是什么。)
回答by Improv
As the other posters and the W3C have pointed out, you cannot specify more than one language in the lang
attribute of the html
tag.
正如其他海报和 W3C 所指出的,您不能lang
在html
标签的属性中指定一种以上的语言。
However, as shown in this answer to "What attribute value should I use for a mixed language page?", you can markup different parts of a page with elements such as div
and span
tags to indicate different languages (or references to other languages) used on the page.
但是,如“我应该为混合语言页面使用什么属性值?”的答案中所示。,你可以标记一个页面的不同部分的元素,如div
和span
标记,以表明在页面上使用不同的语言(或其他语言的引用)。
Also, you can create metadata that describes multiple languages for the intended audience of a page, rather than the language of a specific range of text. You do so by getting the server to send the information in the HTTP Content-Language
header. If your intended audience speaks more than one language, the HTTP header allows you to use a comma-separated list of languages.
此外,您可以创建元数据,为页面的目标受众描述多种语言,而不是特定范围文本的语言。您可以通过让服务器发送 HTTPContent-Language
标头中的信息来实现。如果您的目标受众使用多种语言,则 HTTP 标头允许您使用逗号分隔的语言列表。
Here is an example of an HTTP header that declares the resource to be a mixture of English, Hindi and Punjabi from the W3C's article Declaring language in HTML:
以下是一个 HTTP 标头示例,该标头将资源声明为 W3C 的文章用 HTML 声明语言中的英语、印地语和旁遮普语的混合体:
Content-Language: en, hi, pa
Please Note:since you should always use a language attribute on the html
tag, and the language attribute always overrides the HTTP header information, this really becomes a fine point. The HTTP header should be used only to provide metadata about the intended audience of the document as a whole, and the language attribute on the html
tag should be used to declare the default language of the content.
请注意:由于您应该始终在html
标签上使用语言属性,并且语言属性始终会覆盖 HTTP 标头信息,因此这确实是一个很好的点。HTTP 标头应仅用于提供有关整个文档的预期受众的元数据,html
标签上的语言属性应用于声明内容的默认语言。
For details on this last technique, see HTTP headers, meta
elements and language information. For general language declarations and markup, see Declaring language in HTML.
有关最后一种技术的详细信息,请参阅HTTP 标头、meta
元素和语言信息。有关一般语言声明和标记,请参阅在 HTML 中声明语言。