1
import i18n from 'i18next';
2
import LanguageDetector from 'i18next-browser-languagedetector';
3
import resourcesToBackend from 'i18next-resources-to-backend';
4
import { initReactI18next } from 'react-i18next';
5
import { isRtlLang } from 'rtl-detect';
7
import { getDebugConfig } from '@/config/debug';
8
import { DEFAULT_LANG, LOBE_LOCALE_COOKIE } from '@/const/locale';
9
import { COOKIE_CACHE_DAYS } from '@/const/settings';
10
import { normalizeLocale } from '@/locales/resources';
11
import { isDev, isOnServerSide } from '@/utils/env';
13
const { I18N_DEBUG, I18N_DEBUG_BROWSER, I18N_DEBUG_SERVER } = getDebugConfig();
14
const debugMode = (I18N_DEBUG ?? isOnServerSide) ? I18N_DEBUG_SERVER : I18N_DEBUG_BROWSER;
16
export const createI18nNext = (lang?: string) => {
18
.use(initReactI18next)
19
.use(LanguageDetector)
21
resourcesToBackend(async (lng: string, ns: string) => {
22
if (isDev && lng === 'zh-CN') return import(`./default/${ns}`);
24
return import(`@/../locales/${normalizeLocale(lng)}/${ns}.json`);
27
// Dynamically set HTML direction on language change
28
instance.on('languageChanged', (lng) => {
29
if (typeof window !== 'undefined') {
30
const direction = isRtlLang(lng) ? 'rtl' : 'ltr';
31
document.documentElement.dir = direction;
38
defaultNS: ['error', 'common', 'chat'],
41
cookieMinutes: 60 * 24 * COOKIE_CACHE_DAYS,
43
Set `sameSite` to `lax` so that the i18n cookie can be passed to the
44
server side when returning from the OAuth authorization website.
45
ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value
46
discussion: https://github.com/lobehub/lobe-chat/pull/1474
51
lookupCookie: LOBE_LOCALE_COOKIE,
53
fallbackLng: DEFAULT_LANG,