Skip to content

Commit

Permalink
feat(common): add common.utils.intl API
Browse files Browse the repository at this point in the history
  • Loading branch information
liujuping committed Jan 11, 2024
1 parent a00c5c9 commit 4b38603
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
16 changes: 16 additions & 0 deletions docs/docs/api/common.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions packages/editor-core/src/intl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('-', '_')];
Expand All @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/shell/src/api/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
IPublicApiCommonEditorCabin,
IPublicModelDragon,
IPublicModelSettingField,
IPublicTypeI18nData,
} from '@alilc/lowcode-types';
import {
SettingField as InnerSettingField,
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion packages/types/src/shell/api/common.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -69,6 +69,11 @@ export interface IPublicApiCommonUtils {
getLocale(): string;
setLocale(locale: string): void;
};

/**
* i18n 转换方法
*/
intl(data: IPublicTypeI18nData | string, params?: object): string;
}
export interface IPublicApiCommonSkeletonCabin {

Expand Down

0 comments on commit 4b38603

Please sign in to comment.