All files / src/utils localization.ts

86.95% Statements 20/23
66.66% Branches 2/3
50% Functions 1/2
90% Lines 18/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 584x 4x 4x   4x 4x         4x                         4x                 4x 2x   1x 1x 1x 1x   1x 1x 1x                 4x         4x  
import i18n from 'i18next';
import { toast } from 'react-toastify';
import * as Yup from 'yup';
 
import { locale as enUsYup } from '@/locales/en-US.yup';
import { locale as jaJpYup } from '@/locales/js-JP.yup';
 
/**
 * ${language}-${region} の形式で定義されるロケールコードを表します。
 */
export const localeCodes = {
  jaJp: 'ja-JP',
  enUs: 'en-US',
} as const;
 
/**
 * ${language}-${region} の形式で定義されるロケールコードを表します。
 */
export type LocaleCodes = (typeof localeCodes)[keyof typeof localeCodes];
 
/**
 * ロケール名を表します。
 */
export const localeNames = {
  [`${localeCodes.jaJp}`]: '日本語',
  [`${localeCodes.enUs}`]: 'English',
} as const;
 
/**
 * 多言語表示に使用されるロケール情報を変更します。
 * @param localeCode ロケールコード
 */
export const changeLocale = (localeCode: LocaleCodes) => {
  switch (localeCode) {
    case localeCodes.enUs:
      i18n.changeLanguage(localeCode);
      Yup.setLocale(enUsYup);
      toast.info('The text is generated by machine translation.');
      break;
    default:
      i18n.changeLanguage(localeCodes.jaJp);
      Yup.setLocale(jaJpYup);
      break;
  }
};
 
/**
 * ロケール情報を初期化します。
 * 事前に i18next の初期化を完了させておく必要があります。
 * @param localeCode ロケールコード
 */
export const initLocale = (localeCode: LocaleCodes) => {
  Iif (!i18n.isInitialized) throw Error('Not initialized i18next. Please call i18n.use() before calling this function.');
  changeLocale(localeCode);
};
 
export default Yup;