feat: add dayjs i18n

pull/13/head
moonrailgun 4 years ago
parent b78335d24b
commit b8d0969c61

@ -8,6 +8,11 @@ import { languageDetector } from './language';
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import HttpApi from 'i18next-http-backend'; // https://github.com/i18next/i18next-http-backend import HttpApi from 'i18next-http-backend'; // https://github.com/i18next/i18next-http-backend
/**
*
*/
type AllowedLanguage = 'zh-CN' | 'en-US';
i18next i18next
.use(languageDetector) .use(languageDetector)
.use(HttpApi) .use(HttpApi)
@ -56,7 +61,7 @@ export const t: TFunction = (
/** /**
* i18next * i18next
*/ */
export async function setLanguage(lang: string): Promise<void> { export async function setLanguage(lang: AllowedLanguage): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
i18next.changeLanguage(lang, (err) => { i18next.changeLanguage(lang, (err) => {
if (err) { if (err) {
@ -68,6 +73,13 @@ export async function setLanguage(lang: string): Promise<void> {
}); });
} }
/**
*
*/
export function onLanguageChange(cb: (lang: string) => void) {
i18next.on('languageChanged', cb);
}
/** /**
* fork from i18next/react-i18next/-/blob/src/useTranslation.js * fork from i18next/react-i18next/-/blob/src/useTranslation.js
* i18n for react 使hooks * i18n for react 使hooks

@ -1,13 +1,22 @@
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime'; // 导入插件 import relativeTime from 'dayjs/plugin/relativeTime'; // 导入插件
import 'dayjs/locale/zh-cn'; // 导入本地化语言 import 'dayjs/locale/zh-cn'; // 导入本地化语言
import { onLanguageChange } from '../i18n';
/** /**
* Reference: https://day.js.org/ * Reference: https://day.js.org/
*/ */
dayjs.extend(relativeTime); dayjs.extend(relativeTime);
dayjs.locale('zh-cn'); dayjs.locale('zh-cn'); // 默认使用中文
onLanguageChange((lang) => {
if (lang === 'en-US') {
dayjs.locale('en');
return;
}
dayjs.locale('zh-cn');
});
/** /**
* *

Loading…
Cancel
Save