defineI18nLocaleDetector
defineI18nLocaleDetector() 是一个组合函数,用于定义一个在服务器端检测语言环境的函数,该函数会在服务器端对每个请求调用。
defineI18nLocaleDetector() 是一个组合函数,用于定义一个在服务器端检测语言环境的函数,该函数会在服务器端对每个请求调用。
该函数需要返回一个语言环境字符串。
你可以在语言环境检测函数中使用@intlify/h3 工具,这些工具会自动导入。
此组合函数为实验性功能。 你需要在
experimental.localeDetector 选项 中配置文件路径。类型
type LocaleConfig = {
defaultLocale: Locale
fallbackLocale: FallbackLocale
}
declare function defineI18nLocaleDetector(
detector: (event: H3Event, config: LocaleConfig) => string
): (event: H3Event, config: LocaleConfig) => string
参数
detector
一个作为语言环境检测器的函数,具有以下参数:
event- 类型:
H3Event - 一个 H3 事件。详情见 H3 API 文档
- 类型:
config- 类型:
object - 从 Nitro 传入的语言环境配置。
- 属性:
defaultLocale- 类型:
Locale - 该值设置为 Nuxt i18n 的
defaultLocale选项。如果未设置,则取自 Vue I18n 配置中的locale选项(即在vueI18n选项中设置的i18n.config文件)。如果两者均未设置,则默认值为'en-US'。
- 类型:
fallbackLocale- 类型:
FallbackLocale - 该值设置为从 Vue I18n 配置(
vueI18n选项中设置的i18n.config文件)加载的fallbackLocale选项。如果未配置回退语言环境,则默认值为false。
- 类型:
- 类型:
用法
下面是一个语言环境检测器的示例:
// 根据查询参数、cookie、请求头检测语言环境
export default defineI18nLocaleDetector((event, config) => {
const query = tryQueryLocale(event, { lang: '' })
if (query) {
return query.toString()
}
const cookie = tryCookieLocale(event, { lang: '', name: 'i18n_locale' })
if (cookie) {
return cookie.toString()
}
const header = tryHeaderLocale(event, { lang: '' })
if (header) {
return header.toString()
}
return config.defaultLocale
})