Skip to content

Commit

Permalink
feat: added handler feature for missing translations #23
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchuech committed Feb 4, 2016
1 parent 8d31112 commit 6b15197
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions ng2-translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions src/missingtranslationhandler.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface MissingTranslationHandler {
handle(key: string): void;
}
14 changes: 14 additions & 0 deletions src/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +69,11 @@ export class TranslateService {
private defaultLang: string;
private langs: Array<string>;
private parser: Parser = new Parser();

/**
* Handler for missing translations
*/
private missingTranslationHandler: MissingTranslationHandler;

constructor(private http: Http) {
this.useStaticFilesLoader();
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
}

}
31 changes: 31 additions & 0 deletions tests/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -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();
});
});
});
}

0 comments on commit 6b15197

Please sign in to comment.