Skip to content

Commit

Permalink
fix(TranslateService): shouldMerge option now does merging with lodash
Browse files Browse the repository at this point in the history
shouldMerge option of the `setTranslation` method didn't handle non-valid JSON translations object such compiled translations with custom compiler.
It is now doing deep merging with lodash.
Fixes ngx-translate#532

Change-Id: Ie9d83ef65d42ec9bea93489b11f0abd4ab641c4a
  • Loading branch information
hanyu-natsu authored and hl2308 committed Jan 9, 2018
1 parent 45f1f6e commit 97cdacb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
20 changes: 3 additions & 17 deletions lib/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import merge from 'lodash-es/merge';

/* tslint:disable */
/**
* @name equals
Expand Down Expand Up @@ -61,21 +63,5 @@ export function isObject(item: any): boolean {
}

export function mergeDeep(target: any, source: any): any {
target = JSON.parse(JSON.stringify(target));
source = JSON.parse(JSON.stringify(source));
let output = Object.assign({}, target);
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach((key: any) => {
if (isObject(source[key])) {
if (!(key in target)) {
Object.assign(output, { [key]: source[key] });
} else {
output[key] = mergeDeep(target[key], source[key]);
}
} else {
Object.assign(output, { [key]: source[key] });
}
});
}
return output;
return merge(target, source);
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@angular/router": "5.0.3",
"@types/hammerjs": "2.0.35",
"@types/jasmine": "2.8.2",
"@types/lodash-es": "^4.17.0",
"@types/node": "8.0.53",
"awesome-typescript-loader": "3.4.0",
"clean-webpack-plugin": "0.1.17",
Expand All @@ -60,6 +61,7 @@
"karma-sourcemap-loader": "0.3.7",
"karma-webpack": "2.0.6",
"loader-utils": "1.1.0",
"lodash-es": "^4.17.4",
"ng-packagr": "^2.0.0-rc.2",
"reflect-metadata": "0.1.10",
"rimraf": "2.6.2",
Expand Down
14 changes: 14 additions & 0 deletions tests/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ describe('TranslateService', () => {
});
});

it("should merge non-valid JSON translations if option shouldMerge is true", () => {
translations = {};
translate.setTranslation('en', { "TEST": { "sub1": () => "value1" } }, true);
translate.setTranslation('en', { "TEST": { "sub2": () => "value2" } }, true);
translate.use('en');

translate.get('TEST.sub1').subscribe((res: string) => {
expect(res).toEqual('value1');
});
translate.get('TEST.sub2').subscribe((res: string) => {
expect(res).toEqual('value2');
});
});

it("shouldn't call the current loader if you set the translation yourself", (done: Function) => {
translations = {};
translate.setTranslation('en', {"TEST": "This is a test"});
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@
version "2.8.2"
resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-2.8.2.tgz#6ae4d8740c0da5d5a627df725b4eed71b8e36668"

"@types/lodash-es@^4.17.0":
version "4.17.0"
resolved "https://registry.yarnpkg.com/@types/lodash-es/-/lodash-es-4.17.0.tgz#ed9044d62ee36a93e0650b112701986b1c74c766"
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.92"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.92.tgz#6e3cb0b71a1e12180a47a42a744e856c3ae99a57"

"@types/[email protected]":
version "8.0.53"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.53.tgz#396b35af826fa66aad472c8cb7b8d5e277f4e6d8"
Expand Down Expand Up @@ -3011,6 +3021,10 @@ locate-path@^2.0.0:
p-locate "^2.0.0"
path-exists "^3.0.0"

lodash-es@^4.17.4:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.4.tgz#dcc1d7552e150a0640073ba9cb31d70f032950e7"

lodash._basecopy@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._basecopy/-/lodash._basecopy-3.0.1.tgz#8da0e6a876cf344c0ad8a54882111dd3c5c7ca36"
Expand Down

0 comments on commit 97cdacb

Please sign in to comment.