diff --git a/packages/ilib-loctool-json/JsonFile.js b/packages/ilib-loctool-json/JsonFile.js index ffa60660d..d28ac65a5 100644 --- a/packages/ilib-loctool-json/JsonFile.js +++ b/packages/ilib-loctool-json/JsonFile.js @@ -343,12 +343,35 @@ JsonFile.prototype.parseObj = function(json, root, schema, ref, name, localizabl if (!schema) return this.sparseValue(json); localizable |= schema.localizable; + const isComment = schema.isComment ?? false; + var type = schema.type || typeof(json); switch (type) { case "boolean": case "number": case "integer": case "string": + if(isComment) { + this.comment = json; + + /* + Find the resource related to the currently processed JSON value and + update its comment property with the associated comment. + + This is necessary because JSON values are processed in the order + they are defined in the JSON file, meaning we need to account for + different value orderings. For example, in one case, we may get + "description" first, which has "isComment" set to true, and therefore, it is a comment source. + In another case, we may get "defaultMessage" first, which is instead used as a + source string for translation. + */ + const resources = this.set.getBy({ reskey: ref }) + if(resources && resources.length) { + const resource = resources[0]; + resource.comment = this.comment; + } + } + if (localizable) { if (isPrimitive(typeof(json))) { var text = String(json); @@ -414,7 +437,7 @@ JsonFile.prototype.parseObj = function(json, root, schema, ref, name, localizabl var opts = { resType: "string", project: this.project.getProjectId(), - key: key, + key: this.key ?? key, sourceLocale: this.project.sourceLocale, pathName: this.pathName, state: "new", @@ -447,11 +470,15 @@ JsonFile.prototype.parseObj = function(json, root, schema, ref, name, localizabl break; case "object": - if (typeof(json) !== "object") { - this.logger.warn(this.pathName + '/' + ref + " is a " + - typeof(json) + " but should be an object according to the schema... skipping."); - return; - } + if(schema.usePropertyKeyAsResname) { + this.key = JsonFile.unescapeRef(ref).substring(2); + } + + if(typeof(json) !== "object") { + this.logger.warn(this.pathName + '/' + ref + " is a " + + typeof(json) + " but should be an object according to the schema... skipping."); + return; + } if (isPlural(json)) { // handle this as a single plural resource instance instead // of an object that has resources inside of it @@ -553,7 +580,7 @@ JsonFile.prototype.parseObj = function(json, root, schema, ref, name, localizabl json[prop], root, schema.properties[prop], - ref + '/' + JsonFile.escapeRef(prop), + this.key ?? ref + '/' + JsonFile.escapeRef(prop), prop, localizable, translations, diff --git a/packages/ilib-loctool-json/README.md b/packages/ilib-loctool-json/README.md index 10e6c8083..1f7bfbe86 100644 --- a/packages/ilib-loctool-json/README.md +++ b/packages/ilib-loctool-json/README.md @@ -299,6 +299,91 @@ For strings that have an `enum` keyword, each of the values in the `enum` will not be translated as well, as the code that reads this json file is explicitly expecting one of the given fixed values. +### The `isComment` Keyword +The `isComment` keyword specifies that the property value should be treated as a comment for the translator. + +#### Example + +Assume we have the following `schema.json`: +```json +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "sample-schema", + "title": "Sample schema with isComment", + "type": "object", + "additionalProperties": { + "type": "object", + "properties": { + "defaultMessage": { + "type": "string", + "localizable": true + }, + "description": { + "type": "string", + "isComment": true + } + } + } +} +``` +And a JSON file with the following content: +```json +{ + "project.whatever.text": { + "defaultMessage": "Text to translate", + "description": "A comment for the translator" + } +} +``` +The corresponding `` in XLIFF file will look as follows: +```xml + + Text to translate + A comment for the translator + +``` +Note that the `` tag content is set to the `description` property value from the JSON file (`"A comment for the translator"`), as the `isComment` keyword for the property `description` in the associated JSON schema is set to `true`. + + +### The `usePropertyKeyAsResname` Keyword +The `usePropertyKeyAsResname` keyword specifies that the property key from the JSON file should be used as the resource name for localization. + +#### Example + +Assume we have the following `schema.json`: +```json +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "sample-schema", + "title": "Sample schema with usePropertyKeyAsResname", + "type": "object", + "additionalProperties": { + "type": "object", + "properties": { + ... + }, + "usePropertyKeyAsResname": true, + } +} +``` +And a JSON file with the following content: +```json +{ + "project.whatever.text": { + "defaultMessage": "Text to translate", + "description": "A comment for the translator" + } +} +``` +The corresponding `` in XLIFF file will look as follows: +```xml + + ... + +``` +Mind, that the `resname` attribute is set to the property key from the JSON file (`"project.whatever.text"`). + + ## JSON File Generation When you create a new, empty JsonFile instance that is not backed