diff --git a/packages/ilib-loctool-json/JsonFile.js b/packages/ilib-loctool-json/JsonFile.js index d4d0819ed..e8d205530 100644 --- a/packages/ilib-loctool-json/JsonFile.js +++ b/packages/ilib-loctool-json/JsonFile.js @@ -429,7 +429,7 @@ JsonFile.prototype.extractFromPrimitive = function (localizable, json, ref, tran returnValue = __ret.returnValue; break case "comment": - this.handleComment(json, ref); + this.handleComment(json, this.key ?? ref); returnValue = this.sparseValue(json); break; default: diff --git a/packages/ilib-loctool-json/test/JsonFile.test.js b/packages/ilib-loctool-json/test/JsonFile.test.js index f251354b6..cb1fafc18 100644 --- a/packages/ilib-loctool-json/test/JsonFile.test.js +++ b/packages/ilib-loctool-json/test/JsonFile.test.js @@ -2875,25 +2875,82 @@ describe("jsonfile", function () { }); }); -// -// it.each([ -// { localizable: false, schema: { localizable: "source" } }, -// { localizable: false, schema: { localizable: "source" } }, -// -// { localizable: false, schema: { localizable: true } }, -// { localizable: false, schema: { localizable: true } }, -// ])("sets localizable to true, for supported schema.localizable values", ({localizable, schema}) => { -// -// const result = JsonFile.prototype.isLocalizable(localizable, schema); -// -// expect(result).toBe(true); -// }); -// -// it.each([ -// { localizable: false, schema: { localizable: "invalid keyword" } }, -// ])("sets localizable to false, for unsupported schema.localizable values", ({localizable, schema}) => { -// -// const result = JsonFile.prototype.isLocalizable(localizable, schema); -// -// expect(result).toBe(false); -// }); + +describe("JsonFile localizable schema keyword", () => { + test('parses localizable properties correctly', () => { + const project = createTestProject(); + const jsonFileType = new JsonFileType(project); + const jsonFile = new JsonFile({ + project: project, + type: jsonFileType, + pathName: './testfiles/schemas/localizable.json' + }); + + jsonFile.parse(`{ + "project.whateverModal.saveButton": { + "defaultMessage": "Save", + "description": "Button text for save" + } + }`); + + const set = jsonFile.getTranslationSet(); + const resource = set.get(ResourceString.hashKey("foo", "en-US", "project.whateverModal.saveButton", "json")); + + expect(resource).toBeTruthy(); + expect(resource.getSource()).toBe("Save"); + expect(resource.getKey()).toBe("project.whateverModal.saveButton"); + expect(resource.getComment()).toBe("Button text for save"); + }); + + test('extracts localizable properties correctly', () => { + const project = createTestProject(); + const jsonFileType = new JsonFileType(project); + const jsonFile = new JsonFile({ + project: project, + type: jsonFileType, + pathName: './testfiles/schemas/localizable.json' + }); + + jsonFile.parse(`{ + "project.whateverModal.saveButton": { + "defaultMessage": "Save", + "description": "Button text for save" + } + }`); + + const set = jsonFile.getTranslationSet(); + const resource = set.get(ResourceString.hashKey("foo", "en-US", "project.whateverModal.saveButton", "json")); + + expect(resource).toBeTruthy(); + + jsonFile.extract(); + const translationSet = jsonFile.getTranslationSet(); + const resources = translationSet.getAll(); + + expect(resources).toHaveLength(1); + }); +}); + +function createTestProject() { + return new CustomProject({ + name: 'foo', + id: 'foo', + sourceLocale: 'en-US' + }, './test/testfiles', { + locales: ['en-GB'], + targetDir: '.', + nopseudo: true, + json: { + schemas: [ + "./test/testfiles/schemas" + ], + mappings: { + "**/localizable.json": { + "schema": "localizable-schema", + "method": "copy", + "template": "resources/localizable_[locale].json" + } + } + } + }) +} diff --git a/packages/ilib-loctool-json/test/testfiles/json/localizable.json b/packages/ilib-loctool-json/test/testfiles/json/localizable.json new file mode 100644 index 000000000..0d0d88bcf --- /dev/null +++ b/packages/ilib-loctool-json/test/testfiles/json/localizable.json @@ -0,0 +1,18 @@ +{ + "project.whateverModal.saveButton": { + "defaultMessage": "Save", + "description": "Button text for save" + }, + "project.whateverModal.createButton": { + "defaultMessage": "Create", + "description": "Button text for create" + }, + "project.whateverModal.invalid": { + "defaultMessage": "Invalid", + "description": "Title for invalid" + }, + "project.whateverModal.nameLabel": { + "description": "Input label", + "defaultMessage": "Name" + } +} diff --git a/packages/ilib-loctool-json/test/testfiles/schemas/localizable-schema.json b/packages/ilib-loctool-json/test/testfiles/schemas/localizable-schema.json new file mode 100644 index 000000000..e5e0793da --- /dev/null +++ b/packages/ilib-loctool-json/test/testfiles/schemas/localizable-schema.json @@ -0,0 +1,25 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "localizable-schema", + "title": "Sample schema for testing localizable supported keywords (comment, key, source)", + "type": "object", + "additionalProperties": { + "type": "object", + "localizable": "key", + "properties": { + "defaultMessage": { + "type": "string", + "localizable": "source" + }, + "description": { + "type": "string", + "localizable": "comment" + } + }, + "required": [ + "defaultMessage", + "description" + ], + "additionalProperties": false + } +}