Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module:theme:title): add setTitleByI18n method #299

Merged
merged 1 commit into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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