Skip to content

Commit ae251c3

Browse files
refactor: import translations lazily
Implements the idea in typst-jp/docs#303 (comment) Blocks #21
1 parent 5f2d7a0 commit ae251c3

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

src/translation/index.tsx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import type { FC } from "hono/jsx";
22
import type { TooltipProps } from "../components/ui/Tooltip";
33
import { language } from "../metadata";
4-
import {
5-
Translation as EnUSTranslation,
6-
translation as enUSTranslation,
7-
} from "./en-US";
8-
import {
9-
Translation as JaJPTranslation,
10-
translation as jaJPTranslation,
11-
} from "./ja-JP";
124

135
/**
146
* Translation dictionary for UI attributes and aria labels.
@@ -105,12 +97,15 @@ export type TranslationComponentProps =
10597
export type TranslationComponent = FC<TranslationComponentProps>;
10698

10799
// Switch translation language.
108-
const { Translation, translation } = (() => {
100+
const { Translation, translation } = await (() => {
101+
// These imports should be lazy and static.
102+
// - Lazy: A translation may use special metadata (e.g., Discord/QQ URL), so not all translations can be safely imported in every build. Therefore, we have to use the import function, not the import statement.
103+
// - Static: The argument of the import function has to be a plain string, not a variable. Dynamic import has limitations even with rollup and vite properly configured.
109104
switch (language) {
110-
case "ja-JP":
111-
return { Translation: JaJPTranslation, translation: jaJPTranslation };
112105
case "en-US":
113-
return { Translation: EnUSTranslation, translation: enUSTranslation };
106+
return import("./en-US");
107+
case "ja-JP":
108+
return import("./ja-JP");
114109
default:
115110
throw new Error(`Unsupported language: ${language}`);
116111
}

0 commit comments

Comments
 (0)