Skip to content

Commit

Permalink
feat: add metadata attribute for passing context information about …
Browse files Browse the repository at this point in the history
…the translation key/string (#230)

* feat: Add externalOptions support

* cleanup

* cleanup

* update handleObject expression

* Add test

* Add comment

* Update readme

* update description and test

* update key name

* update file name

Co-authored-by: webguy-github-ssh-key <[email protected]>
  • Loading branch information
mmohajer and webguy-github-ssh-key authored Jun 16, 2022
1 parent ceb4c2c commit 8b5407c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 822,12 @@ interpolation options
## Integration Guide
Checkout [Integration Guide](https://github.com/i18next/i18next-scanner/wiki/Integration-Guide) to learn how to integrate with [React](https://github.com/i18next/i18next-scanner/wiki/Integration-Guide#react), [Gettext Style I18n](https://github.com/i18next/i18next-scanner/wiki/Integration-Guide#gettext-style-i18n), and [Handlebars](https://github.com/i18next/i18next-scanner/wiki/Integration-Guide#handlebars).

#### metadata

Type: `Object` Default: `{}`

This can be used to pass any additional information regarding the string.

## License

MIT
55 changes: 44 additions & 11 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 95,8 @@ const defaults = {
interpolation: {
prefix: '{{', // prefix for interpolation
suffix: '}}' // suffix for interpolation
}
},
metadata: {} // additional custom options
};

// http://codereview.stackexchange.com/questions/45991/balanced-parentheses
Expand Down Expand Up @@ -366,6 367,46 @@ class Parser {
return fixedString;
}

handleObjectExpression(props) {
return props.reduce((acc, prop) => {
if (prop.type !== 'ObjectMethod') {
const value = this.optionsBuilder(prop.value);
if (value !== undefined) {
return {
...acc,
[prop.key.name]: value
};
}
}
return acc;
}, {});
}

handleArrayExpression(elements) {
return elements.reduce((acc, element) => [
...acc,
this.optionsBuilder(element)
],
[],);
}

optionsBuilder(prop) {
if (prop.value && prop.value.type === 'Literal' || prop.type && prop.type === 'Literal') {
return prop.value.value !== undefined ? prop.value.value : prop.value;
} else if (prop.value && prop.value.type === 'TemplateLiteral' || prop.type && prop.type === 'TemplateLiteral') {
return prop.value.quasis.map((element) => {
return element.value.cooked;
}).join('');
} else if (prop.value && prop.value.type === 'ObjectExpression' || prop.type && prop.type === 'ObjectExpression') {
return this.handleObjectExpression(prop.value.properties);
} else if (prop.value && prop.value.type === 'ArrayExpression' || prop.type && prop.type === 'ArrayExpression') {
return this.handleArrayExpression(prop.elements);
} else {
// Unable to get value of the property
return '';
}
}

// i18next.t('ns:foo.bar') // matched
// i18next.t("ns:foo.bar") // matched
// i18next.t('ns:foo.bar') // matched
Expand Down Expand Up @@ -450,20 491,12 @@ class Parser {
'ns',
'keySeparator',
'nsSeparator',
'metadata',
];

props.forEach((prop) => {
if (_.includes(supportedOptions, prop.key.name)) {
if (prop.value.type === 'Literal') {
options[prop.key.name] = prop.value.value;
} else if (prop.value.type === 'TemplateLiteral') {
options[prop.key.name] = prop.value.quasis
.map(element => element.value.cooked)
.join('');
} else {
// Unable to get value of the property
options[prop.key.name] = '';
}
options[prop.key.name] = this.optionsBuilder(prop);
}
});
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
i18next.t('friend', {metadata: {tags: ['tag1', 'tag2']}});
25 changes: 25 additions & 0 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1198,3 1198,28 @@ test('Default values test', (t) => {

t.end();
});

test('metadata', (t) => {
const parser = new Parser();
const content = fs.readFileSync(path.resolve(__dirname, 'fixtures/metadata.js'), 'utf-8');
const customHandler = function(key, options) {
parser.set(key, options);
t.same(options, {
'metadata': {
'tags': [
'tag1',
'tag2',
],
},
});
};
parser.parseFuncFromString(content, customHandler);
t.same(parser.get(), {
en: {
translation: {
'friend': '',
}
}
});
t.end();
});

0 comments on commit 8b5407c

Please sign in to comment.