diff --git a/src/__data__/languages.json b/src/__data__/languages.json index 2ce307b..3e58904 100644 --- a/src/__data__/languages.json +++ b/src/__data__/languages.json @@ -8,6 +8,7 @@ "name": "German", "nativeName": "Deutsch", "country": "de", + "antLocale": "de_DE", "dayjsLocale": "de", "timeagoLocale": "de", "contributors": [ @@ -21,6 +22,7 @@ "name": "French", "nativeName": "Français", "country": "fr", + "antLocale": "fr_FR", "dayjsLocale": "fr", "timeagoLocale": "fr", "contributors": [ @@ -34,6 +36,7 @@ "name": "Dutch", "nativeName": "Nederlands", "country": "nl", + "antLocale": "nl_NL", "dayjsLocale": "nl", "timeagoLocale": "nl", "contributors": [ @@ -47,6 +50,7 @@ "name": "Polish", "nativeName": "Polski", "country": "pl", + "antLocale": "pl_PL", "dayjsLocale": "pl", "timeagoLocale": "pl", "contributors": [ @@ -60,6 +64,7 @@ "name": "Vietnamese", "nativeName": "Tiếng Việt", "country": "vn", + "antLocale": "vi_VN", "dayjsLocale": "vi", "timeagoLocale": "vi", "contributors": [ diff --git a/src/global/LocaleContext.tsx b/src/global/LocaleContext.tsx index ff85d2b..60b1828 100644 --- a/src/global/LocaleContext.tsx +++ b/src/global/LocaleContext.tsx @@ -4,7 +4,6 @@ import { FC, createContext, useEffect, useState } from "react"; import { ConfigProvider } from "antd"; import { Locale } from "antd/lib/locale-provider"; -import localeValues from "antd/lib/locale/default"; import { useTranslation } from "react-i18next"; import { getLanguages } from "@utils/i18n"; @@ -23,14 +22,15 @@ const { t, i18n } = useTranslation(); const langCode = i18n.language; const languages = getLanguages(); + const lang = languages?.[langCode]; + // These are wrapped in objects due to some bizarre issues where React was + // attempting to call the timeagoFormatter at some point?? const [timeagoFormatter, setTimeagoFormatter] = useState<{ formatter: Formatter }>(); + const [antLocale, setAntLocale] = useState<{ locale: Locale }>(); // Load the day.js locale if available useEffect(() => { - if (!languages) return; - const lang = languages[langCode]; - // See if the language has a dayjs locale set. If not, revert to `en` const dayjsLocale = lang?.dayjsLocale; if (!dayjsLocale) { @@ -56,13 +56,10 @@ dayjs.locale(dayjsLocale); }) .catch(console.error); - }, [langCode, languages]); + }, [lang, langCode, languages]); // Load the timeago locale if available useEffect(() => { - if (!languages) return; - const lang = languages[langCode]; - // See if the language has a timeago locale set. If not, revert to default const timeagoLocale = lang?.timeagoLocale; if (!timeagoLocale) { @@ -80,15 +77,39 @@ `react-timeago/lib/language-strings/${timeagoLocale}` ) .then(strings => { - debug("got timeagoLocale locale %s", timeagoLocale); - console.log(strings.default); + debug("got timeago locale %s", timeagoLocale); setTimeagoFormatter({ formatter: buildFormatter(strings.default) }); }) .catch(console.error); - }, [langCode, languages]); + }, [lang, langCode, languages]); + + // Load the antd locale if available + useEffect(() => { + // See if the language has an antd locale set. If not, revert to default + const antLocaleCode = lang?.antLocale; + if (!antLocaleCode) { + debug("language %s doesn't have an antd locale, reverting to default", langCode); + setAntLocale(undefined); + return; + } + + // Load the locale + debug("loading antd locale %s for language %s", antLocaleCode, langCode); + import( + /* webpackInclude: /\.js$/ */ + /* webpackMode: "lazy" */ + /* webpackChunkName: "locale-antd-[request]" */ + `antd/lib/locale/${antLocaleCode}` + ) + .then(locale => { + debug("got antd locale %s", antLocaleCode); + setAntLocale({ locale: locale.default }); + }) + .catch(console.error); + }, [lang, langCode, languages]); return - + {children} ; diff --git a/src/utils/i18n.ts b/src/utils/i18n.ts index de4c6bf..04dd4b0 100644 --- a/src/utils/i18n.ts +++ b/src/utils/i18n.ts @@ -23,6 +23,7 @@ country?: string; dayjsLocale?: string; timeagoLocale?: string; + antLocale?: string; contributors: Contributor[]; }