@cap-js/change-tracking 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -1
- package/README.md +33 -1
- package/_i18n/i18n_ar.properties +55 -0
- package/_i18n/i18n_bg.properties +55 -0
- package/_i18n/i18n_cs.properties +55 -0
- package/_i18n/i18n_da.properties +55 -0
- package/_i18n/i18n_de.properties +55 -55
- package/_i18n/i18n_el.properties +55 -0
- package/_i18n/i18n_en.properties +1 -1
- package/_i18n/i18n_en_US_saptrc.properties +55 -0
- package/_i18n/i18n_es.properties +55 -51
- package/_i18n/i18n_es_MX.properties +55 -0
- package/_i18n/i18n_fi.properties +55 -0
- package/_i18n/i18n_fr.properties +55 -51
- package/_i18n/i18n_he.properties +55 -0
- package/_i18n/i18n_hr.properties +55 -0
- package/_i18n/i18n_hu.properties +55 -0
- package/_i18n/i18n_it.properties +55 -51
- package/_i18n/i18n_ja.properties +11 -3
- package/_i18n/i18n_kk.properties +55 -0
- package/_i18n/i18n_ko.properties +55 -0
- package/_i18n/i18n_ms.properties +55 -0
- package/_i18n/i18n_nl.properties +55 -0
- package/_i18n/i18n_no.properties +55 -0
- package/_i18n/i18n_pl.properties +55 -51
- package/_i18n/i18n_pt.properties +55 -51
- package/_i18n/i18n_ro.properties +55 -0
- package/_i18n/i18n_ru.properties +55 -51
- package/_i18n/i18n_sh.properties +55 -0
- package/_i18n/i18n_sk.properties +55 -0
- package/_i18n/i18n_sl.properties +55 -0
- package/_i18n/i18n_sv.properties +55 -0
- package/_i18n/i18n_th.properties +55 -0
- package/_i18n/i18n_tr.properties +55 -0
- package/_i18n/i18n_uk.properties +55 -0
- package/_i18n/i18n_vi.properties +55 -0
- package/_i18n/i18n_zh_CN.properties +10 -6
- package/_i18n/i18n_zh_TW.properties +55 -0
- package/index.cds +2 -1
- package/lib/change-log.js +6 -3
- package/lib/format-options.js +66 -0
- package/lib/localization.js +53 -41
- package/package.json +4 -3
package/lib/localization.js
CHANGED
|
@@ -1,37 +1,21 @@
|
|
|
1
1
|
const cds = require("@sap/cds/lib");
|
|
2
2
|
const LOG = cds.log("change-log");
|
|
3
|
+
const { formatOptions } = require("./format-options");
|
|
3
4
|
const { getNameFromPathVal, getDBEntity, splitPath } = require("./entity-helper");
|
|
4
5
|
|
|
5
6
|
const MODIF_I18N_MAP = {
|
|
6
|
-
create: "
|
|
7
|
-
update: "
|
|
8
|
-
delete: "
|
|
7
|
+
create: "ChangeLog.modification.create",
|
|
8
|
+
update: "ChangeLog.modification.update",
|
|
9
|
+
delete: "ChangeLog.modification.delete",
|
|
9
10
|
};
|
|
10
11
|
|
|
11
|
-
const
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
// REVISIT!
|
|
17
|
-
// REVISIT!
|
|
18
|
-
// REVISIT!
|
|
19
|
-
// REVISIT!
|
|
20
|
-
// REVISIT!
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
return JSON.parse(cds.localize(cds.model, locale, JSON.stringify(i18nKey)));
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const _localizeModification = function (change, locale) {
|
|
12
|
+
const _localizeModification = function (change) {
|
|
29
13
|
if (change.modification && MODIF_I18N_MAP[change.modification]) {
|
|
30
|
-
change.modification =
|
|
14
|
+
change.modification = cds.i18n.labels.for(MODIF_I18N_MAP[change.modification]);
|
|
31
15
|
}
|
|
32
16
|
};
|
|
33
17
|
|
|
34
|
-
const _localizeDefaultObjectID = function (change
|
|
18
|
+
const _localizeDefaultObjectID = function (change) {
|
|
35
19
|
if (!change.objectID) {
|
|
36
20
|
change.objectID = change.entity ? change.entity : "";
|
|
37
21
|
}
|
|
@@ -41,9 +25,8 @@ const _localizeDefaultObjectID = function (change, locale) {
|
|
|
41
25
|
const parentEntityName = getNameFromPathVal(parentNodePathVal);
|
|
42
26
|
const dbEntity = getDBEntity(parentEntityName);
|
|
43
27
|
try {
|
|
44
|
-
const labelI18nKey = dbEntity['@Common.Label'] || dbEntity['@title'];
|
|
45
|
-
|
|
46
|
-
change.parentObjectID = labelI18nValue ? labelI18nValue : dbEntity.name;
|
|
28
|
+
const labelI18nKey = getTranslationKey(dbEntity['@Common.Label'] || dbEntity['@title']);
|
|
29
|
+
change.parentObjectID = cds.i18n.labels.for(labelI18nKey) || labelI18nKey || dbEntity.name;
|
|
47
30
|
} catch (e) {
|
|
48
31
|
LOG.error("Failed to localize parent object id", e);
|
|
49
32
|
throw new Error("Failed to localize parent object id", e);
|
|
@@ -51,13 +34,11 @@ const _localizeDefaultObjectID = function (change, locale) {
|
|
|
51
34
|
}
|
|
52
35
|
};
|
|
53
36
|
|
|
54
|
-
const _localizeEntityType = function (change
|
|
37
|
+
const _localizeEntityType = function (change) {
|
|
55
38
|
if (change.entity) {
|
|
56
39
|
try {
|
|
57
40
|
const labelI18nKey = _getLabelI18nKeyOnEntity(change.serviceEntity);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
change.entity = labelI18nValue ? labelI18nValue : change.entity;
|
|
41
|
+
change.entity = labelI18nKey || change.entity;
|
|
61
42
|
} catch (e) {
|
|
62
43
|
LOG.error("Failed to localize entity type", e);
|
|
63
44
|
throw new Error("Failed to localize entity type", e);
|
|
@@ -66,9 +47,7 @@ const _localizeEntityType = function (change, locale) {
|
|
|
66
47
|
if (change.serviceEntity) {
|
|
67
48
|
try {
|
|
68
49
|
const labelI18nKey = _getLabelI18nKeyOnEntity(change.serviceEntity);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
change.serviceEntity = labelI18nValue ? labelI18nValue : change.serviceEntity;
|
|
50
|
+
change.serviceEntity = labelI18nKey || change.serviceEntity;
|
|
72
51
|
} catch (e) {
|
|
73
52
|
LOG.error("Failed to localize service entity", e);
|
|
74
53
|
throw new Error("Failed to localize service entity", e);
|
|
@@ -76,7 +55,13 @@ const _localizeEntityType = function (change, locale) {
|
|
|
76
55
|
}
|
|
77
56
|
};
|
|
78
57
|
|
|
79
|
-
const
|
|
58
|
+
const getTranslationKey = (value) => {
|
|
59
|
+
if (typeof value != 'string') return value;
|
|
60
|
+
const result = value.match(/(?<=\{@?(i18n>)).*(?=\})/g)
|
|
61
|
+
return result ? result[0] : value
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const _localizeAttribute = function (change) {
|
|
80
65
|
if (change.attribute && change.serviceEntity) {
|
|
81
66
|
try {
|
|
82
67
|
const serviceEntity = cds.model.definitions[change.serviceEntity];
|
|
@@ -85,8 +70,7 @@ const _localizeAttribute = function (change, locale) {
|
|
|
85
70
|
const element = serviceEntity.elements[change.attribute];
|
|
86
71
|
if (element.isAssociation) labelI18nKey = _getLabelI18nKeyOnEntity(element.target);
|
|
87
72
|
}
|
|
88
|
-
|
|
89
|
-
change.attribute = labelI18nValue ? labelI18nValue : change.attribute;
|
|
73
|
+
change.attribute = labelI18nKey || change.attribute;
|
|
90
74
|
} catch (e) {
|
|
91
75
|
LOG.error("Failed to localize change attribute", e);
|
|
92
76
|
throw new Error("Failed to localize change attribute", e);
|
|
@@ -98,16 +82,44 @@ const _getLabelI18nKeyOnEntity = function (entityName, /** optinal */ attribute)
|
|
|
98
82
|
let def = cds.model.definitions[entityName];
|
|
99
83
|
if (attribute) def = def?.elements[attribute]
|
|
100
84
|
if (!def) return "";
|
|
101
|
-
|
|
85
|
+
const i18nKey = getTranslationKey(def['@Common.Label'] || def['@title'] || def['@UI.HeaderInfo.TypeName']);
|
|
86
|
+
return cds.i18n.labels.for(i18nKey) || i18nKey;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const parseTime = (time, locale, options) => {
|
|
90
|
+
const timeParts = time.split(':');
|
|
91
|
+
const date = new Date();
|
|
92
|
+
date.setHours(parseInt(timeParts[0], 10), parseInt(timeParts[1], 10), parseInt(timeParts[2], 10));
|
|
93
|
+
return date.toLocaleTimeString(locale, options);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const _localizeValue = (change, locale) => {
|
|
97
|
+
if (change.valueDataType !== 'cds.Date' && change.valueDataType !== 'cds.DateTime' && change.valueDataType !== 'cds.Timestamp' && change.valueDataType !== 'cds.Time') {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const normalizedLocale = locale.replaceAll('_', '-');
|
|
101
|
+
const options = formatOptions[change.valueDataType]?.[normalizedLocale]
|
|
102
|
+
?? formatOptions[change.valueDataType]?.['en']
|
|
103
|
+
|
|
104
|
+
if (change.valueDataType === 'cds.Time') {
|
|
105
|
+
if (change.valueChangedFrom) change.valueChangedFrom = parseTime(change.valueChangedFrom, normalizedLocale, options);
|
|
106
|
+
if (change.valueChangedTo) change.valueChangedTo = parseTime(change.valueChangedTo, normalizedLocale, options);
|
|
107
|
+
} else {
|
|
108
|
+
const formatter = change.valueDataType === 'cds.Date' ? 'toLocaleDateString' : 'toLocaleString';
|
|
109
|
+
if (change.valueChangedFrom) change.valueChangedFrom = new Date(change.valueChangedFrom)[formatter](normalizedLocale, options);
|
|
110
|
+
if (change.valueChangedTo) change.valueChangedTo = new Date(change.valueChangedTo)[formatter](normalizedLocale, options);
|
|
111
|
+
}
|
|
112
|
+
|
|
102
113
|
};
|
|
103
114
|
|
|
104
115
|
const localizeLogFields = function (data, locale) {
|
|
105
116
|
if (!locale) return
|
|
106
117
|
for (const change of data) {
|
|
107
|
-
_localizeModification(change
|
|
108
|
-
_localizeAttribute(change
|
|
109
|
-
_localizeEntityType(change
|
|
110
|
-
_localizeDefaultObjectID(change
|
|
118
|
+
_localizeModification(change);
|
|
119
|
+
_localizeAttribute(change);
|
|
120
|
+
_localizeEntityType(change);
|
|
121
|
+
_localizeDefaultObjectID(change);
|
|
122
|
+
_localizeValue(change, locale);
|
|
111
123
|
}
|
|
112
124
|
};
|
|
113
125
|
module.exports = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cap-js/change-tracking",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "CDS plugin providing out-of-the box support for automatic capturing, storing, and viewing of the change records of modeled entities.",
|
|
5
5
|
"repository": "cap-js/change-tracking",
|
|
6
6
|
"author": "SAP SE (https://www.sap.com)",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"test": "npx jest --silent"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
|
-
"@sap/cds": ">=8"
|
|
21
|
+
"@sap/cds": ">=8.5"
|
|
22
22
|
},
|
|
23
23
|
"engines": {
|
|
24
24
|
"node": ">=20.0.0"
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"cds": {
|
|
34
34
|
"requires": {
|
|
35
35
|
"change-tracking": {
|
|
36
|
-
"model": "@cap-js/change-tracking"
|
|
36
|
+
"model": "@cap-js/change-tracking",
|
|
37
|
+
"considerLocalizedValues": false
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
}
|