From 8200127823c319acd7225bd2f375c5845ede07f1 Mon Sep 17 00:00:00 2001 From: Ulmann Christian Date: Thu, 17 Mar 2016 08:42:19 +0100 Subject: [PATCH] fix(TranslateParser): allow keys with dots along with subkeys Fixes #56 --- bundles/ng2-translate.js | 2 +- src/translate.parser.ts | 2 +- tests/translate.parser.spec.ts | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/bundles/ng2-translate.js b/bundles/ng2-translate.js index 45dca6f9..8c954e29 100644 --- a/bundles/ng2-translate.js +++ b/bundles/ng2-translate.js @@ -385,7 +385,7 @@ System.registerDynamic("src/translate.parser", [], true, function($__require, ex key = ''; do { key += keys.shift(); - if (target[key]) { + if (target[key] && (typeof target[key] === 'object' || !keys.length)) { target = target[key]; key = ''; } else if (!keys.length) { diff --git a/src/translate.parser.ts b/src/translate.parser.ts index 08ba1da1..21612db8 100644 --- a/src/translate.parser.ts +++ b/src/translate.parser.ts @@ -32,7 +32,7 @@ export class Parser { key = ''; do { key += keys.shift(); - if (target[key]) { + if (target[key] && (typeof target[key] === 'object' || !keys.length)) { target = target[key]; key = ''; } else if (!keys.length) { diff --git a/tests/translate.parser.spec.ts b/tests/translate.parser.spec.ts index b848f362..3fc35986 100644 --- a/tests/translate.parser.spec.ts +++ b/tests/translate.parser.spec.ts @@ -33,6 +33,10 @@ export function main() { expect(parser.getValue({key1: {'key2.key3': "value3"}}, 'key1.key2.key3')).toEqual("value3"); expect(parser.getValue({'key1.key2.key3': "value3"}, 'key1.key2.key3')).toEqual("value3"); expect(parser.getValue({'key1.key2': {key3: "value3"}}, 'key1.key2.keyWrong')).not.toBeDefined(); + expect(parser.getValue({ + 'key1': "value1", + 'key1.key2' : "value2" + }, 'key1.key2')).toEqual("value2"); }); });