React i18n 自定义语言检测器不起作用

Psy*_*omp 5 internationalization react-i18next

下面是我的代码,

我不知道为什么 console.log('are you running?') 没有显示,

我的代码有什么问题吗?

我尝试添加自定义语言检测器..

import i18n, { LanguageDetectorModule } from 'i18next';

import { initReactI18next } from 'react-i18next';
import resources from './i18n_resources.json';
import { isLocalhost } from '@/common/detect_utils';
import { detectLanguage } from '.';

const languageDetector: LanguageDetectorModule = {
  type: 'languageDetector',
  detect: () => {
    console.log('are you running?');
    return 'en';
  },
  init: () => {},
  cacheUserLanguage: () => {},
};

i18n
  .use(initReactI18next)
  .use(languageDetector)
  .init({
    resources,
    lng: 'ko',
    fallbackLng: 'en',
    keySeparator: false,
    debug: isLocalhost,
    interpolation: {
      escapeValue: false,
    },
  });

export default i18n;
Run Code Online (Sandbox Code Playgroud)

adr*_*rai 4

如果要使用语言检测,则不应设置 lng 选项。此示例有效:https://codesandbox.io/s/react-i18next-http-example-forked-655ik ?file=/src/i18n.js

更多信息:https ://www.i18next.com/overview/configuration-options#languages-namespaces-resources 在此输入图像描述

  • @adrai 当我使用后端(i18next-http-backend)和语言检测器时它不起作用 (2认同)