fix(auth): allows to set the auth type title translation from server end (#4616)

* fix(auth): allows to set auth type title translation from server end

* chore: move tval to utils

* chore: mark deprecated

* fix: test
This commit is contained in:
YANG QIA 2024-06-11 14:25:07 +08:00 committed by GitHub
parent b9fda22fb1
commit 0fb18b495f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 45 additions and 21 deletions

View File

@ -11,6 +11,10 @@ import i18next, { TFuncKey, TOptions } from 'i18next';
import { initReactI18next } from 'react-i18next'; import { initReactI18next } from 'react-i18next';
import locale from '../locale'; import locale from '../locale';
/**
* @deprecated
* use {@link @nocobase/utils/client#tval} instead
*/
export function tval(text: TFuncKey | TFuncKey[], options?: TOptions) { export function tval(text: TFuncKey | TFuncKey[], options?: TOptions) {
if (options) { if (options) {
return `{{t(${JSON.stringify(text)}, ${JSON.stringify(options)})}}`; return `{{t(${JSON.stringify(text)}, ${JSON.stringify(options)})}}`;

View File

@ -29,3 +29,4 @@ export * from './uid';
export * from './url'; export * from './url';
export { dayjs, lodash }; export { dayjs, lodash };
export * from './parseHTML'; export * from './parseHTML';
export * from './i18n';

View File

@ -0,0 +1,17 @@
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { TFuncKey, TOptions } from 'i18next';
export function tval(text: TFuncKey | TFuncKey[], options?: TOptions) {
if (options) {
return `{{t(${JSON.stringify(text)}, ${JSON.stringify(options)})}}`;
}
return `{{t(${JSON.stringify(text)})}}`;
}

View File

@ -33,5 +33,6 @@ export * from './requireModule';
export * from './toposort'; export * from './toposort';
export * from './uid'; export * from './uid';
export * from './url'; export * from './url';
export * from './i18n';
export { dayjs, lodash }; export { dayjs, lodash };

View File

@ -1,5 +1,6 @@
{ {
"Sign in via SMS": "Sign in via SMS", "Sign in via SMS": "Sign in via SMS",
"User will be registered automatically if not exists.": "User will be registered automatically if not exists.", "User will be registered automatically if not exists.": "User will be registered automatically if not exists.",
"Sign up automatically when the user does not exist": "Sign up automatically when the user does not exist" "Sign up automatically when the user does not exist": "Sign up automatically when the user does not exist",
"SMS": "SMS"
} }

View File

@ -1,5 +1,6 @@
{ {
"Sign in via SMS": "短信登录", "Sign in via SMS": "短信登录",
"User will be registered automatically if not exists.": "用户不存在时将自动注册。", "User will be registered automatically if not exists.": "用户不存在时将自动注册。",
"Sign up automatically when the user does not exist": "用户不存在时自动注册" "Sign up automatically when the user does not exist": "用户不存在时自动注册",
"SMS": "短信"
} }

View File

@ -9,9 +9,9 @@
import VerificationPlugin from '@nocobase/plugin-verification'; import VerificationPlugin from '@nocobase/plugin-verification';
import { InstallOptions, Plugin } from '@nocobase/server'; import { InstallOptions, Plugin } from '@nocobase/server';
import { resolve } from 'path'; import { authType, namespace } from '../constants';
import { authType } from '../constants';
import { SMSAuth } from './sms-auth'; import { SMSAuth } from './sms-auth';
import { tval } from '@nocobase/utils';
export class PluginAuthSMSServer extends Plugin { export class PluginAuthSMSServer extends Plugin {
afterAdd() {} afterAdd() {}
@ -38,6 +38,7 @@ export class PluginAuthSMSServer extends Plugin {
this.app.authManager.registerTypes(authType, { this.app.authManager.registerTypes(authType, {
auth: SMSAuth, auth: SMSAuth,
title: tval('SMS', { ns: namespace }),
}); });
} }

View File

@ -24,6 +24,7 @@ import { AuthTypeContext, AuthTypesContext, useAuthTypes } from './authType';
import { useValuesFromOptions, Options } from './Options'; import { useValuesFromOptions, Options } from './Options';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useAuthTranslation } from '../locale'; import { useAuthTranslation } from '../locale';
import { Schema } from '@formily/react';
const useCloseAction = () => { const useCloseAction = () => {
const { setVisible } = useActionContext(); const { setVisible } = useActionContext();
@ -81,7 +82,7 @@ export const Authenticator = () => {
const types = res?.data?.data || []; const types = res?.data?.data || [];
return types.map((type: { name: string; title?: string }) => ({ return types.map((type: { name: string; title?: string }) => ({
key: type.name, key: type.name,
label: t(type.title || type.name), label: Schema.compile(type.title || type.name, { t }),
value: type.name, value: type.name,
})); }));
}), }),

View File

@ -17,11 +17,11 @@
"Please enter a valid email": "Please enter a valid email", "Please enter a valid email": "Please enter a valid email",
"Please enter your username or email": "Please enter your username or email", "Please enter your username or email": "Please enter your username or email",
"Please enter a password": "Please enter a password", "Please enter a password": "Please enter a password",
"SMS": "SMS",
"Username/Email": "Username/Email", "Username/Email": "Username/Email",
"Auth UID": "Auth UID", "Auth UID": "Auth UID",
"The authentication allows users to sign in via username or email.": "The authentication allows users to sign in via username or email.", "The authentication allows users to sign in via username or email.": "The authentication allows users to sign in via username or email.",
"No authentication methods available.": "No authentication methods available.", "No authentication methods available.": "No authentication methods available.",
"The password is inconsistent, please re-enter": "The password is inconsistent, please re-enter", "The password is inconsistent, please re-enter": "The password is inconsistent, please re-enter",
"Sign-in": "Sign-in" "Sign-in": "Sign-in",
"Password": "Password"
} }

View File

@ -17,11 +17,11 @@
"Please enter a valid email": "请输入有效的邮箱", "Please enter a valid email": "请输入有效的邮箱",
"Please enter your username or email": "请输入用户名或邮箱", "Please enter your username or email": "请输入用户名或邮箱",
"Please enter a password": "请输入密码", "Please enter a password": "请输入密码",
"SMS": "短信",
"Username/Email": "用户名/邮箱", "Username/Email": "用户名/邮箱",
"Auth UID": "认证标识", "Auth UID": "认证标识",
"The authentication allows users to sign in via username or email.": "该认证方式支持用户通过用户名或邮箱登录。", "The authentication allows users to sign in via username or email.": "该认证方式支持用户通过用户名或邮箱登录。",
"No authentication methods available.": "没有可用的认证方式。", "No authentication methods available.": "没有可用的认证方式。",
"The password is inconsistent, please re-enter": "密码不一致,请重新输入", "The password is inconsistent, please re-enter": "密码不一致,请重新输入",
"Sign-in": "登录" "Sign-in": "登录",
"Password": "密码"
} }

