diff --git a/docs/docs/api/common.md b/docs/docs/api/common.md index 966e2277f..6613547ea 100644 --- a/docs/docs/api/common.md +++ b/docs/docs/api/common.md @@ -145,6 +145,22 @@ const { intl, getLocale, setLocale } = common.utils.createIntl({ ``` +#### intl + +i18n 转换方法 + +```typescript +/** + * i18n 转换方法 + */ +intl(data: IPublicTypeI18nData | string, params?: object): string; +``` + +##### 示例 +``` +const title = common.utils.intl(node.title) +``` + ### skeletonCabin #### Workbench 编辑器框架 View diff --git a/packages/editor-core/src/intl/index.ts b/packages/editor-core/src/intl/index.ts index 6d9d840c3..99e99a4fb 100644 --- a/packages/editor-core/src/intl/index.ts +++ b/packages/editor-core/src/intl/index.ts @@ -3,6 +3,7 @@ import { IntlMessageFormat } from 'intl-messageformat'; import { globalLocale } from './global-locale'; import { isI18nData } from '@alilc/lowcode-utils'; import { observer } from '../utils'; +import { IPublicTypeI18nData } from '@alilc/lowcode-types'; function generateTryLocales(locale: string) { const tries = [locale, locale.replace('-', '_')]; @@ -26,18 +27,9 @@ function injectVars(msg: string, params: any, locale: string): string { } const formater = new IntlMessageFormat(msg, locale); return formater.format(params as any) as string; - /* - - return template.replace(/({\w+})/g, (_, $1) => { - const key = (/\d+/.exec($1) || [])[0] as any; - if (key && params[key] != null) { - return params[key]; - } - return $1; - }); */ } -export function intl(data: any, params?: object): ReactNode { +export function intl(data: IPublicTypeI18nData | string, params?: object): ReactNode { if (!isI18nData(data)) { return data; } diff --git a/packages/shell/src/api/common.tsx b/packages/shell/src/api/common.tsx index a2e4d8c4f..8ce07153a 100644 --- a/packages/shell/src/api/common.tsx +++ b/packages/shell/src/api/common.tsx @@ -26,6 +26,7 @@ import { IPublicApiCommonEditorCabin, IPublicModelDragon, IPublicModelSettingField, + IPublicTypeI18nData, } from '@alilc/lowcode-types'; import { SettingField as InnerSettingField, @@ -261,6 +262,10 @@ class Utils implements IPublicApiCommonUtils { } { return innerCreateIntl(instance); } + + intl(data: IPublicTypeI18nData | string, params?: object): any { + return innerIntl(data, params); + } } class EditorCabin implements IPublicApiCommonEditorCabin { diff --git a/packages/types/src/shell/api/common.ts b/packages/types/src/shell/api/common.ts index 60fac1606..05ef0da17 100644 --- a/packages/types/src/shell/api/common.ts +++ b/packages/types/src/shell/api/common.ts @@ -1,6 +1,6 @@ import { Component, ReactNode } from 'react'; -import { IPublicTypeNodeSchema, IPublicTypeTitleContent } from '../type'; +import { IPublicTypeI18nData, IPublicTypeNodeSchema, IPublicTypeTitleContent } from '../type'; import { IPublicEnumTransitionType } from '../enum'; export interface IPublicApiCommonUtils { @@ -69,6 +69,11 @@ export interface IPublicApiCommonUtils { getLocale(): string; setLocale(locale: string): void; }; + + /** + * i18n 转换方法 + */ + intl(data: IPublicTypeI18nData | string, params?: object): string; } export interface IPublicApiCommonSkeletonCabin {