Skip to content

Commit a7d3996

Browse files
liujupingJackLian
authored andcommitted
feat(common): add common.utils.intl API
1 parent a00c5c9 commit a7d3996

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

docs/docs/api/common.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,22 @@ const { intl, getLocale, setLocale } = common.utils.createIntl({
145145

146146
```
147147

148+
#### intl
149+
150+
i18n 转换方法
151+
152+
```typescript
153+
/**
154+
* i18n 转换方法
155+
*/
156+
intl(data: IPublicTypeI18nData | string, params?: object): string;
157+
```
158+
159+
##### 示例
160+
```
161+
const title = common.utils.intl(node.title)
162+
```
163+
148164
### skeletonCabin
149165
#### Workbench
150166
编辑器框架 View

packages/editor-core/src/intl/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { IntlMessageFormat } from 'intl-messageformat';
33
import { globalLocale } from './global-locale';
44
import { isI18nData } from '@alilc/lowcode-utils';
55
import { observer } from '../utils';
6+
import { IPublicTypeI18nData } from '@alilc/lowcode-types';
67

78
function generateTryLocales(locale: string) {
89
const tries = [locale, locale.replace('-', '_')];
@@ -26,18 +27,9 @@ function injectVars(msg: string, params: any, locale: string): string {
2627
}
2728
const formater = new IntlMessageFormat(msg, locale);
2829
return formater.format(params as any) as string;
29-
/*
30-
31-
return template.replace(/({\w+})/g, (_, $1) => {
32-
const key = (/\d+/.exec($1) || [])[0] as any;
33-
if (key && params[key] != null) {
34-
return params[key];
35-
}
36-
return $1;
37-
}); */
3830
}
3931

40-
export function intl(data: any, params?: object): ReactNode {
32+
export function intl(data: IPublicTypeI18nData | string, params?: object): ReactNode {
4133
if (!isI18nData(data)) {
4234
return data;
4335
}

packages/shell/src/api/common.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
IPublicApiCommonEditorCabin,
2727
IPublicModelDragon,
2828
IPublicModelSettingField,
29+
IPublicTypeI18nData,
2930
} from '@alilc/lowcode-types';
3031
import {
3132
SettingField as InnerSettingField,
@@ -261,6 +262,10 @@ class Utils implements IPublicApiCommonUtils {
261262
} {
262263
return innerCreateIntl(instance);
263264
}
265+
266+
intl(data: IPublicTypeI18nData | string, params?: object): any {
267+
return innerIntl(data, params);
268+
}
264269
}
265270

266271
class EditorCabin implements IPublicApiCommonEditorCabin {

packages/types/src/shell/api/common.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { Component, ReactNode } from 'react';
3-
import { IPublicTypeNodeSchema, IPublicTypeTitleContent } from '../type';
3+
import { IPublicTypeI18nData, IPublicTypeNodeSchema, IPublicTypeTitleContent } from '../type';
44
import { IPublicEnumTransitionType } from '../enum';
55

66
export interface IPublicApiCommonUtils {
@@ -69,6 +69,11 @@ export interface IPublicApiCommonUtils {
6969
getLocale(): string;
7070
setLocale(locale: string): void;
7171
};
72+
73+
/**
74+
* i18n 转换方法
75+
*/
76+
intl(data: IPublicTypeI18nData | string, params?: object): string;
7277
}
7378
export interface IPublicApiCommonSkeletonCabin {
7479

0 commit comments

Comments
 (0)