Skip to content

Commit 66f8496

Browse files
committed
Export reusable translation placeholder types and update Translation to use them
├── apps │ └── editor │ ├── package.json │ └── src │ └── views │ ├── prompt │ │ └── frontend │ │ └── i18n │ │ ├── translations │ │ │ ├── actions.ts │ │ │ ├── common.ts │ │ │ ├── configurations.ts │ │ │ ├── footer.ts │ │ │ ├── header.ts │ │ │ ├── home.ts │ │ │ ├── index.ts │ │ │ ├── main.ts │ │ │ ├── modals.ts │ │ │ ├── prompt-field.ts │ │ │ ├── recent-donations.ts │ │ │ └── selected-files.ts │ │ └── use-translation.tsx │ ├── settings │ │ └── frontend │ │ └── i18n │ │ ├── translations │ │ │ ├── actions.ts │ │ │ ├── api.ts │ │ │ ├── cli.ts │ │ │ ├── common.ts │ │ │ ├── edit-provider-form.ts │ │ │ ├── edit-template-form.ts │ │ │ ├── general.ts │ │ │ ├── index.ts │ │ │ ├── sections.ts │ │ │ └── web.ts │ │ └── use-translation.tsx │ └── shared │ ├── hooks │ │ └── use-translation.tsx │ └── i18n │ ├── translations │ │ ├── common.ts │ │ ├── edit-api-configuration-form.ts │ │ ├── edit-cli-configuration-form.ts │ │ ├── edit-web-configuration-form.ts │ │ └── index.ts │ └── use-translation.tsx └── packages └── shared └── package.json
1 parent 488c49e commit 66f8496

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

‎apps/editor/src/views/shared/hooks/use-translation.tsx‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@ export const TranslationContext = createContext<
44
Record<string, Record<string, string>>
55
>({})
66

7-
type ExtractPlaceholders<S extends string> =
7+
export type ExtractPlaceholders<S extends string> =
88
S extends `${string}{${infer Key}}${infer Rest}`
99
? Key | ExtractPlaceholders<Rest>
1010
: never
1111

12+
export type PlaceholdersForKey<
13+
T extends Record<string, Record<string, string>>,
14+
K extends keyof T
15+
> = ExtractPlaceholders<T[K]['en'] extends string ? T[K]['en'] : string>
16+
17+
export type TranslationProps<
18+
T extends Record<string, Record<string, string>>,
19+
K extends keyof T
20+
> = [PlaceholdersForKey<T, K>] extends [never]
21+
? { id: K; components?: never }
22+
: { id: K; components: Record<PlaceholdersForKey<T, K>, React.ReactNode> }
23+
1224
export function create_translation<
1325
T extends Record<string, Record<string, string>>
1426
>(translations: T) {
15-
type PlaceholdersForKey<K extends keyof T> = ExtractPlaceholders<
16-
T[K]['en'] extends string ? T[K]['en'] : string
17-
>
18-
19-
type TranslationProps<K extends keyof T> = [PlaceholdersForKey<K>] extends [
20-
never
21-
]
22-
? { id: K; components?: never }
23-
: { id: K; components: Record<PlaceholdersForKey<K>, React.ReactNode> }
24-
2527
const use_translation = () => {
2628
const lang = document.documentElement.lang || 'en'
2729
const context_translations = useContext(TranslationContext)
@@ -48,7 +50,7 @@ export function create_translation<
4850
return { t }
4951
}
5052

51-
const Translation = <K extends keyof T>(params: TranslationProps<K>) => {
53+
const Translation = <K extends keyof T>(params: TranslationProps<T, K>) => {
5254
const components = params.components ?? {}
5355
const { t } = use_translation()
5456
const text = t(params.id)

0 commit comments

Comments
 (0)