Skip to content

Commit b7ca404

Browse files
committed
refactor: improve translation structure and type definitions
1 parent 9cf0f7a commit b7ca404

File tree

1 file changed

+29
-37
lines changed

1 file changed

+29
-37
lines changed

website/src/translations.tsx renamed to website/src/translation.tsx

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
1-
import type { FC, JSX, PropsWithChildren } from "hono/jsx";
1+
import type { FC } from "hono/jsx";
22
import {
33
discordServerUrl,
44
githubOrganizationUrl,
55
githubRepositoryUrl,
66
typstOfficialDocsUrl,
77
} from "./metadata";
88

9-
type MyMap = {
10-
[key: string]: string;
11-
};
12-
13-
export const menuTranslations: MyMap = {
14-
lang: "ja",
15-
ariaCloseMenu: "メニューを閉じる",
16-
documentationTitle: "Typstドキュメント日本語版",
17-
ariaShowInformation: "の詳細情報を表示",
18-
ariaClose: "閉じる",
19-
ariaOpenSearch: "検索を開く",
20-
ariaOpenMenu: "メニューを開く",
21-
ariaCloseSearch: "検索を閉じる",
22-
};
23-
24-
export const t = (key: keyof MyMap) => {
25-
if (typeof menuTranslations[key] === "string") {
26-
return menuTranslations[key];
27-
}
28-
throw new Error(`Not translation found for ${key}`);
29-
};
9+
/**
10+
* Translation dictionary for UI attributes and aria labels.
11+
*
12+
* @example
13+
* translation.ariaCloseMenu()
14+
* translation.ariaShowInformation({ name: "foo" })
15+
*/
16+
export const translation = {
17+
lang: () => "ja",
18+
ariaCloseMenu: () => "メニューを閉じる",
19+
documentationTitle: () => "Typstドキュメント日本語版",
20+
ariaShowInformation: (props: {
21+
name: string;
22+
}) => `${props.name}の詳細情報を表示`,
23+
ariaClose: () => "閉じる",
24+
ariaOpenSearch: () => "検索を開く",
25+
ariaOpenMenu: () => "メニューを開く",
26+
ariaCloseSearch: () => "検索を閉じる",
27+
} as const;
3028

3129
type TranslationKey =
3230
| "definition"
@@ -80,21 +78,15 @@ type TranslationKey =
8078

8179
export type TranslationProps =
8280
| { translationKey: TranslationKey }
83-
| PropsWithChildren<{ translationKey: "banner"; version: string }>;
84-
85-
/*
86-
This is inferred as the following type:
87-
88-
type TranslationProps = {
89-
translationKey: "lang";
90-
} |
91-
...
92-
| {
93-
translationKey: "banner";
94-
children?: string | number | boolean | Promise<string> | JSXNode | Child[] | null;
95-
}
96-
*/
81+
| { translationKey: "banner"; version: string };
9782

83+
/**
84+
* Translation component for UI text, descriptions, and other content to be embedded as JSX.
85+
*
86+
* @example
87+
* <Translation translationKey="definition" />
88+
* <Translation translationKey="banner" version="1.0.0" />
89+
*/
9890
export const Translation: FC<TranslationProps> = (props) => {
9991
switch (props.translationKey) {
10092
case "definition":
@@ -240,6 +232,6 @@ export const Translation: FC<TranslationProps> = (props) => {
240232
case "information":
241233
return <>情報</>;
242234
default:
243-
throw new Error("No translationKey found for Translation Element");
235+
return null;
244236
}
245237
};

0 commit comments

Comments
 (0)