lobe-chat

Форк
0
/
create.ts 
61 строка · 2.1 Кб
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';
6

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';
12

13
const { I18N_DEBUG, I18N_DEBUG_BROWSER, I18N_DEBUG_SERVER } = getDebugConfig();
14
const debugMode = (I18N_DEBUG ?? isOnServerSide) ? I18N_DEBUG_SERVER : I18N_DEBUG_BROWSER;
15

16
export const createI18nNext = (lang?: string) => {
17
  const instance = i18n
18
    .use(initReactI18next)
19
    .use(LanguageDetector)
20
    .use(
21
      resourcesToBackend(async (lng: string, ns: string) => {
22
        if (isDev && lng === 'zh-CN') return import(`./default/${ns}`);
23

24
        return import(`@/../locales/${normalizeLocale(lng)}/${ns}.json`);
25
      }),
26
    );
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;
32
    }
33
  });
34
  return {
35
    init: () =>
36
      instance.init({
37
        debug: debugMode,
38
        defaultNS: ['error', 'common', 'chat'],
39
        detection: {
40
          caches: ['cookie'],
41
          cookieMinutes: 60 * 24 * COOKIE_CACHE_DAYS,
42
          /**
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
47
          */
48
          cookieOptions: {
49
            sameSite: 'lax',
50
          },
51
          lookupCookie: LOBE_LOCALE_COOKIE,
52
        },
53
        fallbackLng: DEFAULT_LANG,
54
        interpolation: {
55
          escapeValue: false,
56
        },
57
        lng: lang,
58
      }),
59
    instance,
60
  };
61
};
62

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.