使用国际化重写(“i18n 重写”)根据用户的国家或首选语言提供不同的内容。以下是您可以设置的一些示例配置:
为喜欢法语的所有用户(无论国家/地区)提供相同的法语内容。
示例:带有法语文本的主页向喜欢法语的用户提供标准法语内容,但为喜欢法语的加拿大用户提供加拿大法语内容。
示例:使用标准法语措辞的主页与使用加拿大法语措辞的主页向所有加拿大用户提供相同的内容(无论他们的语言偏好如何)。
示例:主页采用您网站的“默认”语言,但具有加拿大特有的功能(如假日主题)为偏爱法语的加拿大用户提供加拿大法语内容。
示例:带有加拿大法语措辞和加拿大特定功能(如假日主题)的主页
Firebase 托管根据用户的 IP 地址确定用户所在的国家/地区,并根据Accept-Language
请求标头(通常由其网络浏览器自动设置)确定用户的语言偏好。
设置国际化重写
要为您的托管站点设置 i18n 重写,您需要为所有本地化内容创建一个“i18n 内容”目录,然后将i18n
属性添加到您的firebase.json
文件以指向新的“i18n 内容”目录。
以下是详细步骤:
在本地应用程序目录的
public
文件夹中,为您的“i18n 内容”创建一个单独的目录,然后为您的站点支持的每种语言和国家/地区组合创建子文件夹。在每个子文件夹中,添加特定于该组合的内容,例如假日主题主页或特定语言的 404 页面。
这是一个名为
localized-files
的示例“i18n 内容”目录:public/ index.html // your site's default homepage 404.html // your site's custom 404 page localized-files/ ALL_ca/ index.html es_ALL/ index.html 404.html fr/ index.html 404.html fr_ca/ index.html
public/ // matches requests that aren't specified by your "i18n content" subfolders // example: display your homepage in the "default" language for your site with no country-specific features index.html // your site's default homepage 404.html // your site's custom 404 page localized-files/ // matches requests from Canada with any language preference // example: display your homepage in the "default" language for your site with a Canada-specific feature ALL_ca/ index.html // matches requests from any country with a language preference of `es` or `es-foo` // example: display your homepage in Spanish with no country-specific features es_ALL/ index.html 404.html // your site's custom 404 page in Spanish // matches requests from any country with a language preference of `fr` or `fr-foo` // example: display your homepage in Standard French with no country-specific features fr/ index.html 404.html // your site's custom 404 page in French // matches requests from Canada with a language preference of `fr` or `fr-foo` // example: display your homepage in Canadian French and/or with a Canada-specific feature fr_ca/ index.html
localized-files/
目录包含您站点支持的每种语言和国家/地区组合的单独子文件夹。每个子文件夹的命名模式必须遵循以下格式之一:languageCode_countryCode
:包含特定于具有该语言偏好和国家代码的用户的内容languageCode
:包含特定于具有该语言偏好的用户的内容,但内容不是特定于国家/地区的;基本上等同于languageCode_ALL
有关这些代码的更多详细信息,请参阅下面的国家和语言代码小节。您可以使用
ALL
的值(区分大小写)来指示任何国家(如es_ALL/
)或任何语言(如ALL_ca/
)。子文件夹中的文件不需要在
public
目录或其他子文件夹中有类似的文件。您可以创建完全特定于一种语言和/或国家/地区的内容。将
i18n
属性添加到您的firebase.json
文件并指定包含您的“i18n 内容”的目录。继续我们的例子:// firebase.json "hosting": { "public": "public", "ignore": [ "firebase.json", "**/.*", "**/node_modules/**" ], "i18n": { "root": "/localized-files" // directory that contains your "i18n content" } ... }
为
root
指定的目录必须是包含所有“i18n 内容”子文件夹的目录的名称。如果您将所有“i18n 内容”子文件夹放在public
目录的根目录下,请使用/
作为root
的值。root
值中的前导斜杠和尾随斜杠是可选的。将您的“i18n 内容”和配置部署到您的托管站点。
您可以使用cookie overrides测试您的设置。
国家和语言代码
命名“i18n 内容”子文件夹时,国家和语言代码都必须使用小写字母。您可以使用ALL
的值(区分大小写)来指示任何国家(如es_ALL/
)或任何语言(如ALL_ca/
)。
Hosting 从用户的 IP 地址中获取国家代码。国家/地区代码是两个字母的ISO 3166-1 alpha-2 代码。
语言代码是从用户的Accept-Language
请求标头中获取的(通常由他们的网络浏览器自动设置)。它们是ISO 639-1 代码。使用语言代码时请记住以下几点:
当 Hosting 搜索要提供的“i18n 内容”时,它会根据
Accept-Language
标头中的质量值对语言进行排序。托管会在
Accept-Language
标头中删除任何区域和国家/地区子标签,因此“i18n 内容”子文件夹名称中的语言代码不能包含这些子标签。例如,您不能使用es-419
或es-US
作为子文件夹名称中的语言代码,但可以使用es
。如果您想提供特定区域或国家的内容,您可以创建包含您要支持的特定语言国家/地区内容的子文件夹。
在此示例中,来自西班牙且语言首选项为
es
、es-es
甚至es-419
的请求将从es_es/
子文件夹接收内容,因为 Hosting 将所有这些语言代码视为es
。来自美国、墨西哥或任何其他语言首选项为
es-419
的国家/地区的请求将从es_ALL/
子文件夹接收内容,因为 Hosting 将es-419
视为es
。public/ // matches requests that aren't specified by your "i18n content" subfolders index.html // the site's default homepage localized-files/ // matches requests from Spain with a language preference of `es` or `es-foo` es_es/ index.html // matches requests from any other country with a language preference of `es` or `es-foo` es_ALL/ index.html
在此示例中,来自墨西哥且语言首选项为
es-419
的请求将从es_mx/
子文件夹接收内容,因为 Hosting 将语言代码es-419
视为es
。但是,来自美国且语言首选项为
es-419
的请求将从es_ALL/
子文件夹接收内容,因为 Hosting 将es-419
视为es
并且没有es_us/
子文件夹。public/ // matches requests that aren't specified by your "i18n content" subfolders index.html // the site's default homepage localized-files/ // matches requests from Argentina with a language preference of `es` or `es-foo` (mimics behavior of `es-ar` header tag) es_ar/ index.html // matches requests from Spain with a language preference of `es` or `es-foo` (mimics behavior of `es-es` header tag) es_es/ index.html // matches requests from Mexico with a language preference of `es` or `es-foo` (mimics behavior of `es-mx` header tag) es_mx/ index.html // matches requests from any other country with a language preference of `es` or `es-foo` (mimics behavior of `es-419` header tag) es_ALL/ index.html
“i18n 内容”的优先顺序
如果您设置 i18n 重写,Hosting 会根据以下优先顺序提供内容:
以
/__/*
路径段开头的保留命名空间配置的重定向
完全匹配的静态内容
语言代码 + 国家代码(例如,来自
fr_ca/
的内容)
该顺序遵循请求的Accept-Language
标头中每种语言的质量值。仅国家代码(例如,来自
ALL_ca/
的内容)仅语言代码(例如,来自
fr/
或es_ALL/
的内容)
该顺序遵循请求的Accept-Language
标头中每种语言的质量值。“默认”完全匹配静态内容
这是“i18n 内容”目录之外的内容,例如public
目录的根目录。
配置重写
404处理
国际化 404 页
这遵循上面列出的完全匹配静态内容的相同优先级顺序。自定义404页面
默认 404 页面(由 Firebase 提供)
优先顺序示例
让我们从上面继续我们的例子。我们将使用相同的示例目录和示例请求。
带有“i18n content”目录的示例本地项目目录(称为
localized-files
)public/ index.html // your site's default homepage 404.html // your site's custom 404 page localized-files/ ALL_ca/ index.html es_ALL/ index.html 404.html fr/ index.html 404.html fr_ca/ index.html
示例请求信息
语言代码:
fr
,en
(法语,然后是英语)
语言代码根据Accept-Language
标头中的质量值进行排序。国家代码:
ca
(加拿大)
根据完全匹配的优先顺序和语言首选项的质量值,Hosting 将按以下顺序在目录中搜索请求的页面。
public/localized-files/fr_ca/
public/localized-files/en_ca/
public/localized-files/ALL_ca/
public/localized-files/fr_ALL/
public/localized-files/fr/
public/localized-files/en_ALL/
public/localized-files/en/
public/
404处理
哪个页面将提供给用户?
请求页面:
index.html
来自
fr_ca/
子文件夹的index.html
由于 Hosting 首先搜索
fr_ca/
子文件夹,它会在该子文件夹中找到与index.html
完全匹配的文件。请求页面:
awesome-page.html
来自
fr/
子文件夹的404.html
托管首先按优先顺序搜索整个目录(包括所有“i18n 内容”子文件夹和根目录)以进行完全匹配,但没有完全匹配
awesome-page.html
。因此,Hosting 将开始其 404 处理,它遵循与完全匹配搜索相同的 i18n 优先级顺序。
fr/
子文件夹是搜索到的第一个包含 404 页面的子文件夹。
请注意以下关于“i18n 内容”目录的搜索和服务:
localized-files/
目录实际上并不包含en_ca/
、en_ALL/
或en/
子文件夹,因此 Hosting 将跳过优先级列表,直到找到请求的语言-国家/地区组合的匹配子文件夹。即使
localized-files/
目录包含es_ALL/
子文件夹,上面的示例请求不包含es
或es-foo
语言代码,因此 Hosting 不会搜索与es
匹配的“i18n 内容”。从用户的国家和语言偏好的角度来看,名为
fr/
和fr_ALL/
的子文件夹是等效的。但是,如果两个子文件夹都存在,Hosting 将在fr/
content 之前提供fr_ALL/
content。
用 cookie 覆盖语言和国家代码
您可以通过使用 cookie 覆盖国家和语言标头来更改提供的内容。
以下是您可以使用 cookie 覆盖的一些方法:
使用不同的语言/国家/地区组合测试功能以检查提供了哪些内容。
使您的用户能够更改他们看到的内容。例如,您可以实施语言选择器,然后相应地设置用户的
firebase-language-override
cookie。
要配置 cookie 覆盖,请使用以下两个名称或其中一个名称设置 cookie: firebase-country-override
和firebase-language-override
。例如,以下 JavaScript 代码片段将国家代码覆盖为ca
并将Accept-Language
标头覆盖为fr,en
:
document.cookie = "firebase-country-override=ca";
document.cookie = "firebase-language-override=fr,en";
语言 cookie 覆盖必须是按偏好顺序排列的以逗号分隔的语言代码列表,没有子标签或质量值。
Cookie 覆盖不会反映在日志中。