[ADF-1636] fix issues with i18n resource composing (#2434)

* fix issues with i18n resource composing

merge resources instead of concatenating, make 'app' resource always load last

refs #2412

* unit tests
This commit is contained in:
Denys Vuika 2017-10-05 17:13:37 +01:00 committed by Eugenio Romano
parent 6d3c3316d9
commit 1ac8f9634f
3 changed files with 119 additions and 11 deletions

View File

@ -20,6 +20,7 @@ import { Http, Response } from '@angular/http';
import { TranslateLoader } from '@ngx-translate/core';
import { Observable } from 'rxjs/Rx';
import { ComponentTranslationModel } from '../models/component.model';
import { ObjectUtils } from '../utils/object-utils';
import { LogService } from './log.service';
@Injectable()
@ -78,17 +79,27 @@ export class AlfrescoTranslateLoader implements TranslateLoader {
return (this.queue[lang] || []).find(x => x === name) ? true : false;
}
getFullTranslationJSON(lang: string) {
let fullTranslation: string = '';
let cloneList = this.providers.slice(0);
cloneList.reverse().forEach((component) => {
if (component.json && component.json[lang]) {
fullTranslation += JSON.stringify(component.json[lang]);
}
});
if (fullTranslation !== '') {
return JSON.parse(fullTranslation.replace(/}{/g, ','));
}
getFullTranslationJSON(lang: string): any {
let result = {};
this.providers
.slice(0)
.sort((a, b) => {
if (a.name === 'app') {
return 1;
}
if (b.name === 'app') {
return -1;
}
return a.name.localeCompare(b.name);
})
.forEach(model => {
if (model.json && model.json[lang]) {
result = ObjectUtils.merge(result, model.json[lang]);
}
});
return result;
}
getTranslation(lang: string): Observable<any> {

View File

@ -51,4 +51,83 @@ describe('ObjectUtils', () => {
expect(ObjectUtils.getValue(null, 'id')).toBeUndefined();
});
describe('merge', () => {
it('should merge top level properties', () => {
const obj1 = {
prop1: 'value1'
};
const obj2 = {
prop2: 'value2'
};
const result = ObjectUtils.merge(obj1, obj2);
expect(result).toEqual({
prop1: 'value1',
prop2: 'value2'
});
});
it('should merge object properties', () => {
const obj1 = {
child: {
prop1: 1
},
prop: 1
};
const obj2 = {
child: {
prop1: 2,
prop2: 3
}
};
const result = ObjectUtils.merge(obj1, obj2);
expect(result).toEqual({
child: {
prop1: 2,
prop2: 3
},
prop: 1
});
});
it('should merge arrays', () => {
const obj1 = {
arr: [1, 2, 3]
};
const obj2 = {
arr: [4, 5, 6]
};
const result = ObjectUtils.merge(obj1, obj2);
expect(result).toEqual({
arr: [1, 2, 3, 4, 5, 6]
});
});
it('shoud overwrite only single property in the object', () => {
const obj1 = {
child: {
prop1: 1,
prop2: 2,
prop3: 3
}
};
const obj2 = {
child: {
prop3: 0
}
};
const result = ObjectUtils.merge(obj1, obj2);
expect(result).toEqual({
child: {
prop1: 1,
prop2: 2,
prop3: 0
}
});
});
});
});

View File

@ -47,4 +47,22 @@ export class ObjectUtils {
return target;
}
static merge(...objects): any {
let result = {};
objects.forEach(source => {
Object.keys(source).forEach(prop => {
if (prop in result && Array.isArray(result[prop])) {
result[prop] = result[prop].concat(source[prop]);
} else if (prop in result && typeof result[prop] === 'object') {
result[prop] = ObjectUtils.merge(result[prop], source[prop]);
} else {
result[prop] = source[prop];
}
});
});
return result;
}
}