Skip to content

Commit

Permalink
i18n improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mebtte committed Jul 28, 2023
1 parent b0d5aaf commit 03ee853
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 30 deletions.
23 changes: 23 additions & 0 deletions apps/pwa/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,27 @@ export default {
create_user: 'create user',
remark: 'remark',
create: 'create',
please_enter_captcha: 'please enter captcha',
wrong_captcha: 'wrong captcha',
delete_user: 'delete user',
delete_user_question: 'are you sure to delete user ?',
delete_user_question_content:
'the music/singer current user created will transfer to your account after deleting',
set_as_admin: 'set as admin',
set_as_admin_question: 'are you sure to set this user as admin ?',
set_as_admin_question_content:
"the privilege of current user is same as your and the user can't be deleted after setting as admin",
save: 'save',
maximum_amount_of_musicbill: 'maximum amount of musicbill',
maximum_amount_of_creating_music_per_day:
'maximum amount of creating music per day',
music_play_record_indate: 'music play record indate',
should_be_greater_than: '%s1 should be greater than %s2',
should_be_greater_than_or_equal_to:
'%s1 should be greater than or equal to %s2',
should_be_less_than_or_equal_to: '%s1 should be less than or equal to %s2',
length_of: 'length of %s1',
nickname: 'nickname',
join_time: 'join time',
zero_means_unlimited: '0 means unlimited',
};
21 changes: 21 additions & 0 deletions apps/pwa/src/i18n/zh_hans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ const zhCN: {
create_user: '创建用户',
remark: '备注',
create: '创建',
please_enter_captcha: '请输入验证码',
wrong_captcha: '错误的验证码',
delete_user: '删除用户',
delete_user_question: '确定删除用户吗?',
delete_user_question_content:
'删除用户后, 其创建的音乐/歌手将会转移到你的账号下',
set_as_admin: '设为管理员',
set_as_admin_question: '确定设为管理员吗?',
set_as_admin_question_content:
'成为管理员后账号将无法被删除, 以及拥有和你一样的权限且无法被撤销管理员身份',
save: '保存',
maximum_amount_of_musicbill: '乐单最大数量',
maximum_amount_of_creating_music_per_day: '每天创建音乐最大数量',
music_play_record_indate: '音乐播放记录保留天数',
should_be_greater_than: '%s1应该大于%s2',
should_be_greater_than_or_equal_to: '%s1应该大于等于%s2',
should_be_less_than_or_equal_to: '%s1应该小于等于%s2',
length_of: '%s1长度',
nickname: '昵称',
join_time: '加入时间',
zero_means_unlimited: '0表示无限制',
};

export default zhCN;
9 changes: 7 additions & 2 deletions apps/pwa/src/pages/login/email_panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,14 @@ function EmailPanel({ toNext }: { toNext: (email: string) => void }) {
toNext(email);
break;
}
default: {
/**
* prevent closing captcha dialog
* @author mebtte<hi@mebtte.com>
*/
return false;
}
}

return false;
}
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import adminUpdateUser from '@/server/api/admin_update_user';
import adminUpdateUserAdmin from '@/server/api/admin_update_user_admin';
import { AdminAllowUpdateKey, REMARK_MAX_LENGTH } from '#/constants/user';
import adminDeleteUser from '@/server/api/admin_delete_user';
import { t } from '@/i18n';
import { User } from '../constants';
import e, { EventType } from '../eventemitter';

