mirror of
https://github.com/type-challenges/type-challenges.git
synced 2025-12-08 19:06:13 +00:00
Co-authored-by: kabrunko-dev <kabrunko.dev@protonmail.com> Co-authored-by: kabrunko <142346548+kabrunko-dev@users.noreply.github.com>
27 lines
808 B
TypeScript
27 lines
808 B
TypeScript
export const defaultLocale = 'en'
|
|
|
|
export const supportedLocales = ['en', 'zh-CN', 'ja', 'ko', 'pt-BR'] as const
|
|
|
|
export const messages = {
|
|
'en': require('./locales/en.json'),
|
|
'zh-CN': require('./locales/zh-CN.json'),
|
|
'ja': require('./locales/ja.json'),
|
|
'ko': require('./locales/ko.json'),
|
|
'pt-BR': require('./locales/pt-BR.json'),
|
|
}
|
|
|
|
export type SupportedLocale = keyof typeof messages
|
|
|
|
export function t(locale: SupportedLocale, key: string): string {
|
|
const result = (messages[locale] && messages[locale][key]) || messages[defaultLocale][key]
|
|
if (!result)
|
|
throw new Error(`Missing message for key "${key}"`)
|
|
return result
|
|
}
|
|
|
|
export function f(name: string, locale: string, ext: string) {
|
|
if (locale === defaultLocale)
|
|
return `${name}.${ext}`
|
|
return `${name}.${locale}.${ext}`
|
|
}
|