Skip to content

Commit 772cbd0

Browse files
committed
#1527 Fixed interpolation function in TranslateDefaultParser
1 parent 82433d7 commit 772cbd0

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

projects/ngx-translate/src/lib/translate.parser.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ describe('Parser', () => {
3434
expect(parser.interpolate( uc , {"x":'bless'})).toBe('BLESS YOU!');
3535
});
3636

37+
it('should handle edge cases: value not found', () => {
38+
expect(parser.interpolate("This is an array {{ key1.key2 }}", {key1: {key3: ['A', 'B', 'C']}})).toEqual("This is an array {{ key1.key2 }}");
39+
});
40+
3741
it('should handle edge cases: array', () => {
3842
expect(parser.interpolate("This is an array {{ key1.key2 }}", {key1: {key2: ['A', 'B', 'C']}})).toEqual("This is an array A, B, C");
3943
});
4044

45+
it('should handle edge cases: null', () => {
46+
expect(parser.interpolate("This is {{ key1.key2 }}", {key1: {key2: null}})).toEqual("This is null");
47+
});
48+
4149
it('should handle edge cases: bool', () => {
4250
expect(parser.interpolate("This is a bool: {{ key1.key2 }}", {key1: {key2: true}})).toEqual("This is a bool: true");
4351
});

projects/ngx-translate/src/lib/translate.parser.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,29 @@ export class TranslateDefaultParser extends TranslateParser
4848
return expr;
4949
}
5050

51-
return expr.replace(this.templateMatcher, (_substring: string, key: string) =>
51+
return expr.replace(this.templateMatcher, (substring: string, key: string) =>
5252
{
53-
return this.getInterpolationReplacement(params, key);
53+
const replacement = this.getInterpolationReplacement(params, key);
54+
return replacement !== undefined ? replacement : substring;
5455
});
5556
}
5657

5758
/**
5859
* Returns the replacement for an interpolation parameter
5960
* @params:
6061
*/
61-
protected getInterpolationReplacement(params: InterpolationParameters, key: string): string
62+
protected getInterpolationReplacement(params: InterpolationParameters, key: string): string|undefined
6263
{
63-
return this.formatValue(getValue(params, key), key);
64+
return this.formatValue(getValue(params, key));
6465
}
6566

6667
/**
6768
* Converts a value into a useful string representation.
6869
* @param value The value to format.
69-
* @param fallback the value to return in case value is undefined
7070
* @returns A string representation of the value.
7171
*/
7272
// eslint-disable-next-line @typescript-eslint/no-explicit-any
73-
protected formatValue(value: any, fallback:string): string
73+
protected formatValue(value: any): string|undefined
7474
{
7575
if (isString(value)) {
7676
return value;
@@ -93,6 +93,6 @@ export class TranslateDefaultParser extends TranslateParser
9393
return JSON.stringify(value); // Pretty-print JSON if no meaningful toString()
9494
}
9595

96-
return fallback;
96+
return undefined;
9797
}
9898
}

projects/ngx-translate/src/lib/util.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ export function getValue(target: any, key: string): any
141141
do
142142
{
143143
key += keys.shift();
144-
if (isDefined(target) && isDefined(target[key]) && (isDict(target[key]) ||isArray(target[key]) || !keys.length))
144+
if (
145+
isDefined(target) &&
146+
(isDefined(target[key]) || target[key] === null) &&
147+
(isDict(target[key]) || isArray(target[key]) || !keys.length)
148+
)
145149
{
146150
target = target[key];
147151
key = "";

0 commit comments

Comments
 (0)