Skip to content

Commit

Permalink
Use a different delimiter to build the translation map. (#36)
Browse files Browse the repository at this point in the history
* Use a different delimiter to build the translation map.

* Period needs to be in the key.

* Add built index.js

* add static delimiter

* add build output
  • Loading branch information
Franklin89 authored Dec 9, 2021
1 parent 00ebe79 commit 025a086
Show file tree
Hide file tree
Showing 3 changed files with 451 additions and 131 deletions.
30 changes: 19 additions & 11 deletions __tests__/parsers/json-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ const content = JSON.stringify(
msg2: "world"
},
},
msg3: "Doe"
msg3: "Doe",
"msg.4": "this is a fullstop.",
"msg.5.": "this is a fullstop. With another sentance."
}, null, '\t');

test('JSON PARSER: correctly parses from string', async () => {
const file = await parser.parseFrom(content);
expect(file).toBeTruthy();
expect(file['messages.foo.msg1']).toEqual('hello {{variable}}');
expect(file['messages.foo.msg2']).toEqual('world');
expect(file['messages.bar']).toEqual('John');
expect(file[`messages${JsonParser.DELIMITER}foo${JsonParser.DELIMITER}msg1`]).toEqual('hello {{variable}}');
expect(file[`messages${JsonParser.DELIMITER}foo${JsonParser.DELIMITER}msg2`]).toEqual('world');
expect(file[`messages${JsonParser.DELIMITER}bar`]).toEqual('John');
expect(file['msg3']).toEqual('Doe');
expect(file['msg.4']).toEqual('this is a fullstop.');
expect(file['msg.5.']).toEqual('this is a fullstop. With another sentance.');
});

test('JSON PARSER: correctly formats back as string', async () => {
Expand All @@ -36,14 +40,16 @@ test('JSON PARSER: correctly applies translations', async () => {
expect(file).toBeTruthy();

const result = parser.applyTranslations(file, {
'messages.foo.msg2': 'Does this work?'
'messages[--]foo[--]msg2': 'Does this work?'
});

expect(result).toBeTruthy();
expect(file['messages.foo.msg1']).toEqual('hello {{variable}}');
expect(file['messages.foo.msg2']).toEqual('Does this work?');
expect(file['messages.bar']).toEqual('John');
expect(file[`messages${JsonParser.DELIMITER}foo${JsonParser.DELIMITER}msg1`]).toEqual('hello {{variable}}');
expect(file[`messages${JsonParser.DELIMITER}foo${JsonParser.DELIMITER}msg2`]).toEqual('Does this work?');
expect(file[`messages${JsonParser.DELIMITER}bar`]).toEqual('John');
expect(file['msg3']).toEqual('Doe');
expect(file['msg.4']).toEqual('this is a fullstop.');
expect(file['msg.5.']).toEqual('this is a fullstop. With another sentance.');
});

test('JSON PARSER: correctly creates translatable text map', async () => {
Expand All @@ -52,8 +58,10 @@ test('JSON PARSER: correctly creates translatable text map', async () => {

const translatableTextMap = parser.toTranslatableTextMap(file);
expect(translatableTextMap).toBeTruthy();
expect(translatableTextMap.text.get('messages.foo.msg1')).toEqual('hello {{variable}}');
expect(translatableTextMap.text.get('messages.foo.msg2')).toEqual('world');
expect(translatableTextMap.text.get('messages.bar')).toEqual('John');
expect(translatableTextMap.text.get(`messages${JsonParser.DELIMITER}foo${JsonParser.DELIMITER}msg1`)).toEqual('hello {{variable}}');
expect(translatableTextMap.text.get(`messages${JsonParser.DELIMITER}foo${JsonParser.DELIMITER}msg2`)).toEqual('world');
expect(translatableTextMap.text.get(`messages${JsonParser.DELIMITER}bar`)).toEqual('John');
expect(translatableTextMap.text.get('msg3')).toEqual('Doe');
expect(translatableTextMap.text.get('msg.4')).toEqual('this is a fullstop.');
expect(translatableTextMap.text.get('msg.5.')).toEqual('this is a fullstop. With another sentance.');
});
Loading

0 comments on commit 025a086

Please sign in to comment.