All files / src/i18n translation.ts

91.67% Statements 11/12
87.5% Branches 7/8
75% Functions 3/4
91.67% Lines 11/12

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              23x                     120x 120x 120x   120x 87x 27x     120x 87x 27x       120x                    
/* eslint-disable @typescript-eslint/camelcase */
import { useEffect, useState } from "react";
import { zh_cn } from "./locale/zh_cn";
import { useConfig } from "../_util/config-context";
import { translationMap, Translation } from "./translationMap";
 
// 约定中语言名不规范
const lngs = {
  zh: "zh_cn",
  en: "en_us",
  jp: "ja",
};
 
export function setLocale() {
  console.error("`setLocale()` 方法已废弃");
}
 
export function useTranslation(moment?: typeof import("moment")): Translation {
  const { locale: configLocale } = useConfig();
  const t = translationMap[configLocale] || zh_cn;
  const locale = lngs[t.locale] || t.locale;
 
  useState(() => {
    if (moment) {
      moment.locale(locale);
    }
  });
  useEffect(() => {
    if (moment) {
      moment.locale(locale);
    }
  }, [locale]); // eslint-disable-line react-hooks/exhaustive-deps
 
  return t;
}
 
export interface WithTranslationProps {
  /**
   * @ignore
   */
  t?: Translation;
}
/* eslint-enable @typescript-eslint/camelcase */