Skip to content

Commit a016030

Browse files
committed
fix: handle parser error when couldn't parse
1 parent fde2d81 commit a016030

File tree

8 files changed

+44
-10
lines changed

8 files changed

+44
-10
lines changed

cypress/integration/home.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export function generateContent(lang = 'english') {
2929

3030
// Missing key
3131
cy.get(`[data-cy=missing-key]`).should('contain', 'alertty');
32+
33+
// Loop
34+
cy.get(`[data-cy=translation-loop]`).should('contain', `b ${lang}`);
35+
cy.get(`[data-cy=translation-loop]`).should('contain', `c ${lang}`);
3236
}
3337

3438
describe('Transloco', () => {

projects/transloco/src/lib/tests/transloco.parser.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DefaultParser } from '../../public-api';
2+
import createSpy = jasmine.createSpy;
23

34
describe('TranslocoParser', () => {
45
const parser = new DefaultParser();
@@ -34,4 +35,15 @@ describe('TranslocoParser', () => {
3435
expect(parser.parse(null)).toEqual(null);
3536
expect(parser.parse(undefined)).toEqual(undefined);
3637
});
38+
39+
it('shoudl call error handler when value can not be parsed', () => {
40+
const errorCB = createSpy();
41+
parser.parse(null, {}, {}, errorCB);
42+
parser.parse(undefined, {}, {}, errorCB);
43+
parser.parse(<any>false, {}, {}, errorCB);
44+
parser.parse(<any>{}, {}, {}, errorCB);
45+
parser.parse(<any>[], {}, {}, errorCB);
46+
47+
expect(errorCB).toHaveBeenCalledTimes(5);
48+
});
3749
});

projects/transloco/src/lib/transloco.parser.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import { getValue } from './helpers';
55
export const TRANSLOCO_PARSER = new InjectionToken('TRANSLOCO_PARSER');
66

77
export abstract class TranslocoParser {
8-
abstract parse(value: string, params: HashMap, lang: HashMap): string;
8+
abstract parse(value: string, params: HashMap, lang: HashMap, onError?: Function): string;
99
}
1010

1111
export class DefaultParser extends TranslocoParser {
12-
parse(value: string, params: HashMap = {}, lang?: HashMap) {
12+
parse(value: string, params: HashMap = {}, lang?: HashMap, onError?: Function): string {
13+
if (typeof value !== 'string') {
14+
return onError ? onError() && value : value;
15+
}
16+
1317
return value
1418
? value.replace(/{{(.*?)}}/g, function(_, match) {
1519
match = match.trim();

projects/transloco/src/lib/transloco.service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ export class TranslocoService {
115115
if (!value) {
116116
return this.missingHandler.handle(key, params, this.config);
117117
}
118-
return this.parser.parse(value, params, lang);
118+
119+
return this.parser.parse(value, params, lang, this.parseErrorHandler.bind(this, key));
119120
}
120121

121122
/**
@@ -160,4 +161,8 @@ export class TranslocoService {
160161
getTranslation(lang: string) {
161162
return this.langs.get(lang);
162163
}
164+
165+
private parseErrorHandler(key) {
166+
console.warn(`could not parse key: ${key}`);
167+
}
163168
}

src/app/home/home.component.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@ <h3 class="mtb">Missing key</h3>
4040
<ul class="list-group">
4141
<li class="list-group-item" data-cy="missing-key"><b>Open the console </b>{{ 'alertty' | transloco }}</li>
4242
</ul>
43+
44+
<h3 class="mtb">Translation loop</h3>
45+
<ul class="list-group">
46+
<li *ngFor="let key of translateList" class="list-group-item" data-cy="translation-loop">{{ key | transloco }}</li>
47+
</ul>

src/app/home/home.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ export class HomeComponent implements OnInit {
99
dynamic = '🦄';
1010
key = 'home';
1111

12-
constructor() {
13-
}
12+
translateList = ['b', 'c'];
1413

15-
ngOnInit() {
16-
}
14+
constructor() {}
15+
16+
ngOnInit() {}
1717

1818
changeKey() {
1919
this.key = this.key === 'home' ? 'fromList' : 'home';
2020
}
2121

2222
changeParam() {
23-
this.dynamic = this.dynamic === '🦄' ? '🦄🦄🦄' :'🦄';
23+
this.dynamic = this.dynamic === '🦄' ? '🦄🦄🦄' : '🦄';
2424
}
2525
}

src/assets/i18n/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
"b": {
77
"c": "a.b.c {{fromList}} english"
88
}
9-
}
9+
},
10+
"b": "b english",
11+
"c": "c english"
1012
}

src/assets/i18n/es.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
"b": {
77
"c": "a.b.c {{fromList}} spanish"
88
}
9-
}
9+
},
10+
"b": "b spanish",
11+
"c": "c spanish"
1012
}

0 commit comments

Comments
 (0)