[U] update i18n.ts type and more intuitive

pull/22/head
chiba 2024-03-06 16:17:06 +08:00
parent e137210cbc
commit 0ab78983d4
1 changed files with 51 additions and 32 deletions

View File

@ -1,5 +1,7 @@
const EN_REF = {
const EN_REF_USER = {
'TEST': 'TEST'
}
const EN_REF_Welcome = {
'back': 'Back',
'email': 'Email',
'password': 'Password',
@ -12,17 +14,15 @@ const EN_REF = {
'welcome.turnstile-error': 'Error verifying your network environment. Please turn off your VPN and try again.',
'welcome.turnstile-timeout': 'Network verification timed out. Please try again.',
'welcome.verification-sent': 'A verification email has been sent to ${email}. Please check your inbox!',
'welcome.verify-state-0': "You haven't verified your email. A verification email had been sent to your inbox less than a minute ago. Please check your inbox!",
'welcome.verify-state-1': "You haven't verified your email. We've already sent 3 emails over the last 24 hours so we'll not send another one. Please check your inbox!",
'welcome.verify-state-2': "You haven't verified your email. We just sent you another verification email. Please check your inbox!",
'welcome.verifying': "Verifying your email... please wait.",
'welcome.verified': "Your email has been verified! You can now log in now.",
'welcome.verify-state-0': 'You haven\'t verified your email. A verification email had been sent to your inbox less than a minute ago. Please check your inbox!',
'welcome.verify-state-1': 'You haven\'t verified your email. We\'ve already sent 3 emails over the last 24 hours so we\'ll not send another one. Please check your inbox!',
'welcome.verify-state-2': 'You haven\'t verified your email. We just sent you another verification email. Please check your inbox!',
'welcome.verifying': 'Verifying your email... please wait.',
'welcome.verified': 'Your email has been verified! You can now log in now.',
'welcome.verification-failed': 'Verification failed: ${message}. Please try again.',
}
const msgs: { [index: string]: typeof EN_REF } = {
en: EN_REF,
zh: {
const zhWelcome ={
'back': '返回',
'email': '邮箱',
'password': '密码',
@ -41,19 +41,38 @@ const msgs: { [index: string]: typeof EN_REF } = {
'welcome.verifying': '正在验证邮箱...请稍等',
'welcome.verified': '您的邮箱已经验证成功!您现在可以登录了',
'welcome.verification-failed': '验证失败:${message}。请重试',
}
}
const zhUser={
'test':'test'
}
let lang = 'en'
type LocalizationMessages = {
[key: string]: string;
};
const allI18n: { en: LocalizationMessages; zh: LocalizationMessages } = {
en: { ...EN_REF_USER, ...EN_REF_Welcome },
zh: { ...zhUser, ...zhWelcome }
};
const msgs: { [index: string]: LocalizationMessages} = {
en: allI18n.en,
zh: allI18n.zh
}
let lang:string = 'en'
// Infer language from browser
if (navigator.language.startsWith('zh')) {
lang = 'zh'
}
export function t(key: keyof typeof EN_REF, variables?: { [index: string]: string }) {
export function t(key: keyof typeof allI18n.zh |keyof typeof allI18n.en, variables?: { [index: string]: string }) {
if (variables) {
return msgs[lang][key].replace(/\${(.*?)}/g, (_, v) => variables[v])
return msgs[lang][key].replace(/\${(.*?)}/g, (_:string, v: string | number) => variables[v])
}
return msgs[lang][key]
}