What Values Can HTML's lang Attribute Use, and How Do You Follow the Standard?
This post was translated from Chinese by AI. If anything reads oddly, the Chinese original is authoritative. 中文原文
Recommended for Chinese:
lang="zh-cmn-Hans"
In the language tag "zh-cmn-Hans", "cmn" is the code for Mandarin Chinese, and "Hans" is the code for Simplified Chinese.
The full terms are:
- The full term for "cmn" is "Chinese, Mandarin", meaning Mandarin Chinese.
- The full term for "Hans" is "Simplified Chinese", meaning Chinese written in simplified characters.
The abbreviation "Hans" for "Simplified Chinese" comes from the English transliteration of the Chinese word for Chinese characters. In ISO 639-3 language codes, "Han" was chosen as the code for Simplified Chinese. To distinguish between different Chinese writing systems, such as Traditional Chinese and Simplified Chinese, "Hans" and "Hant" are used as suffixes for simplified and traditional characters.
The suffixes "Hans" and "Hant" come from the English transliteration of the Chinese word for Chinese characters: "Hans" represents the first three letters of "Hanzi", while "Hant" represents the last three letters of "Hanzi". This choice was intended to distinguish Chinese writing systems clearly and concisely.
Most instructors only briefly touch on the lang attribute, giving you a few common values, such as lang="en" for English and lang="zh" for Chinese.
But as we encounter more code, we discover that the lang attribute is not as simple as it seems.
For example: What values can the lang attribute take? How do we use it according to the specification? What is the difference between lang="zh", lang="zh-CN", and lang="zh-cmn-Hans"?
Not sure how to answer any of these? Let's use this article to sort things out so we can refer back to it later.
1. What does the lang attribute do?
The list of Html global attributes describes lang as “Defines the language used in the element.” As the name suggests, the lang attribute defines the language used in an element.
2. How is the lang attribute defined, and how do we use it according to the specification?
According to the explanation of lang on MDN, it is defined by the IETF's BCP 47 specification.
We previously published an article explaining What is BCP 47? in detail. Click through if you would like to learn more.
In short, BCP 47 is a specification for language tags and tag-matching algorithms designed to better identify the wide variety of languages around the world.
The latest specification defines the syntax of language tags as follows:
language-extlang-script-region-variant-extension-privateuse
Subtags are joined in order with hyphens (-) to form a complete language tag. The format of each subtag follows these standards:
language: A 2-letter language code from ISO639-1 (2002).
extlang: ISO639-3 codes, each consisting of 3 letters.
script: A 4-letter script code from ISO15924.
region: A 2-letter region code from ISO3166-1_alpha-2 (usually uppercase), or a 3-digit geographical area code from UNM.49.
variant: The IANA Language Subtag Registry.
3. What values can the lang attribute take?
As we learned above, the lang attribute is defined by BCP 47, a specification for language tags and tag-matching algorithms designed to better identify the wide variety of languages around the world.
Because of this linguistic diversity, the lang attribute can take a vast number of values. However, the same rules apply to all of them: any value that follows the latest language-tag syntax described in section 2 is a standard lang value.
There are far too many possible values to list them all, so here we will focus on subtags related to Chinese.
language
zh Chinese characters or the Chinese language
extlang
cdo Eastern Min
cjy Jin Chinese
cmn Mandarin Chinese
cpx Pu-Xian Min
czh Huizhou Chinese
czo Central Min
gan Gan Chinese
hak Hakka Chinese
hsn Xiang Chinese
mnp Northern Min
nan Southern Min
wuu Wu Chinese
yue Cantonese
lzh Literary Chinese
script
Hans Simplified Chinese characters
Hant Traditional Chinese characters
region
CN CHINA China
HK HONG KONG Hong Kong
TW Taiwan Taiwan
SG Singapore Singapore
4. What is the difference between lang="zh", lang="zh-CN", and lang="zh-cmn-Hans"?
Based on sections 2 and 3 above, lang="zh" and lang="zh-CN" are both deprecated forms, while lang="zh-cmn-Hans" is the correct form.
Original article information:
- https://www.hubaye.com/front/post/219.html
- When we first start learning Html, one of the first global attributes we encounter is lang, which we can use to define the language used in an element.
- 2023-09-03 11:09:03
Last updated 2024-01-14
Comments 0