From 90e890805b41db7520b70b4574a05ebdc897a754 Mon Sep 17 00:00:00 2001 From: baurine <2008.hbl@gmail.com> Date: Tue, 8 Sep 2020 18:17:22 +0800 Subject: [PATCH 1/7] fix dayjs i18n --- ui/lib/utils/i18n.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ui/lib/utils/i18n.ts b/ui/lib/utils/i18n.ts index 001504d06b..974ce1f26b 100644 --- a/ui/lib/utils/i18n.ts +++ b/ui/lib/utils/i18n.ts @@ -8,7 +8,11 @@ import { initReactI18next } from 'react-i18next' i18next.on('languageChanged', function (lng) { console.log('Language', lng) - dayjs.locale(lng.toLowerCase()) + if (lng.startsWith('zh')) { + dayjs.locale('zh-cn') + } else { + dayjs.locale('en') + } }) export function addTranslations(requireContext) { From ca9be18d8356be131814eceb38b33361e8c7eed6 Mon Sep 17 00:00:00 2001 From: baurine <2008.hbl@gmail.com> Date: Thu, 17 Sep 2020 11:55:19 +0800 Subject: [PATCH 2/7] refine --- ui/lib/utils/i18n.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/ui/lib/utils/i18n.ts b/ui/lib/utils/i18n.ts index 974ce1f26b..f59dae802d 100644 --- a/ui/lib/utils/i18n.ts +++ b/ui/lib/utils/i18n.ts @@ -1,18 +1,14 @@ -import 'dayjs/locale/en' -import 'dayjs/locale/zh-cn' +import zh from 'dayjs/locale/zh-cn' import dayjs from 'dayjs' import i18next from 'i18next' import LanguageDetector from 'i18next-browser-languagedetector' import { initReactI18next } from 'react-i18next' +const DAYJS_LOCALES = { zh } + i18next.on('languageChanged', function (lng) { - console.log('Language', lng) - if (lng.startsWith('zh')) { - dayjs.locale('zh-cn') - } else { - dayjs.locale('en') - } + dayjs.locale(DAYJS_LOCALES[lng.toLocaleLowerCase()] || 'en') }) export function addTranslations(requireContext) { From bbcb94f5ce43de435c87a3fc50049a3fd391de57 Mon Sep 17 00:00:00 2001 From: baurine <2008.hbl@gmail.com> Date: Thu, 17 Sep 2020 15:20:30 +0800 Subject: [PATCH 3/7] refine --- .../components/DiagnosisReport.tsx | 4 +-- ui/lib/apps/UserProfile/index.tsx | 4 +-- ui/lib/components/LanguageDropdown/index.tsx | 4 +-- ui/lib/utils/i18n.ts | 26 ++++++++++++++----- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/ui/diagnoseReportApp/components/DiagnosisReport.tsx b/ui/diagnoseReportApp/components/DiagnosisReport.tsx index bf0bb5b03f..2591f6a55a 100644 --- a/ui/diagnoseReportApp/components/DiagnosisReport.tsx +++ b/ui/diagnoseReportApp/components/DiagnosisReport.tsx @@ -2,7 +2,7 @@ import React, { useState } from 'react' import { useTranslation } from 'react-i18next' import DiagnosisTable from './DiagnosisTable' import { ExpandContext, TableDef } from '../types' -import { ALL_LANGUAGES } from '@lib/utils/i18n' +import { ALL_LANGUAGES, getEffetiveLang } from '@lib/utils/i18n' function LangDropdown() { const { i18n } = useTranslation() @@ -10,7 +10,7 @@ function LangDropdown() {
{_.map(ALL_LANGUAGES, (name, key) => { diff --git a/ui/lib/components/LanguageDropdown/index.tsx b/ui/lib/components/LanguageDropdown/index.tsx index 72686651b1..967a9648d5 100644 --- a/ui/lib/components/LanguageDropdown/index.tsx +++ b/ui/lib/components/LanguageDropdown/index.tsx @@ -3,7 +3,7 @@ import _ from 'lodash' import React, { ReactNode } from 'react' import { useTranslation } from 'react-i18next' -import { ALL_LANGUAGES } from '@lib/utils/i18n' +import { ALL_LANGUAGES, getEffetiveLang } from '@lib/utils/i18n' function LanguageDropdown({ children }: { children: ReactNode }) { const { i18n } = useTranslation() @@ -13,7 +13,7 @@ function LanguageDropdown({ children }: { children: ReactNode }) { } const menu = ( - + {_.map(ALL_LANGUAGES, (name, key) => { return {name} })} diff --git a/ui/lib/utils/i18n.ts b/ui/lib/utils/i18n.ts index f59dae802d..3745783c0f 100644 --- a/ui/lib/utils/i18n.ts +++ b/ui/lib/utils/i18n.ts @@ -1,14 +1,12 @@ -import zh from 'dayjs/locale/zh-cn' +import 'dayjs/locale/zh-cn' import dayjs from 'dayjs' import i18next from 'i18next' import LanguageDetector from 'i18next-browser-languagedetector' import { initReactI18next } from 'react-i18next' -const DAYJS_LOCALES = { zh } - i18next.on('languageChanged', function (lng) { - dayjs.locale(DAYJS_LOCALES[lng.toLocaleLowerCase()] || 'en') + dayjs.locale(lng.toLocaleLowerCase()) }) export function addTranslations(requireContext) { @@ -37,18 +35,32 @@ export function addTranslationResource(lang, translations) { } export const ALL_LANGUAGES = { - zh: '简体中文', + 'zh-CN': '简体中文', en: 'English', } +export function getEffetiveLang(): string { + const effetiveLangs = Object.keys(ALL_LANGUAGES) + const detectedLang = i18next.language + if (effetiveLangs.includes(detectedLang)) { + return detectedLang + } + if (detectedLang.startsWith('zh')) { + return 'zh-CN' + } + return 'en' +} + i18next .use(LanguageDetector) .use(initReactI18next) .init({ resources: {}, // oh! this line is a big pitfall, we can't remove it, else it will cause strange crash! - fallbackLng: 'en', - whitelist: ['zh', 'en'], + fallbackLng: 'en', // fallbackLng won't change the detected language interpolation: { escapeValue: false, }, }) + +// init dayjs locale +dayjs.locale(getEffetiveLang().toLocaleLowerCase()) From 7fb8c2c29162639cdec832dad54423f30a6e053c Mon Sep 17 00:00:00 2001 From: baurine <2008.hbl@gmail.com> Date: Thu, 17 Sep 2020 15:25:41 +0800 Subject: [PATCH 4/7] fix --- ui/lib/utils/i18n.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ui/lib/utils/i18n.ts b/ui/lib/utils/i18n.ts index 3745783c0f..6dc25b5115 100644 --- a/ui/lib/utils/i18n.ts +++ b/ui/lib/utils/i18n.ts @@ -6,7 +6,7 @@ import LanguageDetector from 'i18next-browser-languagedetector' import { initReactI18next } from 'react-i18next' i18next.on('languageChanged', function (lng) { - dayjs.locale(lng.toLocaleLowerCase()) + dayjs.locale(lng.toLowerCase()) }) export function addTranslations(requireContext) { @@ -63,4 +63,4 @@ i18next }) // init dayjs locale -dayjs.locale(getEffetiveLang().toLocaleLowerCase()) +dayjs.locale(getEffetiveLang().toLowerCase()) From fdde546b53ae441e028e365c073be85db0664f58 Mon Sep 17 00:00:00 2001 From: baurine <2008.hbl@gmail.com> Date: Thu, 17 Sep 2020 15:45:05 +0800 Subject: [PATCH 5/7] fix --- ui/diagnoseReportApp/components/DiagnosisReport.tsx | 4 ++-- ui/lib/apps/UserProfile/index.tsx | 7 +++++-- ui/lib/components/LanguageDropdown/index.tsx | 4 ++-- ui/lib/utils/i18n.ts | 4 ++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/ui/diagnoseReportApp/components/DiagnosisReport.tsx b/ui/diagnoseReportApp/components/DiagnosisReport.tsx index 2591f6a55a..612bdf3881 100644 --- a/ui/diagnoseReportApp/components/DiagnosisReport.tsx +++ b/ui/diagnoseReportApp/components/DiagnosisReport.tsx @@ -2,7 +2,7 @@ import React, { useState } from 'react' import { useTranslation } from 'react-i18next' import DiagnosisTable from './DiagnosisTable' import { ExpandContext, TableDef } from '../types' -import { ALL_LANGUAGES, getEffetiveLang } from '@lib/utils/i18n' +import { ALL_LANGUAGES, getEffectiveLang } from '@lib/utils/i18n' function LangDropdown() { const { i18n } = useTranslation() @@ -10,7 +10,7 @@ function LangDropdown() {
{_.map(ALL_LANGUAGES, (name, key) => { diff --git a/ui/lib/components/LanguageDropdown/index.tsx b/ui/lib/components/LanguageDropdown/index.tsx index 967a9648d5..d4c1d22bad 100644 --- a/ui/lib/components/LanguageDropdown/index.tsx +++ b/ui/lib/components/LanguageDropdown/index.tsx @@ -3,7 +3,7 @@ import _ from 'lodash' import React, { ReactNode } from 'react' import { useTranslation } from 'react-i18next' -import { ALL_LANGUAGES, getEffetiveLang } from '@lib/utils/i18n' +import { ALL_LANGUAGES, getEffectiveLang } from '@lib/utils/i18n' function LanguageDropdown({ children }: { children: ReactNode }) { const { i18n } = useTranslation() @@ -13,7 +13,7 @@ function LanguageDropdown({ children }: { children: ReactNode }) { } const menu = ( - + {_.map(ALL_LANGUAGES, (name, key) => { return {name} })} diff --git a/ui/lib/utils/i18n.ts b/ui/lib/utils/i18n.ts index 6dc25b5115..0ecc01c9fa 100644 --- a/ui/lib/utils/i18n.ts +++ b/ui/lib/utils/i18n.ts @@ -39,7 +39,7 @@ export const ALL_LANGUAGES = { en: 'English', } -export function getEffetiveLang(): string { +export function getEffectiveLang(): string { const effetiveLangs = Object.keys(ALL_LANGUAGES) const detectedLang = i18next.language if (effetiveLangs.includes(detectedLang)) { @@ -63,4 +63,4 @@ i18next }) // init dayjs locale -dayjs.locale(getEffetiveLang().toLowerCase()) +dayjs.locale(getEffectiveLang().toLowerCase()) From bec06e23a13b06c802e6137ebe687a1aa70eff7b Mon Sep 17 00:00:00 2001 From: baurine <2008.hbl@gmail.com> Date: Thu, 17 Sep 2020 17:17:37 +0800 Subject: [PATCH 6/7] refine --- ui/lib/components/Root/index.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ui/lib/components/Root/index.tsx b/ui/lib/components/Root/index.tsx index b5aa232050..9499c00f4d 100644 --- a/ui/lib/components/Root/index.tsx +++ b/ui/lib/components/Root/index.tsx @@ -9,10 +9,11 @@ import { createTheme, registerIcons } from 'office-ui-fabric-react/lib/Styling' import { Customizations } from 'office-ui-fabric-react/lib/Utilities' import { ConfigProvider } from 'antd' -import i18next from 'i18next' import enUS from 'antd/es/locale/en_US' import zhCN from 'antd/es/locale/zh_CN' +import { getEffectiveLang } from '@lib/utils/i18n' + registerIcons({ icons: { SortUp: , @@ -30,7 +31,7 @@ Customizations.applySettings({ theme }) export default function Root({ children }) { return ( - + {children} ) From 328abbe5ed55fd4d6eb35981364f1d83ba7c0956 Mon Sep 17 00:00:00 2001 From: baurine <2008.hbl@gmail.com> Date: Fri, 18 Sep 2020 10:32:00 +0800 Subject: [PATCH 7/7] revert --- .../components/DiagnosisReport.tsx | 4 ++-- ui/lib/apps/UserProfile/index.tsx | 7 ++----- ui/lib/components/LanguageDropdown/index.tsx | 4 ++-- ui/lib/components/Root/index.tsx | 5 ++--- ui/lib/utils/i18n.ts | 20 +++---------------- 5 files changed, 11 insertions(+), 29 deletions(-) diff --git a/ui/diagnoseReportApp/components/DiagnosisReport.tsx b/ui/diagnoseReportApp/components/DiagnosisReport.tsx index 612bdf3881..bf0bb5b03f 100644 --- a/ui/diagnoseReportApp/components/DiagnosisReport.tsx +++ b/ui/diagnoseReportApp/components/DiagnosisReport.tsx @@ -2,7 +2,7 @@ import React, { useState } from 'react' import { useTranslation } from 'react-i18next' import DiagnosisTable from './DiagnosisTable' import { ExpandContext, TableDef } from '../types' -import { ALL_LANGUAGES, getEffectiveLang } from '@lib/utils/i18n' +import { ALL_LANGUAGES } from '@lib/utils/i18n' function LangDropdown() { const { i18n } = useTranslation() @@ -10,7 +10,7 @@ function LangDropdown() {
{_.map(ALL_LANGUAGES, (name, key) => { diff --git a/ui/lib/components/LanguageDropdown/index.tsx b/ui/lib/components/LanguageDropdown/index.tsx index d4c1d22bad..72686651b1 100644 --- a/ui/lib/components/LanguageDropdown/index.tsx +++ b/ui/lib/components/LanguageDropdown/index.tsx @@ -3,7 +3,7 @@ import _ from 'lodash' import React, { ReactNode } from 'react' import { useTranslation } from 'react-i18next' -import { ALL_LANGUAGES, getEffectiveLang } from '@lib/utils/i18n' +import { ALL_LANGUAGES } from '@lib/utils/i18n' function LanguageDropdown({ children }: { children: ReactNode }) { const { i18n } = useTranslation() @@ -13,7 +13,7 @@ function LanguageDropdown({ children }: { children: ReactNode }) { } const menu = ( - + {_.map(ALL_LANGUAGES, (name, key) => { return {name} })} diff --git a/ui/lib/components/Root/index.tsx b/ui/lib/components/Root/index.tsx index 9499c00f4d..b5aa232050 100644 --- a/ui/lib/components/Root/index.tsx +++ b/ui/lib/components/Root/index.tsx @@ -9,11 +9,10 @@ import { createTheme, registerIcons } from 'office-ui-fabric-react/lib/Styling' import { Customizations } from 'office-ui-fabric-react/lib/Utilities' import { ConfigProvider } from 'antd' +import i18next from 'i18next' import enUS from 'antd/es/locale/en_US' import zhCN from 'antd/es/locale/zh_CN' -import { getEffectiveLang } from '@lib/utils/i18n' - registerIcons({ icons: { SortUp: , @@ -31,7 +30,7 @@ Customizations.applySettings({ theme }) export default function Root({ children }) { return ( - + {children} ) diff --git a/ui/lib/utils/i18n.ts b/ui/lib/utils/i18n.ts index 0ecc01c9fa..0da831e337 100644 --- a/ui/lib/utils/i18n.ts +++ b/ui/lib/utils/i18n.ts @@ -1,4 +1,4 @@ -import 'dayjs/locale/zh-cn' +import 'dayjs/locale/zh' import dayjs from 'dayjs' import i18next from 'i18next' @@ -35,32 +35,18 @@ export function addTranslationResource(lang, translations) { } export const ALL_LANGUAGES = { - 'zh-CN': '简体中文', + zh: '简体中文', en: 'English', } -export function getEffectiveLang(): string { - const effetiveLangs = Object.keys(ALL_LANGUAGES) - const detectedLang = i18next.language - if (effetiveLangs.includes(detectedLang)) { - return detectedLang - } - if (detectedLang.startsWith('zh')) { - return 'zh-CN' - } - return 'en' -} - i18next .use(LanguageDetector) .use(initReactI18next) .init({ resources: {}, // oh! this line is a big pitfall, we can't remove it, else it will cause strange crash! fallbackLng: 'en', // fallbackLng won't change the detected language + whitelist: ['zh', 'en'], // whitelist will change the detected lanuage interpolation: { escapeValue: false, }, }) - -// init dayjs locale -dayjs.locale(getEffectiveLang().toLowerCase())