-
Notifications
You must be signed in to change notification settings - Fork 0
/
Intl.tsx
57 lines (53 loc) · 1.79 KB
/
Intl.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { LocaleKey, TranslationDocument, TranslationFunction } from 'intl-schematic';
import { For, children, JSX } from 'solid-js';
export function useIntl<
LocaleDoc extends TranslationDocument,
T extends TranslationFunction<LocaleDoc, any>
>(t: T): {
<
Key extends LocaleKey<LocaleDoc>,
Parts extends PropertyKey =
LocaleDoc[Key] extends { dictionary: infer _Parts } ? keyof _Parts : keyof LocaleDoc[Key]
>(props: {
k: Key;
children: Record<Parts, (part: string) => JSX.Element>;
}): JSX.Element;
};
export function useIntl<
LocaleDoc extends TranslationDocument,
T extends TranslationFunction<LocaleDoc, any>,
Key extends LocaleKey<LocaleDoc>,
Parts extends PropertyKey =
LocaleDoc[Key] extends { dictionary: infer _Parts } ? keyof _Parts : keyof LocaleDoc[Key]
>(t: T, k: () => Key): {
(props: {
children: Record<Parts, (part: string) => JSX.Element>;
}): JSX.Element;
};
export function useIntl(t: TranslationFunction<any, any>, key?: () => string) {
return (props: {
k: string;
children: Record<string, (part: string) => JSX.Element>;
}) => (
<Intl t={t} k={key?.() ?? props.k}>
{props.children}
</Intl>
);
}
export default function Intl<
LocaleDoc extends TranslationDocument,
T extends TranslationFunction<LocaleDoc, any>,
Key extends LocaleKey<LocaleDoc>,
Parts extends PropertyKey =
LocaleDoc[Key] extends { dictionary: infer _Parts } ? keyof _Parts : keyof LocaleDoc[Key]
>(props: {
t: T;
k: Key;
children: Record<Parts, (part: string) => JSX.Element>;
}) {
return children(() => (
<For each={Object.entries<(part: string) => JSX.Element>(props.children)}>
{([part, h]) => h(props.t(props.k, part))}
</For>
)) as unknown as JSX.Element;
}