diff --git a/ng2-translate.ts b/ng2-translate.ts index e0901593..a42e7a81 100644 --- a/ng2-translate.ts +++ b/ng2-translate.ts @@ -4,6 +4,7 @@ import {TranslateService} from './src/translate.service'; export * from './src/translate.pipe'; export * from './src/translate.service'; export * from './src/translate.parser'; +export * from './src/missingtranslationhandler.interface'; export default { pipes: [TranslatePipe], diff --git a/package.json b/package.json index c3b97eec..e44eb6f1 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "es6-shim": "~0.33.3", "reflect-metadata": "0.1.2", "rxjs": "5.0.0-beta.0", - "zone.js": "~0.5.10" + "zone.js": "0.5.11" }, "czConfig": { "path": "node_modules/cz-conventional-changelog" diff --git a/src/missingtranslationhandler.interface.ts b/src/missingtranslationhandler.interface.ts new file mode 100644 index 00000000..be35fd19 --- /dev/null +++ b/src/missingtranslationhandler.interface.ts @@ -0,0 +1,3 @@ +export interface MissingTranslationHandler { + handle(key: string): void; +} diff --git a/src/translate.service.ts b/src/translate.service.ts index c6055d40..6f7841e3 100644 --- a/src/translate.service.ts +++ b/src/translate.service.ts @@ -6,6 +6,7 @@ import 'rxjs/add/operator/share'; import 'rxjs/add/operator/map'; import {Parser} from './translate.parser'; +import {MissingTranslationHandler} from './missingtranslationhandler.interface'; interface TranslateLoader { getTranslation(lang: string): any; @@ -68,6 +69,11 @@ export class TranslateService { private defaultLang: string; private langs: Array; private parser: Parser = new Parser(); + + /** + * Handler for missing translations + */ + private missingTranslationHandler: MissingTranslationHandler; constructor(private http: Http) { this.useStaticFilesLoader(); @@ -183,6 +189,10 @@ export class TranslateService { let translations: any = this.parser.flattenObject(this.translations[this.defaultLang]); res = this.parser.interpolate(translations[key], interpolateParams); } + + if (!res && this.missingTranslationHandler) { + this.missingTranslationHandler.handle(key); + } return res || key; }; @@ -218,5 +228,9 @@ export class TranslateService { this.currentLang = lang; this.onLangChange.emit({lang: lang, translations: this.translations[lang]}); } + + public setMissingTranslationHandler(handler: MissingTranslationHandler) { + this.missingTranslationHandler = handler; + } } diff --git a/tests/translate.service.spec.ts b/tests/translate.service.spec.ts index 8ed63244..80346d8d 100644 --- a/tests/translate.service.spec.ts +++ b/tests/translate.service.spec.ts @@ -6,6 +6,7 @@ import { } from "angular2/http"; import {MockBackend, MockConnection} from "angular2/http/testing"; import {TranslateService} from '../src/translate.service'; +import {MissingTranslationHandler} from '../src/missingtranslationhandler.interface'; export function main() { @@ -151,5 +152,35 @@ export function main() { done(); }); }); + + function prepareMissingTranslationHandler() { + class Missing implements MissingTranslationHandler { + handle(key: string) {} + } + let handler = new Missing(); + spyOn(handler, 'handle'); + + translate.setMissingTranslationHandler(handler); + + return handler; + } + + it('should use the MissingTranslationHandler when the key does not exist', () => { + prepareStaticTranslate(); + let handler = prepareMissingTranslationHandler(); + + translate.get('nonExistingKey').subscribe(() => { + expect(handler.handle).toHaveBeenCalledWith('nonExistingKey'); + }); + }); + + it('should not call the MissingTranslationHandler when the key exists', () => { + let handler = prepareMissingTranslationHandler(); + prepareStaticTranslate(); + + translate.get('TEST').subscribe(() => { + expect(handler.handle).not.toHaveBeenCalled(); + }); + }); }); } \ No newline at end of file