forked from type-challenges/type-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocales.ts
25 lines (20 loc) · 755 Bytes
/
locales.ts
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
export const defaultLocale = 'en'
export const supportedLocales = ['en', 'zh-CN', 'ja', 'ko'] 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'),
}
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}`
}