网页头部的声明应该是用 lang=“zh” 还是 lang=“zh-CN”?
遇到问题
不知道大家有没有留意到一个问题,就是使用 VsCode 新建的 html 文件的,根标签会有 lang="en"
。
这样会导致打开网页的时候,浏览器的右上角会蹦出一个翻译英文提示。
翻译弹窗
源文件 lang=“en”
如何解决
修改设置
在 VsCode 设置界面,给 emmet
代码片段添加变量,设置 lang
的值为 zh-CN
。
测试成果
变量设置生效 lang="zh-CN"
思考于争议
- 曾经因为简体中文的 lang 设置为什么有过激烈的讨论,这种在圈子内对技术的严谨追求和探讨其实是极好的。
- 其实描述“中文 (简体, 中国大陆)”用
zh-CN
即可。
网站 | 网址 | lang 值 |
---|---|---|
淘宝 | https://www.taobao.com/ | lang=“zh-CN” |
腾讯网 | https://www.qq.com/ | lang=“zh-CN” |
腾讯 alloyteam | http://www.alloyteam.com/ | lang=“zh-cn” |
Bilibili | https://www.bilibili.com/ | lang=“zh-CN” |
apifox | https://www.apifox.cn/ | lang=“zh-CN” |
uniapp | https://uniapp.dcloud.net.cn | lang=“zh-CN” |
小米 | https://www.mi.com/ | lang=“zh-CN” |
博客园 | https://www.cnblogs.com/ | lang=“zh-cn” |
微软(中国大陆) | https://www.microsoft.com/zh-cn/ | lang=“zh-CN” |
苹果(中国大陆) | https://www.apple.com.cn/ | lang=“zh-CN” |
谷歌(简体中文) | https://www.google.com.hk/ | lang=“zh-CN” |
VueJS(简体中文) | https://cn.vuejs.org/ | lang=“zh-CN” |
百度 | https://www.baidu.com/ | 无 |
京东 | https://www.jd.com | 无 |
网易 | https://www.163.com/ | 无 |
微信 | https://weixin.qq.com/ | 无 |
抖音 | https://www.douyin.com/ | 无 |
黑马程序员 | https://www.itheima.com/ | 无 |
传智播客 | https://www.itcast.cn/ | 无 |
掘金 | https://juejin.cn/ | lang=“zh” |
CSDN | https://www.csdn.net/ | lang=“zh” |
知乎 | https://www.zhihu.com/ | lang=“zh” |
Facebook(简体中文) | https://www.facebook.com/ | lang=“zh-Hans” |
React(简体中文) | https://zh-hans.reactjs.org/ | lang=“zh-Hans” |
YouTube(简体中文) | https://www.youtube.com | lang=“zh-Hans-CN” |
维基百科(简体中文) | https://zh.wikipedia.org/ | lang=“zh-Hans-CN” |
豆瓣 | https://www.douban.com | lang=“zh-cmn-Hans” |
为什么列这个表格
知乎讨论:https://www.zhihu.com/question/20797118
W3C官网: https://www.w3.org/International/articles/language-tags/
PS:考虑到大家不会主动打开 W3C 链接,所以我截图标记出 W3C 对 zh-CN
部分描述。
结论
描述“中文 (简体, 中国大陆)” 用 zh-CN
即可
而且也是最多国内外大厂采用的方式,所有浏览器都能准确识别,通俗易懂,推荐使用。
补充
-
其实模糊设置为
zh
或者精确设置为zh-Hans
、zh-Hans-CN
、zh-cmn-Hans
,其实浏览器都能识别成中文。 -
如果不设置
lang
,让浏览器自动推导语言也是可以的。 -
中文体系是复杂的,但标准是为开发者服务的,同时浏览器也是智能的。其实设置为
zh-CN
、zh-Hans
、zh-Hans-CN
、zh-cmn-Hans
还是zh
浏览器都能识别成中文。 -
不要再使用默认的
lang="en"
即可,否则浏览会出现翻译弹窗。