mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-3028] i18n support for title service (#3342)
* i18n support for title service * cleanup tests * update tests
This commit is contained in:
committed by
Eugenio Romano
parent
b00eb1433e
commit
53cf5acc86
@@ -15,9 +15,8 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Component, ViewEncapsulation } from '@angular/core';
|
import { Component, ViewEncapsulation, OnInit } from '@angular/core';
|
||||||
import { ActivatedRoute } from '@angular/router';
|
import { SettingsService, PageTitleService, StorageService } from '@alfresco/adf-core';
|
||||||
import { SettingsService, PageTitleService, StorageService, TranslationService } from '@alfresco/adf-core';
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-root',
|
selector: 'app-root',
|
||||||
@@ -25,15 +24,17 @@ import { SettingsService, PageTitleService, StorageService, TranslationService }
|
|||||||
styleUrls: ['./app.component.scss'],
|
styleUrls: ['./app.component.scss'],
|
||||||
encapsulation: ViewEncapsulation.None
|
encapsulation: ViewEncapsulation.None
|
||||||
})
|
})
|
||||||
export class AppComponent {
|
export class AppComponent implements OnInit {
|
||||||
|
|
||||||
constructor(private settingsService: SettingsService,
|
constructor(private settingsService: SettingsService,
|
||||||
private storage: StorageService,
|
private storage: StorageService,
|
||||||
translationService: TranslationService,
|
private pageTitleService: PageTitleService) {
|
||||||
pageTitleService: PageTitleService,
|
}
|
||||||
route: ActivatedRoute) {
|
|
||||||
|
ngOnInit() {
|
||||||
this.setProvider();
|
this.setProvider();
|
||||||
pageTitleService.setTitle();
|
|
||||||
|
this.pageTitleService.setTitle('title');
|
||||||
}
|
}
|
||||||
|
|
||||||
private setProvider() {
|
private setProvider() {
|
||||||
|
@@ -26,7 +26,6 @@ import { NoopAnimationsModule } from '@angular/platform-browser/animations';
|
|||||||
selector: 'adf-upload-button-test',
|
selector: 'adf-upload-button-test',
|
||||||
template: 'test componente'
|
template: 'test componente'
|
||||||
})
|
})
|
||||||
|
|
||||||
export class UploadTestComponent extends UploadBase {
|
export class UploadTestComponent extends UploadBase {
|
||||||
|
|
||||||
constructor(protected uploadService: UploadService,
|
constructor(protected uploadService: UploadService,
|
||||||
@@ -234,7 +233,7 @@ describe('UploadBase', () => {
|
|||||||
addToQueueSpy = spyOn(uploadService, 'addToQueue');
|
addToQueueSpy = spyOn(uploadService, 'addToQueue');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be a mahor version upload if majorVersion is true', () => {
|
it('should be a major version upload if majorVersion is true', () => {
|
||||||
component.majorVersion = true;
|
component.majorVersion = true;
|
||||||
component.versioning = true;
|
component.versioning = true;
|
||||||
|
|
||||||
@@ -250,7 +249,7 @@ describe('UploadBase', () => {
|
|||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not be a mahor version upload if majorVersion is false', () => {
|
it('should not be a major version upload if majorVersion is false', () => {
|
||||||
component.majorVersion = false;
|
component.majorVersion = false;
|
||||||
component.versioning = true;
|
component.versioning = true;
|
||||||
|
|
||||||
|
@@ -20,7 +20,7 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|||||||
import { ContentService, UploadService, TranslationService, setupTestBed, CoreModule } from '@alfresco/adf-core';
|
import { ContentService, UploadService, TranslationService, setupTestBed, CoreModule } from '@alfresco/adf-core';
|
||||||
import { Observable } from 'rxjs/Observable';
|
import { Observable } from 'rxjs/Observable';
|
||||||
import { UploadButtonComponent } from './upload-button.component';
|
import { UploadButtonComponent } from './upload-button.component';
|
||||||
import { TranslationMock, PermissionsEnum } from '@alfresco/adf-core';
|
import { TranslationMock } from '@alfresco/adf-core';
|
||||||
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
|
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
|
||||||
|
|
||||||
describe('UploadButtonComponent', () => {
|
describe('UploadButtonComponent', () => {
|
||||||
|
@@ -28,9 +28,9 @@ export class TranslationMock {
|
|||||||
defaultLang: string = 'en';
|
defaultLang: string = 'en';
|
||||||
userLang: string;
|
userLang: string;
|
||||||
customLoader: any;
|
customLoader: any;
|
||||||
translate: any;
|
translate = {
|
||||||
|
onLangChange: new EventEmitter<LangChangeEvent>()
|
||||||
onLangChange: EventEmitter<LangChangeEvent> = new EventEmitter<LangChangeEvent>();
|
};
|
||||||
|
|
||||||
addTranslationFolder() {
|
addTranslationFolder() {
|
||||||
|
|
||||||
|
@@ -20,6 +20,9 @@ import { Title } from '@angular/platform-browser';
|
|||||||
|
|
||||||
import { AppConfigService } from '../app-config/app-config.service';
|
import { AppConfigService } from '../app-config/app-config.service';
|
||||||
import { PageTitleService } from './page-title.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 {
|
class TestConfig {
|
||||||
private setup: any = {
|
private setup: any = {
|
||||||
@@ -28,6 +31,7 @@ class TestConfig {
|
|||||||
|
|
||||||
titleService: Title = null;
|
titleService: Title = null;
|
||||||
appTitleService: PageTitleService = null;
|
appTitleService: PageTitleService = null;
|
||||||
|
translationService: TranslationService;
|
||||||
|
|
||||||
constructor(setup: any = {}) {
|
constructor(setup: any = {}) {
|
||||||
Object.assign(this.setup, setup);
|
Object.assign(this.setup, setup);
|
||||||
@@ -53,48 +57,69 @@ class TestConfig {
|
|||||||
};
|
};
|
||||||
|
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
|
imports: [
|
||||||
|
CoreTestingModule
|
||||||
|
],
|
||||||
providers: [
|
providers: [
|
||||||
titleServiceProvider,
|
titleServiceProvider,
|
||||||
appConfigProvider,
|
appConfigProvider,
|
||||||
PageTitleService
|
PageTitleService,
|
||||||
|
{
|
||||||
|
provide: TranslationService,
|
||||||
|
useClass: TranslationMock
|
||||||
|
}
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
inject([ Title, PageTitleService ], (titleService, appTitleService) => {
|
inject([ Title, PageTitleService, TranslationService ], (titleService, appTitleService, translationService) => {
|
||||||
this.titleService = titleService;
|
this.titleService = titleService;
|
||||||
this.appTitleService = appTitleService;
|
this.appTitleService = appTitleService;
|
||||||
|
this.translationService = translationService;
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('AppTitle service', () => {
|
describe('AppTitle service', () => {
|
||||||
it('sets default application name', () => {
|
it('should set default application name', () => {
|
||||||
const { appTitleService, titleService } = new TestConfig({
|
const { appTitleService, titleService } = new TestConfig({
|
||||||
applicationName: undefined
|
applicationName: undefined
|
||||||
});
|
});
|
||||||
|
|
||||||
appTitleService.setTitle();
|
appTitleService.setTitle();
|
||||||
expect(titleService.setTitle)
|
expect(titleService.setTitle).toHaveBeenCalledWith('Alfresco ADF Application');
|
||||||
.toHaveBeenCalledWith('Alfresco ADF Application');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sets only the application name', () => {
|
it('should set only the application name', () => {
|
||||||
const { appTitleService, titleService } = new TestConfig({
|
const { appTitleService, titleService } = new TestConfig({
|
||||||
applicationName: 'My application'
|
applicationName: 'My application'
|
||||||
});
|
});
|
||||||
|
|
||||||
appTitleService.setTitle();
|
appTitleService.setTitle();
|
||||||
expect(titleService.setTitle)
|
expect(titleService.setTitle).toHaveBeenCalledWith('My application');
|
||||||
.toHaveBeenCalledWith('My application');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('appends application name to the title', () => {
|
it('should append application name to the title', () => {
|
||||||
const { appTitleService, titleService } = new TestConfig({
|
const { appTitleService, titleService } = new TestConfig({
|
||||||
applicationName: 'My application'
|
applicationName: 'My application'
|
||||||
});
|
});
|
||||||
|
|
||||||
appTitleService.setTitle('My page');
|
appTitleService.setTitle('My page');
|
||||||
expect(titleService.setTitle)
|
expect(titleService.setTitle).toHaveBeenCalledWith('My page - My application');
|
||||||
.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');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -18,22 +18,41 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { Title } from '@angular/platform-browser';
|
import { Title } from '@angular/platform-browser';
|
||||||
import { AppConfigService } from '../app-config/app-config.service';
|
import { AppConfigService } from '../app-config/app-config.service';
|
||||||
|
import { TranslationService } from './translation.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PageTitleService {
|
export class PageTitleService {
|
||||||
|
|
||||||
|
private originalTitle: string = '';
|
||||||
|
private translatedTitle: string = '';
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private titleService: Title,
|
private titleService: Title,
|
||||||
private appConfig: AppConfigService) {}
|
private appConfig: AppConfigService,
|
||||||
|
private translationService: TranslationService) {
|
||||||
|
translationService.translate.onLangChange.subscribe(() => this.onLanguageChanged());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the page title.
|
* Sets the page title.
|
||||||
* @param value The new title
|
* @param value The new title
|
||||||
*/
|
*/
|
||||||
setTitle(value: string = '') {
|
setTitle(value: string = '') {
|
||||||
const name = this.appConfig.get('application.name') || 'Alfresco ADF Application';
|
this.originalTitle = value;
|
||||||
const title = value ? `${value} - ${name}` : `${name}`;
|
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);
|
this.titleService.setTitle(title);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user