我正在使用 React、i18next
和 i18next-browser-languagedetector
开发多语言应用程序。
我按以下方式初始化 i18next:
i18n
.use(LanguageDetector)
.init({
lng: localStorage.getItem(I18N_LANGUAGE) || "pt",
fallbackLng: "pt",
resources: {
en: stringsEn,
pt: stringsPt
},
detection: {
order: ["localStorage", "navigator"],
lookupQuerystring: "lng",
lookupLocalStorage: I18N_LANGUAGE,
caches: ["localStorage"]
}
});
export default i18n;
我已经实现了一个语言选择器,它只是将 localStorage
中的值更改为用户选择的值。
这是正确的做法吗?
我问是因为即使这有效,我觉得我通过设置 localStorage.getItem(I18N_LANGUAGE) || 是在“作弊” “pt”
并且我没有按应有的方式使用语言检测。
最佳答案
根据documentation ,您不需要自己指定语言:
import i18next from 'i18next';
import LngDetector from 'i18next-browser-languagedetector';
i18next
.use(LngDetector)
.init({
detection: options
});
并根据this piece of source在i18next
中,确实使用了插件的检测能力:
if (!lng && this.services.languageDetector) lng = this.services.languageDetector.detect();
Is this the correct way of doing it?
所以,不,不是。让插件完成它的工作。 :)
关于javascript - React i18next 和更改语言的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35728632/