javascript - 我可以在 i18next 的(异步)初始化之前使用 i18next-browser-languageDetector
问题描述
我正在使用i18next
并且i18next-browser-languageDetector
喜欢这样:
await (i18next
.use(initReactI18next)
.use(LanguageDetector)
.init({
resources,
fallbackLng: process.env.DEV ? 'dev' : DEFAULT_LANGUAGE,
debug: Boolean(process.env.DEV),
returnObjects: true,
interpolation: {
escapeValue: false,
},
detection: {
// Order and from where user language should be detected:
order: ['localStorage', 'cookie', 'navigator'],
// Keys or params to lookup language from:
lookupLocalStorage: I18N_LANGUAGE_KEY,
lookupCookie: I18N_COOKIE_KEY,
// Cache user language on:
caches: ['localStorage', 'cookie'],
// Only detect languages that are in the whitelist:
checkWhitelist: true,
},
}));
现在检测是在i18next
库的初始化阶段进行的,这是异步的,所以如果我想检查已检测到的语言 ( i18next.language
),我需要等待它完成。
问题是我需要在i18next
加载资源之前检测语言,以便在向后端发出任何请求之前设置Accept-Language
标头。axios
我知道我可以只阅读localStorage
,cookies
或者navigator
我自己,但如果可能的话,我想使用已经内置的功能i18next-browser-languageDetector
。
解决方案
推荐阅读
- python - (Python) 计算最小移动计数
- android - 错误:EISDIR:对目录的非法操作,在 Object.readSync 处读取
- google-sheets - 谷歌表格自动填充公式
- flutter - Flutter:避免将`forEach`与函数文字一起使用
- python-3.x - 如何根据 Python 中的条件将一个数据框中的列值添加到第二个数据框中的另一列?
- android-studio - 消费者被配置为查找与 Java 11 兼容的库的运行时,打包为 jar,其依赖项在外部声明,但是:
- scala - 如何在 Scala 中使用将函数作为参数的方法?
- face-detection - 使用 open cv 进行人脸检测
- python - 尝试从脚本执行二进制文件时,pkexec“找不到这样的文件或目录”
- rust - 如何从 Rust 获得自动转换为特征?