fix(plugin-users): add translations (#416)

This commit is contained in:
chenos 2022-05-24 14:14:19 +08:00
parent c851033406
commit 72c3ba4fae
8 changed files with 27 additions and 8 deletions

View File

@ -59,6 +59,7 @@ export class APIClient {
authMiddleware() {
this.axios.interceptors.request.use((config) => {
const token = localStorage.getItem(this.tokenKey);
config.headers['X-Locale'] = localStorage.getItem('NOCOBASE_LANG');
config.headers['X-Hostname'] = window.location.hostname;
if (token) {
config.headers['Authorization'] = `Bearer ${token}`;

View File

@ -1,7 +1,6 @@
import cors from '@koa/cors';
import Database from '@nocobase/database';
import Resourcer from '@nocobase/resourcer';
import { Command } from 'commander';
import i18next from 'i18next';
import { DefaultContext, DefaultState } from 'koa';
import bodyParser from 'koa-bodyparser';
@ -56,7 +55,7 @@ export function registerMiddlewares(app: Application, options: ApplicationOption
const i18n = app.i18n.cloneInstance({ initImmediate: false });
ctx.i18n = i18n;
ctx.t = i18n.t.bind(i18n);
const lng = (ctx.request.query.locale as string) || ctx.acceptsLanguages().shift();
const lng = ctx.get('X-Locale') || (ctx.request.query.locale as string) || ctx.acceptsLanguages().shift() || 'en-US';
if (lng !== '*' && lng) {
i18n.changeLanguage(lng);
}

View File

@ -1,6 +1,7 @@
import { Context, Next } from '@nocobase/actions';
import { PasswordField } from '@nocobase/database';
import crypto from 'crypto';
import { namespace } from '../';
export async function check(ctx: Context, next: Next) {
if (ctx.state.currentUser) {
@ -16,7 +17,7 @@ export async function signin(ctx: Context, next: Next) {
const { uniqueField = 'email', values } = ctx.action.params;
if (!values[uniqueField]) {
ctx.throw(401, '请填写邮箱账号');
ctx.throw(401, ctx.t('Please fill in your email address', { ns: namespace }));
}
const User = ctx.db.getCollection('users');
const user = await User.model.findOne<any>({
@ -25,12 +26,12 @@ export async function signin(ctx: Context, next: Next) {
},
});
if (!user) {
ctx.throw(401, '邮箱账号未注册');
ctx.throw(401, ctx.t('The email is incorrect, please re-enter', { ns: namespace }));
}
const pwd = User.getField<PasswordField>('password');
const isValid = await pwd.verify(values.password, user.password);
if (!isValid) {
ctx.throw(401, '密码错误,请您重新输入');
ctx.throw(401, ctx.t('The password is incorrect, please re-enter', { ns: namespace }));
}
const pluginUser = ctx.app.getPlugin('@nocobase/plugin-users');
@ -62,7 +63,7 @@ export async function lostpassword(ctx: Context, next: Next) {
values: { email },
} = ctx.action.params;
if (!email) {
ctx.throw(401, '请填写邮箱账号');
ctx.throw(401, ctx.t('Please fill in your email address', { ns: namespace }));
}
const User = ctx.db.getCollection('users');
const user = await User.model.findOne<any>({
@ -71,7 +72,7 @@ export async function lostpassword(ctx: Context, next: Next) {
},
});
if (!user) {
ctx.throw(401, '邮箱账号未注册');
ctx.throw(401, ctx.t('The email is incorrect, please re-enter', { ns: namespace }));
}
user.resetToken = crypto.randomBytes(20).toString('hex');
await user.save();
@ -142,7 +143,7 @@ export async function changePassword(ctx: Context, next: Next) {
const pwd = User.getField<PasswordField>('password');
const isValid = await pwd.verify(oldPassword, user.password);
if (!isValid) {
ctx.throw(401, '密码错误,请您重新输入');
ctx.throw(401, ctx.t('The password is incorrect, please re-enter', { ns: namespace }));
}
user.password = newPassword;
user.save();

View File

@ -1 +1,3 @@
export { default } from './server';
export const namespace = require('../package.json').name;

View File

@ -0,0 +1,4 @@
export default {
'Please fill in your email address': 'Please fill in your email address',
'The password is incorrect, please re-enter': 'The password is incorrect, please re-enter',
};

View File

@ -0,0 +1,3 @@
export { default as enUS } from './en-US';
export { default as zhCN } from './zh-CN';

View File

@ -0,0 +1,5 @@
export default {
'The email is incorrect, please re-enter': '邮箱错误,请重新输入',
'Please fill in your email address': '请填写密码',
'The password is incorrect, please re-enter': '密码错误,请重新输入',
};

View File

@ -1,8 +1,10 @@
import { Collection, Op } from '@nocobase/database';
import { Plugin } from '@nocobase/server';
import { resolve } from 'path';
import { namespace } from './';
import * as actions from './actions/users';
import { JwtOptions, JwtService } from './jwt-service';
import { enUS, zhCN } from './locale';
import * as middlewares from './middlewares';
import { UserModel } from './models/UserModel';
@ -19,6 +21,8 @@ export default class UsersPlugin extends Plugin<UserPluginConfig> {
}
async beforeLoad() {
this.app.i18n.addResources('zh-CN', namespace, zhCN);
this.app.i18n.addResources('en-US', namespace, enUS);
const cmd = this.app.findCommand('install');
if (cmd) {
cmd.requiredOption('-e, --root-email <rootEmail>', '', process.env.INIT_ROOT_EMAIL);