diff --git a/src/App.tsx b/src/App.tsx index f63a6c4..0496463 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -17,6 +17,7 @@ import { CheckStatus } from "./pages/CheckStatus"; import { AppServices } from "./global/AppServices"; import { WebsocketProvider } from "./global/ws/WebsocketProvider"; +import { LocaleContext } from "./global/LocaleContext"; import Debug from "debug"; const debug = Debug("kristweb:app"); @@ -29,14 +30,16 @@ return }> - - - + + + + - {/* Services, etc. */} - - - + {/* Services, etc. */} + + + + ; } diff --git a/src/__data__/languages.json b/src/__data__/languages.json index 1e60b79..e8cc1a7 100644 --- a/src/__data__/languages.json +++ b/src/__data__/languages.json @@ -8,6 +8,7 @@ "name": "German", "nativeName": "Deutsch", "country": "de", + "dayjsLocale": "de", "contributors": [ { "name": "Lignum", @@ -19,6 +20,7 @@ "name": "French", "nativeName": "Français", "country": "fr", + "dayjsLocale": "fr", "contributors": [ { "name": "Anavrins", @@ -30,6 +32,7 @@ "name": "Dutch", "nativeName": "Nederlands", "country": "nl", + "dayjsLocale": "nl", "contributors": [ { "name": "HydroNitrogen", @@ -41,6 +44,7 @@ "name": "Vietnamese", "nativeName": "Tiếng Việt", "country": "vn", + "dayjsLocale": "vi", "contributors": [ { "name": "Boom", diff --git a/src/global/LocaleContext.tsx b/src/global/LocaleContext.tsx new file mode 100644 index 0000000..2ad3dcf --- /dev/null +++ b/src/global/LocaleContext.tsx @@ -0,0 +1,57 @@ +// Copyright (c) 2020-2021 Drew Lemmy +// This file is part of KristWeb 2 under GPL-3.0. +// Full details: https://github.com/tmpim/KristWeb2/blob/master/LICENSE.txt +import { FC, useEffect } 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"; + +import dayjs from "dayjs"; + +import Debug from "debug"; +const debug = Debug("kristweb:locale-context"); + +export const LocaleContext: FC = ({ children }): JSX.Element => { + const { t, i18n } = useTranslation(); + const langCode = i18n.language; + const languages = getLanguages(); + + // 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) { + debug("language %s doesn't have a dayjs locale, reverting to `en`", langCode); + dayjs.locale("en"); + return; + } + + // Attempt to import the locale asynchronously. This will usually incur a + // network request, but it should be cached by the service worker. + debug("loading dayjs locale %s for language %s", dayjsLocale, langCode); + // Including only `.js` files here ensures that it doesn't attempt to load + // the TypeScript typings, which causes build warnings due to a missing + // loader for those files. + import( + /* webpackInclude: /\.js$/ */ + /* webpackMode: "lazy" */ + /* webpackChunkName: "locale-dayjs-[request]" */ + `dayjs/locale/${dayjsLocale}` + ) + .then(() => { + debug("got dayjs locale %s", dayjsLocale); + dayjs.locale(dayjsLocale); + }) + .catch(console.error); + }, [langCode, languages]); + + return + {children} + ; +}; diff --git a/src/service-worker.ts b/src/service-worker.ts index 5f5610f..4aeb74b 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -24,7 +24,12 @@ // Their URLs are injected into the manifest variable below. // This variable must be present somewhere in your service worker file, // even if you decide not to use precaching. See https://cra.link/PWA -precacheAndRoute(self.__WB_MANIFEST); + +// Filter out all locale files from the precache. There's usually a few hundred +// of these, so we definitely don't want to download them all on install! +const filteredManifest = self.__WB_MANIFEST + .filter(u => !(typeof u === "string" ? u : u.url).includes("locale-")); +precacheAndRoute(filteredManifest); // Cache the locale files (currently the only preloading step) registerRoute( diff --git a/src/utils/i18n.ts b/src/utils/i18n.ts index 7f48db2..530c774 100644 --- a/src/utils/i18n.ts +++ b/src/utils/i18n.ts @@ -21,6 +21,7 @@ name: string; nativeName?: string; country?: string; + dayjsLocale?: string; contributors: Contributor[]; }