Last active
June 24, 2024 13:28
-
-
Save SandroMaglione/35532b42df93f71b8a3af016f2f2207c to your computer and use it in GitHub Desktop.
i18n (Internationalization and localization) in Typescript using `satisfies` only
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Message = string | ((args: any) => string); | |
type Translation = { en: Message; it: Message }; | |
export type I18n = Record<string, Translation>; | |
export const t = { | |
helloWorld: { | |
en: "Hello World", | |
it: "Ciao mondo", | |
}, | |
helloParam: { | |
en: (param: string) => `Hello ${param}` as const, | |
it: (param: number) => `Ciao ${param}` as const, | |
}, | |
} as const satisfies I18n; | |
const helloWorld = t.helloWorld["it"]; | |
const helloParamEn = t.helloParam["en"]("world"); | |
const helloParamIt = t.helloParam["it"](10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment