[ADF-3028] i18n support for title service (#3342)

* i18n support for title service

* cleanup tests

* update tests
This commit is contained in:
Denys Vuika
2018-05-18 10:51:13 +01:00
committed by Eugenio Romano
parent b00eb1433e
commit 53cf5acc86
6 changed files with 75 additions and 31 deletions

View File

@@ -28,9 +28,9 @@ export class TranslationMock {
defaultLang: string = 'en';
userLang: string;
customLoader: any;
translate: any;
onLangChange: EventEmitter<LangChangeEvent> = new EventEmitter<LangChangeEvent>();
translate = {
onLangChange: new EventEmitter<LangChangeEvent>()
};
addTranslationFolder() {

View File

@@ -20,6 +20,9 @@ import { Title } from '@angular/platform-browser';
import { AppConfigService } from '../app-config/app-config.service';
import { PageTitleService } from './page-title.service';
import { CoreTestingModule } from '../testing/core.testing.module';
import { TranslationService } from './translation.service';
import { TranslationMock } from '../mock/translation.service.mock';
class TestConfig {
private setup: any = {
@@ -28,6 +31,7 @@ class TestConfig {
titleService: Title = null;
appTitleService: PageTitleService = null;
translationService: TranslationService;
constructor(setup: any = {}) {
Object.assign(this.setup, setup);
@@ -53,48 +57,69 @@ class TestConfig {
};
TestBed.configureTestingModule({
imports: [
CoreTestingModule
],
providers: [
titleServiceProvider,
appConfigProvider,
PageTitleService
PageTitleService,
{
provide: TranslationService,
useClass: TranslationMock
}
]
});
inject([ Title, PageTitleService ], (titleService, appTitleService) => {
inject([ Title, PageTitleService, TranslationService ], (titleService, appTitleService, translationService) => {
this.titleService = titleService;
this.appTitleService = appTitleService;
this.translationService = translationService;
})();
}
}
describe('AppTitle service', () => {
it('sets default application name', () => {
it('should set default application name', () => {
const { appTitleService, titleService } = new TestConfig({
applicationName: undefined
});
appTitleService.setTitle();
expect(titleService.setTitle)
.toHaveBeenCalledWith('Alfresco ADF Application');
expect(titleService.setTitle).toHaveBeenCalledWith('Alfresco ADF Application');
});
it('sets only the application name', () => {
it('should set only the application name', () => {
const { appTitleService, titleService } = new TestConfig({
applicationName: 'My application'
});
appTitleService.setTitle();
expect(titleService.setTitle)
.toHaveBeenCalledWith('My application');
expect(titleService.setTitle).toHaveBeenCalledWith('My application');
});
it('appends application name to the title', () => {
it('should append application name to the title', () => {
const { appTitleService, titleService } = new TestConfig({
applicationName: 'My application'
});
appTitleService.setTitle('My page');
expect(titleService.setTitle)
.toHaveBeenCalledWith('My page - My application');
expect(titleService.setTitle).toHaveBeenCalledWith('My page - My application');
});
it('should update title on language change', () => {
const { appTitleService, titleService, translationService } = new TestConfig({
applicationName: 'My application'
});
spyOn(translationService, 'instant').and.returnValues('hello', 'привет');
appTitleService.setTitle('key');
expect(titleService.setTitle).toHaveBeenCalledWith('hello - My application');
(<any> titleService).setTitle.calls.reset();
translationService.translate.onLangChange.next(<any> {});
expect(titleService.setTitle).toHaveBeenCalledWith('привет - My application');
});
});

View File

@@ -18,22 +18,41 @@
import { Injectable } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { AppConfigService } from '../app-config/app-config.service';
import { TranslationService } from './translation.service';
@Injectable()
export class PageTitleService {
private originalTitle: string = '';
private translatedTitle: string = '';
constructor(
private titleService: Title,
private appConfig: AppConfigService) {}
private appConfig: AppConfigService,
private translationService: TranslationService) {
translationService.translate.onLangChange.subscribe(() => this.onLanguageChanged());
}
/**
* Sets the page title.
* @param value The new title
*/
setTitle(value: string = '') {
const name = this.appConfig.get('application.name') || 'Alfresco ADF Application';
const title = value ? `${value} - ${name}` : `${name}`;
this.originalTitle = value;
this.translatedTitle = this.translationService.instant(value);
this.updateTitle();
}
private onLanguageChanged() {
this.translatedTitle = this.translationService.instant(this.originalTitle);
this.updateTitle();
}
private updateTitle() {
const name = this.appConfig.get('application.name') || 'Alfresco ADF Application';
const title = this.translatedTitle ? `${this.translatedTitle} - ${name}` : `${name}`;
this.titleService.setTitle(title);
}
}