Skip to content

Commit

Permalink
feat(module:theme:title): add setTitleByI18n method (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Nov 27, 2018
1 parent 09d1046 commit 80a9636
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/theme/src/services/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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` 触发变更通知
Expand Down
12 changes: 12 additions & 0 deletions packages/theme/src/services/title/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
12 changes: 12 additions & 0 deletions packages/theme/src/services/title/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` | 设置国际化标题 |
13 changes: 11 additions & 2 deletions packages/theme/src/services/title/title.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
});
});
7 changes: 7 additions & 0 deletions packages/theme/src/services/title/title.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 80a9636

Please sign in to comment.