front-end-interview-handbook/packages/quiz/questions/how-do-you-serve-a-page-wit.../zh-CN.mdx

17 lines
1.2 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 你如何提供一个有多国语言内容的页面?
---
> 假设:问题是关于如何为一个有多种语言内容的页面提供服务,而页面内的内容应该只以一种一致的语言显示。
以不同语言提供页面是国际化i18n的一个方面。
当向服务器发出HTTP请求时请求的用户代理浏览器等通常发送有关语言首选项的信息例如在 `Accept-Language ` 标题中。 然后,服务器可以使用这些信息来返回一个适当语言版本的文件,如果有这样的替代品的话。 返回的HTML文档还应该声明`<html>`标签中的`lang`属性,例如`<html lang="en">...</html>`。
若要让搜索引擎知道不同语言的内容相同,应该使用带有`rel="alternate"` and `hreflang="..."` 属性的的 `<link>` 标签 。 例如, `<link rel="alternate" hreflang="de" href="http://de.example.com/page.html" />`.
## 渲染
- **服务器端渲染:** HTML标记将包含字符串占位符特定语言的内容将从代码或翻译服务的配置中获取。 服务器然后动态生成带有特定语言内容的 HTML 页面。
- **客户端渲染:** 将获取相应的地域字符串,并与 JavaScript 合并视图。