Skip to content

Commit c2ab5cf

Browse files
authored
Fix: labels looked up from serviceEntity (#33)
1 parent 82fbcb1 commit c2ab5cf

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

lib/localization.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const cds = require("@sap/cds/lib");
22
const LOG = cds.log("change-log");
33
const { getNameFromPathVal, getDBEntity } = require("./entity-helper");
4-
const OBJECT_TYPE_I18N_LABEL_KEY = "@Common.Label"
5-
const OBJECT_TYPE_I18N_TITLE_KEY = "@title"
64

75
const MODIF_I18N_MAP = {
86
create: "{i18n>ChangeLog.modification.create}",
@@ -43,7 +41,7 @@ const _localizeDefaultObjectID = function (change, locale) {
4341
const parentEntityName = getNameFromPathVal(parentNodePathVal);
4442
const dbEntity = getDBEntity(parentEntityName);
4543
try {
46-
const labelI18nKey = dbEntity[OBJECT_TYPE_I18N_LABEL_KEY];
44+
const labelI18nKey = dbEntity['@Common.Label'] || dbEntity['@title'];
4745
const labelI18nValue = labelI18nKey ? _getLocalization(locale, labelI18nKey) : null;
4846
change.parentObjectID = labelI18nValue ? labelI18nValue : dbEntity.name;
4947
} catch (e) {
@@ -56,7 +54,7 @@ const _localizeDefaultObjectID = function (change, locale) {
5654
const _localizeEntityType = function (change, locale) {
5755
if (change.entity) {
5856
try {
59-
const labelI18nKey = _getLabelI18nKeyOnEntity(change.entity);
57+
const labelI18nKey = _getLabelI18nKeyOnEntity(change.serviceEntity);
6058
const labelI18nValue = labelI18nKey ? _getLocalization(locale, labelI18nKey) : null;
6159

6260
change.entity = labelI18nValue ? labelI18nValue : change.entity;
@@ -83,12 +81,11 @@ const _localizeAttribute = function (change, locale) {
8381
try {
8482
const serviceEntity = cds.model.definitions[change.serviceEntity];
8583
let labelI18nKey = _getLabelI18nKeyOnEntity(change.serviceEntity, change.attribute);
86-
const element = serviceEntity.elements[change.attribute];
87-
if (element.isAssociation && !labelI18nKey) {
88-
labelI18nKey = _getLabelI18nKeyOnEntity(element.target);
84+
if (!labelI18nKey) {
85+
const element = serviceEntity.elements[change.attribute];
86+
if (element.isAssociation) labelI18nKey = _getLabelI18nKeyOnEntity(element.target);
8987
}
9088
const labelI18nValue = labelI18nKey ? _getLocalization(locale, labelI18nKey) : null;
91-
9289
change.attribute = labelI18nValue ? labelI18nValue : change.attribute;
9390
} catch (e) {
9491
LOG.error("Failed to localize change attribute", e);
@@ -98,14 +95,10 @@ const _localizeAttribute = function (change, locale) {
9895
};
9996

10097
const _getLabelI18nKeyOnEntity = function (entityName, /** optinal */ attribute) {
101-
const entity = cds.model.definitions[entityName];
102-
if (!entity) return "";
103-
if (attribute) {
104-
const element = entity.elements[attribute] ? entity.elements[attribute] : {};
105-
return element[OBJECT_TYPE_I18N_LABEL_KEY];
106-
}
107-
const entityLabel = entity[OBJECT_TYPE_I18N_LABEL_KEY] ? entity[OBJECT_TYPE_I18N_LABEL_KEY] : entity[OBJECT_TYPE_I18N_TITLE_KEY];
108-
return entityLabel;
98+
let def = cds.model.definitions[entityName];
99+
if (attribute) def = def?.elements[attribute]
100+
if (!def) return "";
101+
return def['@Common.Label'] || def['@title'];
109102
};
110103

111104
const localizeLogFields = function (data, locale) {

tests/integration/fiori-draft-enabled.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ describe("change log integration test", () => {
886886
);
887887
await utils.apiAction("admin", "BookStores", "64625905-c234-4d0d-9bc1-283ee8946770", "AdminService", action);
888888

889-
const selectedColumns = ["attribute", "modification", "entity", "objectID"];
889+
const selectedColumns = ["attribute", "modification", "entity", "objectID", "parentObjectID"];
890890
const bookElementChanges = [];
891891
for (const selectedColumn of selectedColumns) {
892892
const bookChanges = await adminService.run(
@@ -910,7 +910,7 @@ describe("change log integration test", () => {
910910

911911
// To do localization, entity only needs parameters entity itself, so the localization could be done
912912
const bookChangeEntity = bookElementChanges[2];
913-
expect(bookChangeEntity.entity).to.equal("Book Store");
913+
expect(bookChangeEntity.entity).to.equal("sap.capire.bookshop.BookStores");
914914

915915
// To do localization, object id needs parameters entity (if no object id is annotated), so the localization could not be done
916916
// If no object id is annotated, the real value stored in db of object id should be "".

0 commit comments

Comments
 (0)