Skip to content

Commit

Permalink
[ilib-loctool-json] Add test for localizable keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
Natalia Kędziora committed Mar 7, 2025
1 parent 57fca72 commit debe810
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/ilib-loctool-json/JsonFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
101 changes: 79 additions & 22 deletions packages/ilib-loctool-json/test/JsonFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
})
}
18 changes: 18 additions & 0 deletions packages/ilib-loctool-json/test/testfiles/json/localizable.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit debe810

Please sign in to comment.