Skip to content

Commit

Permalink
[ilib-loctool-json] Fix missing comment in XLIFF translation unit
Browse files Browse the repository at this point in the history
  • Loading branch information
Natalia Kędziora committed Feb 17, 2025
1 parent 78df313 commit 76861e8
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 7 deletions.
41 changes: 34 additions & 7 deletions packages/ilib-loctool-json/JsonFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
85 changes: 85 additions & 0 deletions packages/ilib-loctool-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<trans-unit>` in XLIFF file will look as follows:
```xml
<trans-unit ... >
<source>Text to translate</source>
<note>A comment for the translator</note>
</trans-unit>
```
Note that the `<note>` 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 `<trans-unit>` in XLIFF file will look as follows:
```xml
<trans-unit id="1" resname="project.whatever.text" restype="string" datatype="json">
...
</trans-unit>
```
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
Expand Down

0 comments on commit 76861e8

Please sign in to comment.