React + Typescript에 i18n 적용

i18nextreact-i18next를 설치해준다.

npm i i18next react-i18next

src/config 폴더에 lang 폴더를 만들고 i18n.ts react-i18next.d.ts 파일과 locales 폴더를 만들어준다.

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

import ko from './locales/ko';
import en from './locales/en';

export const resources = {
  ko: {
    translation: ko,
  },
  en: {
    translation: en,
  },
};

i18n.use(initReactI18next).init({
  resources,
  lng: 'ko',
  fallbackLng: 'ko',
  debug: true,
  keySeparator: '.', // default value
  interpolation: {
    escapeValue: false,
  },
  returnObjects: true,
});

export default i18n;
// import the original type declarations
import 'react-i18next';
// import all namespaces (for the default language, only)
import { resources } from './i18n';

declare module 'react-i18next' {
  type DefaultResources = typeof resources['ko'] & typeof resources['en'];
  interface Resources extends DefaultResources {}
}

locales 폴더 안에 언어별로 ts 파일을 생성해준다.

// ko 예시
const lang = {
  name: 'ZungTa',
};
export default lang;

index.tsx에서 i18n.ts 를 import 해준다.

...
import '@/config/lang/i18n';
...

이제 원하는 컴포넌트에서 useTranslation을 임포트해서 에디터의 지원을 받으며 편하게 사용할 수 있다.

...
import { useTranslation } from 'react-i18next';

function App() {
  const { t } = useTranslation();

  return (
    <div css={appCss}>
      Hello {t('name')}
    </div>
  );
}

export default App;

버전 기준

  • “react”: “^17.0.2”
  • “i18next”: “^20.2.2”
  • “react-i18next”: “^11.8.15”

ZungTa
Written by
ZungTa
ZungTa입니다.

GitHub