diff --git a/packages/core/client/src/i18n/i18n.ts b/packages/core/client/src/i18n/i18n.ts index 3e2d37edc5..b0618a2a00 100644 --- a/packages/core/client/src/i18n/i18n.ts +++ b/packages/core/client/src/i18n/i18n.ts @@ -11,6 +11,10 @@ import i18next, { TFuncKey, TOptions } from 'i18next'; import { initReactI18next } from 'react-i18next'; import locale from '../locale'; +/** + * @deprecated + * use {@link @nocobase/utils/client#tval} instead + */ export function tval(text: TFuncKey | TFuncKey[], options?: TOptions) { if (options) { return `{{t(${JSON.stringify(text)}, ${JSON.stringify(options)})}}`; diff --git a/packages/core/utils/src/client.ts b/packages/core/utils/src/client.ts index 4f7a444a13..a3f82e0d69 100644 --- a/packages/core/utils/src/client.ts +++ b/packages/core/utils/src/client.ts @@ -29,3 +29,4 @@ export * from './uid'; export * from './url'; export { dayjs, lodash }; export * from './parseHTML'; +export * from './i18n'; diff --git a/packages/core/utils/src/i18n.ts b/packages/core/utils/src/i18n.ts new file mode 100644 index 0000000000..0eb580c28e --- /dev/null +++ b/packages/core/utils/src/i18n.ts @@ -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)})}}`; +} diff --git a/packages/core/utils/src/index.ts b/packages/core/utils/src/index.ts index fd1751e3b0..0189b23696 100644 --- a/packages/core/utils/src/index.ts +++ b/packages/core/utils/src/index.ts @@ -33,5 +33,6 @@ export * from './requireModule'; export * from './toposort'; export * from './uid'; export * from './url'; +export * from './i18n'; export { dayjs, lodash }; diff --git a/packages/plugins/@nocobase/plugin-auth-sms/src/locale/en-US.json b/packages/plugins/@nocobase/plugin-auth-sms/src/locale/en-US.json index 4d5089e437..6d2fed4793 100644 --- a/packages/plugins/@nocobase/plugin-auth-sms/src/locale/en-US.json +++ b/packages/plugins/@nocobase/plugin-auth-sms/src/locale/en-US.json @@ -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" + "Sign up automatically when the user does not exist": "Sign up automatically when the user does not exist", + "SMS": "SMS" } diff --git a/packages/plugins/@nocobase/plugin-auth-sms/src/locale/zh-CN.json b/packages/plugins/@nocobase/plugin-auth-sms/src/locale/zh-CN.json index 22ec1e4070..70dcf2f0fa 100644 --- a/packages/plugins/@nocobase/plugin-auth-sms/src/locale/zh-CN.json +++ b/packages/plugins/@nocobase/plugin-auth-sms/src/locale/zh-CN.json @@ -1,5 +1,6 @@ { "Sign in via SMS": "短信登录", "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": "短信" } diff --git a/packages/plugins/@nocobase/plugin-auth-sms/src/server/plugin.ts b/packages/plugins/@nocobase/plugin-auth-sms/src/server/plugin.ts index c6f042fe2d..a24666e8e6 100644 --- a/packages/plugins/@nocobase/plugin-auth-sms/src/server/plugin.ts +++ b/packages/plugins/@nocobase/plugin-auth-sms/src/server/plugin.ts @@ -9,9 +9,9 @@ import VerificationPlugin from '@nocobase/plugin-verification'; import { InstallOptions, Plugin } from '@nocobase/server'; -import { resolve } from 'path'; -import { authType } from '../constants'; +import { authType, namespace } from '../constants'; import { SMSAuth } from './sms-auth'; +import { tval } from '@nocobase/utils'; export class PluginAuthSMSServer extends Plugin { afterAdd() {} @@ -38,6 +38,7 @@ export class PluginAuthSMSServer extends Plugin { this.app.authManager.registerTypes(authType, { auth: SMSAuth, + title: tval('SMS', { ns: namespace }), }); } diff --git a/packages/plugins/@nocobase/plugin-auth/src/client/settings/Authenticator.tsx b/packages/plugins/@nocobase/plugin-auth/src/client/settings/Authenticator.tsx index c95737517d..d10d8216c3 100644 --- a/packages/plugins/@nocobase/plugin-auth/src/client/settings/Authenticator.tsx +++ b/packages/plugins/@nocobase/plugin-auth/src/client/settings/Authenticator.tsx @@ -24,6 +24,7 @@ import { AuthTypeContext, AuthTypesContext, useAuthTypes } from './authType'; import { useValuesFromOptions, Options } from './Options'; import { useTranslation } from 'react-i18next'; import { useAuthTranslation } from '../locale'; +import { Schema } from '@formily/react'; const useCloseAction = () => { const { setVisible } = useActionContext(); @@ -81,7 +82,7 @@ export const Authenticator = () => { const types = res?.data?.data || []; return types.map((type: { name: string; title?: string }) => ({ key: type.name, - label: t(type.title || type.name), + label: Schema.compile(type.title || type.name, { t }), value: type.name, })); }), diff --git a/packages/plugins/@nocobase/plugin-auth/src/locale/en-US.json b/packages/plugins/@nocobase/plugin-auth/src/locale/en-US.json index 1386165bf1..f6f77a628c 100644 --- a/packages/plugins/@nocobase/plugin-auth/src/locale/en-US.json +++ b/packages/plugins/@nocobase/plugin-auth/src/locale/en-US.json @@ -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": "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" + "Sign-in": "Sign-in", + "Password": "Password" } diff --git a/packages/plugins/@nocobase/plugin-auth/src/locale/zh-CN.json b/packages/plugins/@nocobase/plugin-auth/src/locale/zh-CN.json index aeb0d18021..1e244da8a7 100644 --- a/packages/plugins/@nocobase/plugin-auth/src/locale/zh-CN.json +++ b/packages/plugins/@nocobase/plugin-auth/src/locale/zh-CN.json @@ -17,11 +17,11 @@ "Please enter a valid email": "请输入有效的邮箱", "Please enter your username or email": "请输入用户名或邮箱", "Please enter a password": "请输入密码", - "SMS": "短信", "Username/Email": "用户名/邮箱", "Auth UID": "认证标识", "The authentication allows users to sign in via username or email.": "该认证方式支持用户通过用户名或邮箱登录。", "No authentication methods available.": "没有可用的认证方式。", "The password is inconsistent, please re-enter": "密码不一致,请重新输入", - "Sign-in": "登录" + "Sign-in": "登录", + "Password": "密码" } diff --git a/packages/plugins/@nocobase/plugin-auth/src/server/__tests__/actions.test.ts b/packages/plugins/@nocobase/plugin-auth/src/server/__tests__/actions.test.ts index 0c002c2772..c5f87ca261 100644 --- a/packages/plugins/@nocobase/plugin-auth/src/server/__tests__/actions.test.ts +++ b/packages/plugins/@nocobase/plugin-auth/src/server/__tests__/actions.test.ts @@ -38,10 +38,9 @@ describe('actions', () => { it('should list authenticator types', async () => { const res = await agent.resource('authenticators').listTypes(); - expect(res.body.data).toEqual([ + expect(res.body.data).toMatchObject([ { name: 'Email/Password', - title: 'Password', }, ]); }); diff --git a/packages/plugins/@nocobase/plugin-auth/src/server/plugin.ts b/packages/plugins/@nocobase/plugin-auth/src/server/plugin.ts index 62827c536a..730d43b736 100644 --- a/packages/plugins/@nocobase/plugin-auth/src/server/plugin.ts +++ b/packages/plugins/@nocobase/plugin-auth/src/server/plugin.ts @@ -15,19 +15,16 @@ import { namespace, presetAuthType, presetAuthenticator } from '../preset'; import authActions from './actions/auth'; import authenticatorsActions from './actions/authenticators'; import { BasicAuth } from './basic-auth'; -import { enUS, zhCN } from './locale'; import { AuthModel } from './model/authenticator'; import { Storer } from './storer'; import { TokenBlacklistService } from './token-blacklist'; +import { tval } from '@nocobase/utils'; export class PluginAuthServer extends Plugin { cache: Cache; afterAdd() {} async beforeLoad() { - this.app.i18n.addResources('zh-CN', namespace, zhCN); - this.app.i18n.addResources('en-US', namespace, enUS); - this.app.db.registerModels({ AuthModel }); } @@ -61,7 +58,7 @@ export class PluginAuthServer extends Plugin { this.app.authManager.registerTypes(presetAuthType, { auth: BasicAuth, - title: 'Password', + title: tval('Password', { ns: namespace }), }); // Register actions Object.entries(authActions).forEach( diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/client/component/CollectionsManager/EditCollectionAction.tsx b/packages/plugins/@nocobase/plugin-data-source-manager/src/client/component/CollectionsManager/EditCollectionAction.tsx index 09d37d4a1e..161f25d3d0 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/client/component/CollectionsManager/EditCollectionAction.tsx +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/client/component/CollectionsManager/EditCollectionAction.tsx @@ -10,12 +10,12 @@ import { ArrayTable } from '@formily/antd-v5'; import { ISchema, useForm } from '@formily/react'; import { uid } from '@formily/shared'; +import { tval } from '@nocobase/utils/client'; import { ActionContextProvider, IField, RecordProvider, SchemaComponent, - tval, useAPIClient, useActionContext, useCollectionManager_deprecated, diff --git a/packages/plugins/@nocobase/plugin-field-markdown-vditor/src/client/locale/index.ts b/packages/plugins/@nocobase/plugin-field-markdown-vditor/src/client/locale/index.ts index bf5e0cf890..5c461f7c06 100644 --- a/packages/plugins/@nocobase/plugin-field-markdown-vditor/src/client/locale/index.ts +++ b/packages/plugins/@nocobase/plugin-field-markdown-vditor/src/client/locale/index.ts @@ -7,10 +7,10 @@ * 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'; export function generateNTemplate(key: string) { return tval(key, { ns: NAMESPACE }) -} \ No newline at end of file +} diff --git a/packages/plugins/@nocobase/plugin-multi-app-manager/src/client/settings/schemas/applications.ts b/packages/plugins/@nocobase/plugin-multi-app-manager/src/client/settings/schemas/applications.ts index 23278302fe..822a3214c1 100644 --- a/packages/plugins/@nocobase/plugin-multi-app-manager/src/client/settings/schemas/applications.ts +++ b/packages/plugins/@nocobase/plugin-multi-app-manager/src/client/settings/schemas/applications.ts @@ -9,9 +9,9 @@ import { ISchema } from '@formily/react'; import { uid } from '@formily/shared'; +import { tval } from '@nocobase/utils/client'; import { SchemaComponentOptions, - tval, useActionContext, useRecord, useRequest, diff --git a/packages/plugins/@nocobase/plugin-users/src/client/index.ts b/packages/plugins/@nocobase/plugin-users/src/client/index.ts index 22b161cd2c..8264bf4f91 100644 --- a/packages/plugins/@nocobase/plugin-users/src/client/index.ts +++ b/packages/plugins/@nocobase/plugin-users/src/client/index.ts @@ -7,7 +7,8 @@ * 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 ACLPlugin from '@nocobase/plugin-acl/client'; import { RoleUsersManager } from './RoleUsersManager';