From 95a5cab44ce74c2ca1aaade3cbfc218272adbe1e Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 29 Dec 2023 09:40:17 +0800 Subject: [PATCH] feat: add plugin.t() method --- packages/core/client/src/application/Plugin.ts | 5 +++++ packages/core/server/src/plugin.ts | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/core/client/src/application/Plugin.ts b/packages/core/client/src/application/Plugin.ts index 8dc7b1d82..853aabe1c 100644 --- a/packages/core/client/src/application/Plugin.ts +++ b/packages/core/client/src/application/Plugin.ts @@ -1,3 +1,4 @@ +import { TFuncKey, TOptions } from 'i18next'; import type { Application } from './Application'; export class Plugin { @@ -38,4 +39,8 @@ export class Plugin { async beforeLoad() {} async load() {} + + t(text: TFuncKey | TFuncKey[], options: TOptions = {}) { + return this.app.i18n.t(text, { ns: this.options?.['packageName'], ...(options as any) }); + } } diff --git a/packages/core/server/src/plugin.ts b/packages/core/server/src/plugin.ts index 55b316e2b..0690a84df 100644 --- a/packages/core/server/src/plugin.ts +++ b/packages/core/server/src/plugin.ts @@ -1,10 +1,11 @@ import { Model } from '@nocobase/database'; +import { LoggerOptions } from '@nocobase/logger'; import fs from 'fs'; +import type { TFuncKey, TOptions } from 'i18next'; import { resolve } from 'path'; import { Application } from './application'; import { InstallOptions, getExposeChangelogUrl, getExposeReadmeUrl } from './plugin-manager'; import { checkAndGetCompatible } from './plugin-manager/utils'; -import { LoggerOptions, createLogger, getLoggerFilePath } from '@nocobase/logger'; export interface PluginInterface { beforeLoad?: () => void; @@ -116,6 +117,10 @@ export abstract class Plugin implements PluginInterface { return []; } + t(text: TFuncKey | TFuncKey[], options: TOptions = {}) { + return this.app.i18n.t(text, { ns: this.options['packageName'], ...(options as any) }); + } + async toJSON(options: any = {}) { const { locale = 'en-US' } = options; const { name, packageName, packageJson } = this.options;