使用国际化重写(“ i18n重写”)来提供不同的内容,具体取决于用户所在的国家/地区或首选语言。这是您可以设置的一些示例配置:
向所有喜欢法语的用户提供相同的法语内容(无论哪个国家/地区)。
示例:带有法语文本的主页向喜欢法语的用户提供标准法语内容,但对于喜欢法语的加拿大用户,则投放加拿大法语内容。
示例:带有标准法语短语的主页与带有加拿大法语短语的主页向所有加拿大用户提供相同的内容(无论他们的语言偏好如何)。
示例:使用您网站的“默认”语言但具有加拿大特定功能(例如假日主题)的首页向喜欢法语的加拿大用户提供加拿大法语内容。
示例:带有加拿大法语短语和加拿大特定功能(如假日主题)的首页
Firebase托管从IP地址和“ Accept-Language
请求标头(通常由其网络浏览器自动设置)中确定用户的语言偏好,从而确定用户所在的国家/地区。
设置i18n重写
要为您的托管站点设置i18n重写,您需要为所有本地化的内容创建一个“ i18n内容”目录,然后将i18n
属性添加到firebase.json
文件中以指向新的“ i18n内容”目录。
详细步骤如下:
在您的本地应用程序目录的
public
文件夹,为您的“国际化内容”一个单独的目录,然后创建您的网站支持的每种语言和国家组合的子文件夹。在每个子文件夹中,添加特定于该组合的内容,例如以假日为主题的主页或特定于语言的404页。
这是一个名为
localized-files
的示例“ i18n content”目录: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
指定的root
必须是包含所有“ i18n内容”子文件夹的目录的名称。如果将所有“ i18n内容”子文件夹放在public
目录的根目录下,则将/
用作root
的值。root
值中的前斜杠和后斜杠是可选的。将您的“ i18n内容”部署并配置到您的托管站点。
您可以使用Cookie替代测试您的设置。
国家和语言代码
命名“ i18n内容”子文件夹时,国家和语言代码都必须使用小写字母。您可以使用ALL
的值(区分大小写)来表示任何国家(例如es_ALL/
)或任何语言(例如ALL_ca/
)。
主机从用户的IP地址获取国家/地区代码。国家/地区代码是两个字母的ISO 3166-1 alpha-2代码。
语言代码是从用户的“ Accept-Language
请求标头(通常由其网络浏览器自动设置)获得的。它们是ISO 639-1代码。使用语言代码时,请记住以下几点:
当Hosting搜索要提供的“ i18n内容”时,它将根据
Accept-Language
标头中的质量值对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/
子文件夹的内容,因为托管将语言代码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重写,则托管将根据以下优先级顺序提供内容:
以
/__/*
路径段开头的保留名称空间配置的重定向
完全匹配静态内容
语言代码+国家代码(例如
fr_ca/
内容)
该顺序遵循请求的“Accept-Language
标头中每种语言的质量值。仅国家/地区代码(例如,来自
ALL_ca/
内容)仅语言代码(例如,来自
fr/
或es_ALL/
)
该顺序遵循请求的“Accept-Language
标头中每种语言的质量值。“默认”完全匹配静态内容
这是“ i18n内容”目录外部的内容,例如在public
目录的根目录中。
配置的重写
404处理
i18n 404页面
这遵循上面列出的完全匹配静态内容的相同优先级顺序。自定义404页面
默认404页面(由Firebase提供)
优先顺序示例
让我们从上面继续我们的示例。我们将使用相同的示例目录和示例请求。
具有“ i18n内容”目录的示例本地项目目录(称为
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
(加拿大)
根据完全匹配优先级顺序和语言首选项的质量值,主机将按以下顺序在目录中搜索请求的页面。
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
没有完全匹配。因此,托管将开始其404处理,该处理遵循与完全匹配搜索相同的i18n优先级顺序。
fr/
子文件夹是搜索到的第一个包含404页的子文件夹。
请注意以下有关“ i18n内容”目录的搜索和服务的内容:
localized-files/
目录实际上并不包含en_ca/
,en_ALL/
或en/
子文件夹,因此Hosting只会跳过优先级列表,直到找到与请求的语言国家组合匹配的子文件夹。即使
localized-files/
目录包含es_ALL/
子文件夹,以上示例请求也不包含es
或es-foo
语言代码,因此Hosting不会搜索与es
匹配的“ i18n content”。从用户所在国家和语言偏好的角度来看,称为
fr/
和fr_ALL/
子文件夹是等效的。但是,如果两个子文件夹都存在,则主机将先提供fr_ALL/
内容,然后再提供fr/
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替代不会反映在日志中。