Skip to content

Commit

Permalink
fix i18n string replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
mebtte committed Jul 27, 2023
1 parent a1999eb commit 971c502
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 21 deletions.
6 changes: 4 additions & 2 deletions apps/cli/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import { Language } from '#/constants';
import { Key } from './constants';
import en from './en';
import zhHans from './zh_hans';
import ja from './ja';

const LANGUAGE_MAP: Record<Language, typeof en> = {
[Language.EN]: en,
[Language.ZH_HANS]: zhHans,
[Language.JA]: ja,
};

export function t(key: Key, language: Language, ...args: string[]) {
const value = LANGUAGE_MAP[language][key];
let value = LANGUAGE_MAP[language][key];

if (args.length) {
for (let i = 0; i < args.length; i += 1) {
value.replace(`%s${i + 1}`, args[i]);
value = value.replace(`%s${i + 1}`, args[i]);
}
}

Expand Down
10 changes: 10 additions & 0 deletions apps/cli/src/i18n/ja.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Key } from './constants';
import en from './en';

const ja: {
[key in Key]: string;
} = {
...en,
};

export default ja;
8 changes: 6 additions & 2 deletions apps/pwa/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
please_enter_valid_email: 'please enter valid email',
captcha: 'captcha',
get_login_code: 'get login code',
login_code_has_emailed: 'login code has emailed',
login_code_emailed: 'login code emailed',
change_language_question:
'changing language will reload application, continue ?',
relative_volume: 'relative volume',
Expand All @@ -24,7 +24,7 @@ export default {
musicbill: 'musicbill',
user_management: 'user management',
logout: 'logout',
logout_question: 'Are you sure to logout ?',
logout_question: 'are you sure to logout ?',
search: 'search',
shared_musicbill_invitation: 'shared musicbill invitation',
public_musicbill_collection: 'public musicbill collection',
Expand All @@ -35,4 +35,8 @@ export default {
create_at: 'create at',
no_music_in_musicbill: 'no music in musicbill',
find_in_musicbill: 'find in musicbill',
user_created: 'user created',
create_user: 'create user',
remark: 'remark',
create: 'create',
};
9 changes: 7 additions & 2 deletions apps/pwa/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ switch (setting.get().language) {
({ default: translation } = await import('./zh_hans'));
break;
}
case Language.JA: {
({ default: translation } = await import('./ja'));
break;
}
default: {
({ default: translation } = await import('./en'));
}
}

export function t(key: Key, ...args: string[]) {
const value = translation[key];
let value = translation[key];

if (args.length) {
for (let i = 0; i < args.length; i += 1) {
value.replace(`%s${i + 1}`, args[i]);
value = value.replace(`%s${i + 1}`, args[i]);
}
}

Expand All @@ -33,6 +37,7 @@ export const LANGUAGE_MAP: Record<
> = {
[Language.EN]: { label: 'english' },
[Language.ZH_HANS]: { label: '简体中文' },
[Language.JA]: { label: '日本語' },
};

export { Key };
10 changes: 10 additions & 0 deletions apps/pwa/src/i18n/ja.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Key } from './constants';
import en from './en';

const ja: {
[key in Key]: string;
} = {
...en,
};

export default ja;
6 changes: 5 additions & 1 deletion apps/pwa/src/i18n/zh_hans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const zhCN: {
please_enter_valid_email: '请输入合法的邮箱',
captcha: '验证码',
get_login_code: '获取登录验证码',
login_code_has_emailed: '登录验证码已经发送到邮箱',
login_code_emailed: '登录验证码已发送到邮箱',
change_language_question: '更换语言将会重新加载应用, 是否继续?',
relative_volume: '相对音量',
music_play_record_short: '播放记录',
Expand All @@ -37,6 +37,10 @@ const zhCN: {
create_at: '创建于',
no_music_in_musicbill: '乐单暂无音乐',
find_in_musicbill: '乐单内查找',
user_created: '用户已创建',
create_user: '创建用户',
remark: '备注',
create: '创建',
};

export default zhCN;
2 changes: 1 addition & 1 deletion apps/pwa/src/pages/login/email_panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function EmailPanel({ toNext }: { toNext: (email: string) => void }) {
captchaId,
captchaValue,
});
notice.info(t('login_code_has_emailed'));
notice.info(t('login_code_emailed'));
toNext(email);
} catch (error) {
logger.error(error, 'Failed to get login code');
Expand Down
23 changes: 10 additions & 13 deletions apps/pwa/src/pages/player/pages/user_manage/create_user_dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import notice from '@/utils/notice';
import { EMAIL } from '#/constants/regexp';
import logger from '@/utils/logger';
import adminCreateUser from '@/server/api/admin_create_user';
import { t } from '@/i18n';
import e, { EventType } from './eventemitter';
import { ZIndex } from '../../constants';

Expand All @@ -33,11 +34,8 @@ function CreateUserDialog() {

const [loading, setLoading] = useState(false);
const onCreate = async () => {
if (!email) {
return notice.error('请输入邮箱');
}
if (!EMAIL.test(email)) {
return notice.error('邮箱格式错误');
if (!email || !EMAIL.test(email)) {
return notice.error(t('please_enter_valid_email'));
}

setLoading(true);
Expand All @@ -47,11 +45,11 @@ function CreateUserDialog() {
remark: remark.replace(/\s+/g, ' ').trim(),
});

notice.info('已创建用户');
notice.info(t('user_created'));
onClose();
e.emit(EventType.RELOAD_DATA, null);
} catch (error) {
logger.error(error, '创建用户失败');
logger.error(error, 'Failed to create user');
notice.error(error.message);
}
setLoading(false);
Expand All @@ -67,35 +65,34 @@ function CreateUserDialog() {
return (
<Dialog open={open} maskProps={maskProps}>
<Container>
<Title>创建用户</Title>
<Title>{t('create_user')}</Title>
<StyledContent>
<Input
label="邮箱"
label={t('email')}
inputProps={{
value: email,
onChange: onEmailChange,
type: 'email',
}}
/>
<Input
label="备注"
label={t('remark')}
inputProps={{
value: remark,
onChange: onRemarkChange,
placeholder: '可选',
}}
/>
</StyledContent>
<Action>
<Button onClick={onClose} disabled={loading}>
取消
{t('cancel')}
</Button>
<Button
variant={Variant.PRIMARY}
loading={loading}
onClick={onCreate}
>
创建
{t('create')}
</Button>
</Action>
</Container>
Expand Down
1 change: 1 addition & 0 deletions shared/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const SINGER_MODIFY_RECORD_TTL = 1000 * 60 * 60 * 24 * 180;
export enum Language {
EN = 'en',
ZH_HANS = 'zh-hans',
JA = 'ja',
}
export const DEFAULT_LANGUAGE = Language.EN;

Expand Down

0 comments on commit 971c502

Please sign in to comment.