[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,48 +14,65 @@ 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: {
'back': '返回',
'email': '邮箱',
'password': '密码',
'username': '用户名',
'welcome.btn-login': '登录',
'welcome.btn-signup': '注册',
'welcome.email-password-missing': '邮箱和密码必须填哦',
'welcome.username-missing': '用户名/邮箱必须填哦',
'welcome.waiting-turnstile': '正在验证网络环境...',
'welcome.turnstile-error': '验证网络环境出错了请关闭VPN后重试',
'welcome.turnstile-timeout': '验证网络环境超时了,请重试',
'welcome.verification-sent': '验证邮件已发送至 ${email},请翻翻收件箱',
'welcome.verify-state-0': '您还没有验证邮箱哦!验证邮件一分钟内刚刚发到您的邮箱,请翻翻收件箱',
'welcome.verify-state-1': '您还没有验证邮箱哦我们在过去的24小时内已经发送了3封验证邮件所以我们不会再发送了请翻翻收件箱',
'welcome.verify-state-2': '您还没有验证邮箱哦!我们刚刚又发送了一封验证邮件,请翻翻收件箱',
'welcome.verifying': '正在验证邮箱...请稍等',
'welcome.verified': '您的邮箱已经验证成功!您现在可以登录了',
'welcome.verification-failed': '验证失败:${message}。请重试',
}
const zhWelcome ={
'back': '返回',
'email': '邮箱',
'password': '密码',
'username': '用户名',
'welcome.btn-login': '登录',
'welcome.btn-signup': '注册',
'welcome.email-password-missing': '邮箱和密码必须填哦',
'welcome.username-missing': '用户名/邮箱必须填哦',
'welcome.waiting-turnstile': '正在验证网络环境...',
'welcome.turnstile-error': '验证网络环境出错了请关闭VPN后重试',
'welcome.turnstile-timeout': '验证网络环境超时了,请重试',
'welcome.verification-sent': '验证邮件已发送至 ${email},请翻翻收件箱',
'welcome.verify-state-0': '您还没有验证邮箱哦!验证邮件一分钟内刚刚发到您的邮箱,请翻翻收件箱',
'welcome.verify-state-1': '您还没有验证邮箱哦我们在过去的24小时内已经发送了3封验证邮件所以我们不会再发送了,请翻翻收件箱',
'welcome.verify-state-2': '您还没有验证邮箱哦!我们刚刚又发送了一封验证邮件,请翻翻收件箱',
'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]
}