我目前从用户的浏览器中获取区域设置。现在,用户可以在其个人资料中设置其首选语言,这将存储在数据库中。我想从数据库中获取此值并为i18next
设置正确的区域设置。我在这里阅读了有关自己检测功能的一些内容:https://github.com/i18next/i18next-browser-languageDetector。但我不确定是否应该使用它。我的文件当前设置如下:
import i18n from 'i18next';
import Backend from 'i18next-xhr-backend';
import LanguageDetector from 'i18next-browser-languagedetector';
import { reactI18nextModule } from 'react-i18next';
import moment from 'moment';
export const i18nInit = (callback = () => { }) =>
// for easy overview use: https://devhints.io/moment its better than official docs:
moment.defineLocale('nl-custom', {
parentLocale: 'nl',
longDateFormat: {
LT: 'HH:mm',
LTS: 'HH:mm:ss',
L: 'D MMM', // need this format to display dates in past year(s)
LL: 'D MMMM YYYY', // need this format to display dates in the current year
LLL: 'YYYY-MM-DD HH:mm', // need this format as input for the date picker
LLLL: 'dddd D MMMM YYYY HH:mm',
},
}) &&
moment.defineLocale('en-custom', {
parentLocale: 'en',
longDateFormat: {
LT: 'HH:mm',
LTS: 'HH:mm:ss',
L: 'MMM D',
LL: 'MMMM D YYYY', // need this format to display dates in the current year
LLL: 'YYYY-MM-DD HH:mm', // need this format as input for the date picker
LLLL: 'MMMM dddd D YYYY HH:mm',
},
}) &&
i18n
// load translation using xhr -> see /public/locales
// learn more: https://github.com/i18next/i18next-xhr-backend
.use(Backend)
// detect user language
// learn more: https://github.com/i18next/i18next-browser-languageDetector
.use(LanguageDetector)
// pass the i18n instance to the react-i18next components.
// Alternative use the I18nextProvider: https://react.i18next.com/components/i18nextprovider
.use(reactI18nextModule)
// init i18next
// for all options read: https://www.i18next.com/overview/configuration-options
.init(
{
fallbackLng: 'en',
ns: ['actionpoints', 'common', 'menu', 'messages', 'overview', 'settings', 'shepherdTour', 'users', 'profile', 'meetingtypes'],
defaultNS: 'common',
whitelist: ['nl', 'en'],
backend: {
// Path where resources get loaded from, or a function
// returning a path:
// function(lngs, namespaces) { return customPath; }
// the returned path will interpolate lng, ns if provided like giving a static path
loadPath: '/locales/{{lng}}/{{ns}}.json',
},
load: 'currentOnly',
debug: false,
interpolation: {
escapeValue: false, // not needed for react as it escapes by default
},
// special options for react-i18next
// learn more: https://react.i18next.com/components/i18next-instance
react: {
wait: true,
},
},
callback
);
export default i18nInit();
在这里是否可以添加一个功能,从数据库中获取语言值,如果没有设置,则回退到浏览器的区域设置?