-
-
Save juliomerisio/94621a71857200a37dae1f8f74e52196 to your computer and use it in GitHub Desktop.
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
import { z } from 'zod'; | |
type SupportedLocales = 'en-US' | 'pt-BR'; | |
export const dictionaries: Record< | |
SupportedLocales, | |
Record<string, Record<string, string>> | |
> = { | |
'en-US': { | |
common: { | |
hello: 'Awesome!', | |
hi: 'Hello {{name}} {{age}}!', | |
bye: 'Goodbye {{name}}!', | |
how: 'blablabla', | |
banana: 'blablabla', | |
}, | |
}, | |
'pt-BR': { | |
common: { | |
hello: 'mundo', | |
hi: 'Ola {{name}} {{age}}!', | |
bye: 'Tchau {{name}}!', | |
how: 'xablau {{other}}', | |
banana: 'goiaba e {{bananinha}}', | |
}, | |
}, | |
}; | |
let currentLocale: SupportedLocales = 'en-US'; | |
export const initialTranslation = async ( | |
locale: SupportedLocales = 'en-US' | |
) => { | |
await new Promise((resolve) => { | |
currentLocale = locale ?? currentLocale; | |
resolve(true); | |
}); | |
}; | |
const TranslationSchema = z.discriminatedUnion('key', [ | |
z.object({ | |
key: z.literal('common:hello'), | |
}), | |
z.object({ | |
key: z.literal('common:hi'), | |
params: z.object({ name: z.string(), age: z.string() }), | |
}), | |
z.object({ | |
key: z.literal('common:bye'), | |
params: z.object({ name: z.string() }), | |
}), | |
z.object({ | |
key: z.literal('common:how'), | |
params: z.object({ other: z.string() }), | |
}), | |
z.object({ | |
key: z.literal('common:banana'), | |
params: z.object({ bananinha: z.string() }), | |
}), | |
]); | |
export const t = ( | |
key: z.infer<typeof TranslationSchema>, | |
locale: SupportedLocales = currentLocale | |
) => { | |
const [namespace, innerKey] = key.key.split(':'); | |
const sentence = dictionaries[locale][namespace][innerKey]; | |
let result = ''; | |
if ('params' in key) { | |
Object.entries(key.params).forEach(([paramKey, paramValue]) => { | |
result = sentence.replace(`{{${paramKey}}}`, paramValue); | |
}); | |
return result; | |
} | |
return sentence; | |
}; | |
t({ key: 'common:hi', params: { name: 'John', age: '20' } }, 'pt-BR'); // Hello John 20! | |
t({ key: 'common:banana', params: { bananinha: 'banana' } }); // goiaba e banana | |
t({ key: 'common:hello' }); // Awesome! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: