diff --git a/packages/theme/src/services/i18n/i18n.ts b/packages/theme/src/services/i18n/i18n.ts index 30af51aed..5a1f0be15 100644 --- a/packages/theme/src/services/i18n/i18n.ts +++ b/packages/theme/src/services/i18n/i18n.ts @@ -19,10 +19,10 @@ export interface AlainI18NService { /** * 翻译 - * - `interpolateParams` 模板所需要的参数对象 + * - `params` 模板所需要的参数对象 * - `isSafe` 是否返回安全字符,自动调用 `bypassSecurityTrustHtml` */ - fanyi(key: string, interpolateParams?: Object, isSafe?: boolean): string; + fanyi(key: string, params?: Object, isSafe?: boolean): string; /** * 调用 `use` 触发变更通知 diff --git a/packages/theme/src/services/title/index.en-US.md b/packages/theme/src/services/title/index.en-US.md index a32f83b93..7dd8d16e7 100644 --- a/packages/theme/src/services/title/index.en-US.md +++ b/packages/theme/src/services/title/index.en-US.md @@ -16,3 +16,15 @@ Get the `title` value according to the following order: 2. Parse the menu data based on the current URL 3. Get `h1` content in page `alain-default__content-title` or `page-header__title` 4. Default title + +## API + +| Name | Type | Description | +| ---------------------------------------------- | ---------- | ------------------------------- | +| `default` | `property` | Default title of document title | +| `separator` | `property` | Separator | +| `prefix` | `property` | Prefix of document title | +| `suffix` | `property` | Suffix of document title | +| `reverse` | `property` | Whether to reverse | +| `setTitle(title?: string | string[])` | `method` | Set document title | +| `setTitleByI18n(key: string, params?: Object)` | `method` | Set i18n document title | diff --git a/packages/theme/src/services/title/index.zh-CN.md b/packages/theme/src/services/title/index.zh-CN.md index 1c73128fb..95ce3bb98 100644 --- a/packages/theme/src/services/title/index.zh-CN.md +++ b/packages/theme/src/services/title/index.zh-CN.md @@ -16,3 +16,15 @@ type: Service 2. 根据当前 URL 解析菜单数据 3. 页面 `alain-default__content-title` 或 `page-header__title` 中获取 `h1` 内容 4. 默认标题名 + +## API + +| 名称 | 类型 | 描述 | +| ---------------------------------------------- | ---------- | -------------- | +| `default` | `property` | 设置默认标题名 | +| `separator` | `property` | 设置分隔符 | +| `prefix` | `property` | 设置前缀 | +| `suffix` | `property` | 设置后缀 | +| `reverse` | `property` | 设置是否反转 | +| `setTitle(title?: string | string[])` | `method` | 设置标题 | +| `setTitleByI18n(key: string, params?: Object)` | `method` | 设置国际化标题 | diff --git a/packages/theme/src/services/title/title.service.spec.ts b/packages/theme/src/services/title/title.service.spec.ts index 2ce0440f5..457a770d0 100644 --- a/packages/theme/src/services/title/title.service.spec.ts +++ b/packages/theme/src/services/title/title.service.spec.ts @@ -40,14 +40,16 @@ describe('Service: Title', () => { const notPageName = 'Not Page Name'; function genModule(providers: any[] = [], loadI18n = true) { - const i18nProvider: any[] = loadI18n ? [ { provide: ALAIN_I18N_TOKEN, useClass: AlainI18NServiceFake } ] : []; + const i18nProvider: any[] = loadI18n + ? [{ provide: ALAIN_I18N_TOKEN, useClass: AlainI18NServiceFake }] + : []; TestBed.configureTestingModule({ imports: [AlainThemeModule, RouterTestingModule], providers: [ TitleService, MenuService, { provide: Title, useClass: TestTitleService }, - ...i18nProvider + ...i18nProvider, ].concat(providers), }); title = TestBed.get(Title); @@ -205,5 +207,12 @@ describe('Service: Title', () => { i18n.use('en'); expect(srv.setTitle).toHaveBeenCalled(); }); + it('#setTitleByI18n', () => { + genModule([], true); + srv.suffix = alain; + const key = 'aa'; + srv.setTitleByI18n(key); + expect(title.setTitle).toHaveBeenCalledWith(key + ' - ' + alain); + }); }); }); diff --git a/packages/theme/src/services/title/title.service.ts b/packages/theme/src/services/title/title.service.ts index 610b5d267..27eaf098c 100644 --- a/packages/theme/src/services/title/title.service.ts +++ b/packages/theme/src/services/title/title.service.ts @@ -120,6 +120,13 @@ export class TitleService implements OnDestroy { this.title.setTitle(newTitles.join(this._separator)); } + /** + * 设置国际化标题 + */ + setTitleByI18n(key: string, params?: Object) { + this.setTitle(this.i18nSrv.fanyi(key, params)); + } + ngOnDestroy(): void { if (this.i18n$) this.i18n$.unsubscribe(); }