Skip to content

Commit b069fab

Browse files
committed
chore(website): use static loading of translations
1 parent d5ce01a commit b069fab

34 files changed

+39
-19
lines changed

apps/website/src/i18n.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
1+
import i18next from "i18next";
2+
import { initReactI18next } from "react-i18next";
3+
14
interface Locale {
25
id: string;
36
name: string;
47
rtl?: boolean;
58
}
69

10+
i18next.use(initReactI18next);
11+
const localeFiles = import.meta.glob("./translations/*/translation.json", { eager: true });
12+
const resources: Record<string, Record<string, string>> = {};
13+
for (const [ path, translations ] of Object.entries(localeFiles)) {
14+
const id = path.split("/").at(-2);
15+
if (!resources[id]) resources[id] = {};
16+
if ("default" in (translations as any)) {
17+
resources[id].translation = (translations as any).default;
18+
} else {
19+
resources[id].translation = translations;
20+
}
21+
}
22+
23+
export function initTranslations(lng: string) {
24+
i18next.init({
25+
fallbackLng: "en",
26+
lng,
27+
returnEmptyString: false,
28+
resources,
29+
initAsync: false,
30+
react: {
31+
useSuspense: false
32+
}
33+
});
34+
}
35+
736
export const LOCALES: Locale[] = [
837
{ id: "en", name: "English" },
938
{ id: "ro", name: "Română" },

apps/website/src/index.tsx

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import Footer from './components/Footer.js';
88
import GetStarted from './pages/GetStarted/get-started.js';
99
import SupportUs from './pages/SupportUs/SupportUs.js';
1010
import { createContext } from 'preact';
11-
import { useLayoutEffect, useState } from 'preact/hooks';
11+
import { useLayoutEffect, useRef, useState } from 'preact/hooks';
1212
import { default as i18next, changeLanguage } from 'i18next';
13-
import { extractLocaleFromUrl, LOCALES, mapLocale } from './i18n';
13+
import { extractLocaleFromUrl, initTranslations, LOCALES, mapLocale } from './i18n';
1414
import HttpApi from 'i18next-http-backend';
1515
import { initReactI18next } from "react-i18next";
1616

@@ -43,29 +43,20 @@ export function App(props: {repoStargazersCount: number}) {
4343
export function LocaleProvider({ children }) {
4444
const { path } = useLocation();
4545
const localeId = mapLocale(extractLocaleFromUrl(path) || navigator.language);
46-
const [ loaded, setLoaded ] = useState(false);
46+
const loadedRef = useRef(false);
4747

48-
useLayoutEffect(() => {
49-
i18next
50-
.use(HttpApi)
51-
.use(initReactI18next);
52-
i18next.init({
53-
lng: localeId,
54-
fallbackLng: "en",
55-
backend: {
56-
loadPath: "/translations/{{lng}}/{{ns}}.json",
57-
},
58-
returnEmptyString: false
59-
}).then(() => setLoaded(true))
60-
}, []);
48+
if (!loadedRef.current) {
49+
initTranslations(localeId);
50+
loadedRef.current = true;
51+
} else {
52+
changeLanguage(localeId);
53+
}
6154

6255
useLayoutEffect(() => {
63-
if (!loaded) return;
64-
changeLanguage(localeId);
6556
const correspondingLocale = LOCALES.find(l => l.id === localeId);
6657
document.documentElement.lang = localeId;
6758
document.documentElement.dir = correspondingLocale?.rtl ? "rtl" : "ltr";
68-
}, [ loaded, localeId ]);
59+
}, [ localeId ]);
6960

7061
return (
7162
<LocaleContext.Provider value={localeId}>

0 commit comments

Comments
 (0)