Skip to content

Commit c843d91

Browse files
committed
fix(MissingTranslationHandler): revert of the breaking change released as a minor instead of major
1 parent 481365d commit c843d91

File tree

3 files changed

+16
-79
lines changed

3 files changed

+16
-79
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,10 @@ Just don't forget that it will be called synchronously from the `instant` method
258258
##### Example:
259259
Create a Missing Translation Handler
260260
```ts
261-
import {MissingTranslationHandler, MissingTranslationHandlerParams} from 'ng2-translate';
261+
import {MissingTranslationHandler} from 'ng2-translate';
262262
263263
export class MyMissingTranslationHandler implements MissingTranslationHandler {
264-
handle(params: MissingTranslationHandlerParams) {
264+
handle(key: string) {
265265
return 'some value';
266266
}
267267
}

src/translate.service.ts

+4-33
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,6 @@ export interface LangChangeEvent {
2020
translations: any;
2121
}
2222

23-
export interface MissingTranslationHandlerParams {
24-
/**
25-
* the key that's missing in translation files
26-
*
27-
* @type {string}
28-
*/
29-
key: string;
30-
31-
/**
32-
* an instance of the service that was unable to translate the key.
33-
*
34-
* @type {TranslateService}
35-
*/
36-
translateService: TranslateService;
37-
38-
/**
39-
* interpolation params that were passed along for translating the given key.
40-
*
41-
* @type {Object}
42-
*/
43-
interpolateParams?: Object;
44-
}
45-
4623
declare interface Window {
4724
navigator: any;
4825
}
@@ -51,15 +28,13 @@ declare var window: Window;
5128
export abstract class MissingTranslationHandler {
5229
/**
5330
* A function that handles missing translations.
54-
*
55-
* @abstract
56-
* @param {MissingTranslationHandlerParams} the context for resolving a missing translation
31+
* @param key the missing key
5732
* @returns {any} a value or an observable
5833
* If it returns a value, then this value is used.
5934
* If it return an observable, the value returned by this observable will be used (except if the method was "instant").
6035
* If it doesn't return then the key will be used as a value
6136
*/
62-
abstract handle(params: MissingTranslationHandlerParams): any;
37+
abstract handle(key: string): any;
6338
}
6439

6540
export abstract class TranslateLoader {
@@ -277,12 +252,8 @@ export class TranslateService {
277252
res = this.parser.interpolate(this.parser.getValue(this.translations[this.defaultLang], key), interpolateParams);
278253
}
279254

280-
if (!res && this.missingTranslationHandler) {
281-
let params: MissingTranslationHandlerParams = { key, translateService: this };
282-
if (typeof interpolateParams !== 'undefined') {
283-
params.interpolateParams = interpolateParams;
284-
}
285-
res = this.missingTranslationHandler.handle(params);
255+
if(!res && this.missingTranslationHandler) {
256+
res = this.missingTranslationHandler.handle(key);
286257
}
287258

288259
return res !== undefined ? res : key;

tests/translate.service.spec.ts

+10-44
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {MockBackend, MockConnection} from "@angular/http/testing";
44
import {
55
TranslateService,
66
MissingTranslationHandler,
7-
MissingTranslationHandlerParams,
87
TranslateLoader,
98
TranslateStaticLoader,
109
LangChangeEvent,
@@ -311,23 +310,23 @@ describe('MissingTranslationHandler', () => {
311310
let missingTranslationHandler: MissingTranslationHandler;
312311

313312
class Missing implements MissingTranslationHandler {
314-
handle(params: MissingTranslationHandlerParams) {
313+
handle(key: string) {
315314
return "handled";
316315
}
317316
}
318317

319318
class MissingObs implements MissingTranslationHandler {
320-
handle(params: MissingTranslationHandlerParams): Observable<any> {
321-
return Observable.of(`handled: ${params.key}`);
319+
handle(key: string): Observable<any> {
320+
return Observable.of(`handled: ${key}`);
322321
}
323322
}
324323

325324
let prepare = ((handlerClass: Function) => {
326325
TestBed.configureTestingModule({
327326
imports: [HttpModule, TranslateModule.forRoot()],
328327
providers: [
329-
{ provide: MissingTranslationHandler, useClass: handlerClass },
330-
{ provide: XHRBackend, useClass: MockBackend }
328+
{provide: MissingTranslationHandler, useClass: handlerClass},
329+
{provide: XHRBackend, useClass: MockBackend}
331330
]
332331
});
333332
injector = getTestBed();
@@ -352,40 +351,7 @@ describe('MissingTranslationHandler', () => {
352351
spyOn(missingTranslationHandler, 'handle').and.callThrough();
353352

354353
translate.get('nonExistingKey').subscribe((res: string) => {
355-
expect(missingTranslationHandler.handle).toHaveBeenCalledWith(jasmine.objectContaining({ key: 'nonExistingKey' }));
356-
//test that the instance of the last called argument is string
357-
expect(res).toEqual('handled');
358-
});
359-
360-
// mock response after the xhr request, otherwise it will be undefined
361-
mockBackendResponse(connection, '{"TEST": "This is a test"}');
362-
});
363-
364-
it('should propagate interpolation params when the key does not exist', () => {
365-
prepare(Missing);
366-
translate.use('en');
367-
spyOn(missingTranslationHandler, 'handle').and.callThrough();
368-
let interpolateParams = { some: 'params' };
369-
370-
translate.get('nonExistingKey', interpolateParams).subscribe((res: string) => {
371-
expect(missingTranslationHandler.handle).toHaveBeenCalledWith(jasmine.objectContaining({ interpolateParams: interpolateParams }));
372-
//test that the instance of the last called argument is string
373-
expect(res).toEqual('handled');
374-
});
375-
376-
// mock response after the xhr request, otherwise it will be undefined
377-
mockBackendResponse(connection, '{"TEST": "This is a test"}');
378-
});
379-
380-
it('should propagate TranslationService params when the key does not exist', () => {
381-
prepare(Missing);
382-
translate.use('en');
383-
spyOn(missingTranslationHandler, 'handle').and.callThrough();
384-
let interpolateParams = { some: 'params' };
385-
386-
translate.get('nonExistingKey', interpolateParams).subscribe((res: string) => {
387-
expect(missingTranslationHandler.handle).toHaveBeenCalledWith(jasmine.objectContaining({ translateService: translate }));
388-
//test that the instance of the last called argument is string
354+
expect(missingTranslationHandler.handle).toHaveBeenCalledWith('nonExistingKey');
389355
expect(res).toEqual('handled');
390356
});
391357

@@ -395,7 +361,7 @@ describe('MissingTranslationHandler', () => {
395361

396362
it('should return the key when using MissingTranslationHandler & the handler returns nothing', () => {
397363
class MissingUndef implements MissingTranslationHandler {
398-
handle(params: MissingTranslationHandlerParams) {
364+
handle(key: string) {
399365
}
400366
}
401367

@@ -404,7 +370,7 @@ describe('MissingTranslationHandler', () => {
404370
spyOn(missingTranslationHandler, 'handle').and.callThrough();
405371

406372
translate.get('nonExistingKey').subscribe((res: string) => {
407-
expect(missingTranslationHandler.handle).toHaveBeenCalledWith(jasmine.objectContaining({ key: 'nonExistingKey' }));
373+
expect(missingTranslationHandler.handle).toHaveBeenCalledWith('nonExistingKey');
408374
expect(res).toEqual('nonExistingKey');
409375
});
410376

@@ -431,7 +397,7 @@ describe('MissingTranslationHandler', () => {
431397
spyOn(missingTranslationHandler, 'handle').and.callThrough();
432398

433399
expect(translate.instant('nonExistingKey')).toEqual('handled');
434-
expect(missingTranslationHandler.handle).toHaveBeenCalledWith(jasmine.objectContaining({ key: 'nonExistingKey' }));
400+
expect(missingTranslationHandler.handle).toHaveBeenCalledWith('nonExistingKey');
435401
});
436402

437403
it('should wait for the MissingTranslationHandler when it returns an observable & we use get', () => {
@@ -440,7 +406,7 @@ describe('MissingTranslationHandler', () => {
440406
spyOn(missingTranslationHandler, 'handle').and.callThrough();
441407

442408
translate.get('nonExistingKey').subscribe((res: string) => {
443-
expect(missingTranslationHandler.handle).toHaveBeenCalledWith(jasmine.objectContaining({ key: 'nonExistingKey' }));
409+
expect(missingTranslationHandler.handle).toHaveBeenCalledWith('nonExistingKey');
444410
expect(res).toEqual('handled: nonExistingKey');
445411
});
446412

0 commit comments

Comments
 (0)