Expand Down Expand Up @@ -57,7 +58,9 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {
const musicbillMaxAmountNumber = Number(musicbillMaxAmount);
if (user.musicbillMaxAmount !== musicbillMaxAmountNumber) {
if (musicbillMaxAmountNumber < 0) {
throw new Error('乐单最大数量应大于等于 0');
throw new Error(
t('should_be_greater_than', t('maximum_amount_of_musicbill'), '0'),
);
}
await adminUpdateUser({
id: user.id,
Expand All @@ -77,7 +80,13 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {
user.createMusicMaxAmountPerDay !== createMusicMaxAmountPerDayNumber
) {
if (createMusicMaxAmountPerDayNumber < 0) {
throw new Error('每天创建音乐最大数量应大于等于 0');
throw new Error(
t(
'should_be_greater_than_or_equal_to',
t('maximum_amount_of_creating_music_per_day'),
'0',
),
);
}
await adminUpdateUser({
id: user.id,
Expand All @@ -93,7 +102,13 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {
const musicPlayRecordIndateNumber = Number(musicPlayRecordIndate);
if (user.musicPlayRecordIndate !== musicPlayRecordIndateNumber) {
if (musicPlayRecordIndateNumber < 0) {
throw new Error('音乐播放记录保留天数应大于等于 0');
throw new Error(
t(
'should_be_greater_than_or_equal_to',
t('music_play_record_indate'),
'0',
),
);
}
await adminUpdateUser({
id: user.id,
Expand All @@ -108,7 +123,13 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {

if (user.remark !== remark) {
if (remark.length > REMARK_MAX_LENGTH) {
throw new Error(`备注长度应小于等于 ${REMARK_MAX_LENGTH}`);
throw new Error(
t(
'should_be_less_than_or_equal_to',
t('length_of', t('remark')),
REMARK_MAX_LENGTH.toString(),
),
);
}
await adminUpdateUser({
id: user.id,
Expand All @@ -123,7 +144,7 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {

window.setTimeout(() => onClose(), 0);
} catch (error) {
logger.error(error, '更新用户信息失败');
logger.error(error, 'Failed to update user info');
notice.error(error.message);
}

Expand All @@ -141,27 +162,29 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {
/>
<Input
className="part"
label="昵称"
label={t('nickname')}
disabled
inputProps={{ defaultValue: user.nickname }}
/>
<Input
className="part"
label="邮箱"
label={t('email')}
disabled
inputProps={{ defaultValue: user.email }}
/>
<Input
className="part"
label="加入时间"
label={t('join_time')}
disabled
inputProps={{
defaultValue: day(user.joinTimestamp).format('YYYY-MM-DD'),
}}
/>
<Input
className="part"
label="乐单最大数量(0 表示无限制)"
label={`${t('maximum_amount_of_musicbill')}(${t(
'zero_means_unlimited',
)})`}
disabled={loading}
inputProps={{
value: musicbillMaxAmount,
Expand All @@ -170,7 +193,9 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {
/>
<Input
className="part"
label="每天创建音乐最大数量(0 表示无限制)"
label={`${t('maximum_amount_of_creating_music_per_day')}(${t(
'zero_means_unlimited',
)})`}
disabled={loading}
inputProps={{
value: createMusicMaxAmountPerDay,
Expand All @@ -179,7 +204,9 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {
/>
<Input
className="part"
label="音乐播放记录保留天数(0 表示无限制)"
label={`${t('music_play_record_indate')}(${t(
'zero_means_unlimited',
)})`}
disabled={loading}
inputProps={{
value: musicPlayRecordIndate,
Expand All @@ -188,7 +215,7 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {
/>
<Textarea
className="part"
label="备注"
label={t('remark')}
disabled={loading}
textareaProps={{ value: remark, onChange: onRemarkChange, rows: 5 }}
/>
Expand All @@ -198,21 +225,20 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {
onClick={onSave}
loading={loading}
>
保存
{t('save')}
</Button>
{user.admin ? null : (
<Button
className="part"
disabled={loading}
onClick={() =>
dialog.confirm({
title: '确定设为管理员吗?',
content:
'成为管理员后账号将无法被删除, 以及拥有和你一样的权限且无法被撤销管理员身份',
confirmText: '继续',
title: t('set_as_admin_question'),
content: t('set_as_admin_question_content'),
confirmText: t('continue'),
onConfirm: () =>
void dialog.captcha({
confirmText: '设为管理员',
confirmText: t('set_as_admin'),
confirmVariant: Variant.PRIMARY,
onConfirm: async ({ captchaId, captchaValue }) => {
try {
Expand All @@ -227,7 +253,7 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {
admin: 1,
});
} catch (error) {
logger.error(error, '设为管理员失败');
logger.error(error, 'Failed to set admin');
notice.error(error.message);
return false;
}
Expand All @@ -236,7 +262,7 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {
})
}
>
设为管理员
{t('set_as_admin')}
</Button>
)}
{user.admin ? null : (
Expand All @@ -246,12 +272,12 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {
disabled={loading}
onClick={() =>
dialog.confirm({
title: '确定删除用户吗?',
content: '用户删除后, 其创建的音乐/歌手将会转移到你的账号',
confirmText: '继续',
title: t('delete_user_question'),
content: t('delete_user_question_content'),
confirmText: t('continue'),
onConfirm: () =>
void dialog.captcha({
confirmText: '删除用户',
confirmText: t('delete_user'),
confirmVariant: Variant.DANGER,
onConfirm: async ({ captchaId, captchaValue }) => {
try {
Expand All @@ -263,7 +289,7 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {
onClose();
e.emit(EventType.USER_DELETED, { id: user.id });
} catch (error) {
logger.error(error, '删除用户失败');
logger.error(error, 'Failed to delete user');
notice.error(error.message);
return false;
}
Expand All @@ -272,7 +298,7 @@ function UserEdit({ user, onClose }: { user: User; onClose: () => void }) {
})
}
>
删除用户
{t('delete_user')}
</Button>
)}
</Style>
Expand Down
4 changes: 2 additions & 2 deletions apps/pwa/src/utils/dialog/captcha/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ function CaptchaContent({
const [confirming, setConfirming] = useState(false);
const onConfirm = useEvent(() => {
if (!captchaData.data) {
return notice.error('请等待验证码加载完毕');
return notice.error(t('wrong_captcha'));
}

if (!captchaValue) {
return notice.error('请输入验证码');
return notice.error(t('please_enter_captcha'));
}

setConfirming(true);
Expand Down

0 comments on commit 03ee853

Please sign in to comment.