View File

@ -38,10 +38,9 @@ describe('actions', () => {
it('should list authenticator types', async () => { it('should list authenticator types', async () => {
const res = await agent.resource('authenticators').listTypes(); const res = await agent.resource('authenticators').listTypes();
expect(res.body.data).toEqual([ expect(res.body.data).toMatchObject([
{ {
name: 'Email/Password', name: 'Email/Password',
title: 'Password',
}, },
]); ]);
}); });

View File

@ -15,19 +15,16 @@ import { namespace, presetAuthType, presetAuthenticator } from '../preset';
import authActions from './actions/auth'; import authActions from './actions/auth';
import authenticatorsActions from './actions/authenticators'; import authenticatorsActions from './actions/authenticators';
import { BasicAuth } from './basic-auth'; import { BasicAuth } from './basic-auth';
import { enUS, zhCN } from './locale';
import { AuthModel } from './model/authenticator'; import { AuthModel } from './model/authenticator';
import { Storer } from './storer'; import { Storer } from './storer';
import { TokenBlacklistService } from './token-blacklist'; import { TokenBlacklistService } from './token-blacklist';
import { tval } from '@nocobase/utils';
export class PluginAuthServer extends Plugin { export class PluginAuthServer extends Plugin {
cache: Cache; cache: Cache;
afterAdd() {} afterAdd() {}
async beforeLoad() { async beforeLoad() {
this.app.i18n.addResources('zh-CN', namespace, zhCN);
this.app.i18n.addResources('en-US', namespace, enUS);
this.app.db.registerModels({ AuthModel }); this.app.db.registerModels({ AuthModel });
} }
@ -61,7 +58,7 @@ export class PluginAuthServer extends Plugin {
this.app.authManager.registerTypes(presetAuthType, { this.app.authManager.registerTypes(presetAuthType, {
auth: BasicAuth, auth: BasicAuth,
title: 'Password', title: tval('Password', { ns: namespace }),
}); });
// Register actions // Register actions
Object.entries(authActions).forEach( Object.entries(authActions).forEach(

View File

@ -10,12 +10,12 @@
import { ArrayTable } from '@formily/antd-v5'; import { ArrayTable } from '@formily/antd-v5';
import { ISchema, useForm } from '@formily/react'; import { ISchema, useForm } from '@formily/react';
import { uid } from '@formily/shared'; import { uid } from '@formily/shared';
import { tval } from '@nocobase/utils/client';
import { import {
ActionContextProvider, ActionContextProvider,
IField, IField,
RecordProvider, RecordProvider,
SchemaComponent, SchemaComponent,
tval,
useAPIClient, useAPIClient,
useActionContext, useActionContext,
useCollectionManager_deprecated, useCollectionManager_deprecated,

View File

@ -7,10 +7,10 @@
* For more information, please refer to: https://www.nocobase.com/agreement. * For more information, please refer to: https://www.nocobase.com/agreement.
*/ */
import { tval } from '@nocobase/client'; import { tval } from '@nocobase/utils/client';
const NAMESPACE = 'field-markdown-vditor'; const NAMESPACE = 'field-markdown-vditor';
export function generateNTemplate(key: string) { export function generateNTemplate(key: string) {
return tval(key, { ns: NAMESPACE }) return tval(key, { ns: NAMESPACE })
} }

View File

@ -9,9 +9,9 @@
import { ISchema } from '@formily/react'; import { ISchema } from '@formily/react';
import { uid } from '@formily/shared'; import { uid } from '@formily/shared';
import { tval } from '@nocobase/utils/client';
import { import {
SchemaComponentOptions, SchemaComponentOptions,
tval,
useActionContext, useActionContext,
useRecord, useRecord,
useRequest, useRequest,

View File

@ -7,7 +7,8 @@
* For more information, please refer to: https://www.nocobase.com/agreement. * For more information, please refer to: https://www.nocobase.com/agreement.
*/ */
import { Plugin, tval } from '@nocobase/client'; import { Plugin } from '@nocobase/client';
import { tval } from '@nocobase/utils/client';
import { UsersManagement } from './UsersManagement'; import { UsersManagement } from './UsersManagement';
import ACLPlugin from '@nocobase/plugin-acl/client'; import ACLPlugin from '@nocobase/plugin-acl/client';
import { RoleUsersManager } from './RoleUsersManager'; import { RoleUsersManager } from './RoleUsersManager';