Skip to content

Commit

Permalink
Merge c28c8fd into 33a8279
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Jun 7, 2018
2 parents 33a8279 + c28c8fd commit 2f7e958
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
19 changes: 16 additions & 3 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const defaults = {
trans: { // Trans component (https://github.com/i18next/react-i18next)
component: 'Trans',
i18nKey: 'i18nKey',
defaultsKey: 'defaults',
extensions: ['.js', '.jsx'],
fallbackKey: false
},
Expand Down Expand Up @@ -140,6 +141,9 @@ const transformOptions = (options) => {
if (_.isUndefined(_.get(options, 'trans.i18nKey'))) {
_.set(options, 'trans.i18nKey', defaults.trans.i18nKey);
}
if (_.isUndefined(_.get(options, 'trans.defaultsKey'))) {
_.set(options, 'trans.defaultsKey', defaults.trans.defaultsKey);
}
if (_.isUndefined(_.get(options, 'trans.extensions'))) {
_.set(options, 'trans.extensions', defaults.trans.extensions);
}
Expand Down Expand Up @@ -406,8 +410,9 @@ class Parser {

const component = opts.component || this.options.trans.component;
const i18nKey = opts.i18nKey || this.options.trans.i18nKey;
const defaultsKey = opts.defaultsKey || this.options.trans.defaultsKey;

const reTrans = new RegExp('<' + component + '([^]*?)>([^]*?)</\\s*' + component + '\\s*>', 'gim');
const reTrans = new RegExp('<' + component + '([^]*?)((/>)|(>([^]*?)</\\s*' + component + '\\s*>))', 'gim');
const reAttribute = /\b(\S+)\s*=\s*({.*?}|".*?"|'.*?')/gm;

let r;
Expand All @@ -426,10 +431,18 @@ class Parser {
continue;
}

let defaultsString;
try {
defaultsString = attributes[defaultsKey] ? getStringFromAttribute(attributes[defaultsKey]) : '';
} catch (e) {
this.log(`i18next-scanner: i18nKey value must be a static string, saw ${chalk.yellow(attributes[defaultsKey])}`);
continue;
}

const key = _.trim(transKey || '');
const fragment = _.trim(r[2]).replace(/\s+/g, ' ');
const fragment = _.trim(r[5]).replace(/\s+/g, ' ');
const options = {
defaultValue: jsxToText(fragment),
defaultValue: defaultsString || jsxToText(fragment),
fallbackKey: opts.fallbackKey || this.options.trans.fallbackKey
};
if (attributes.count) {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ const mycomp = () => (
<Trans i18nKey="key9" context="male">A boyfriend</Trans>
<I18N __t="key10">A wrapper component with key</I18N>
<I18N>A wrapper component without key</I18N>
<Trans i18nKey="key11" defaults="The component might be self-closing" />
<Trans i18nKey="key12" defaults="Hello <0>{{planet}}</0>!" tOptions={{planet: "World"}} components={[<strong>stuff</strong>]} />
</React.Fragment>
)
6 changes: 5 additions & 1 deletion test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ test('Parse Trans component #1', (t) => {
"We can use Trans without i18nKey=\"...\" as well!": "We can use Trans without i18nKey=\"...\" as well!",
"key9": "A boyfriend",
"key9_male": "A boyfriend",
"key11": "The component might be self-closing",
"key12": "Hello <0>{{planet}}</0>!"
}
}
});
Expand Down Expand Up @@ -165,7 +167,9 @@ test('Parse Trans component #2', (t) => {
"8f5c444dd42fe9a3e42a8ab3a677e04a4a708105_plural": "key8 default <1>{{count}}</1>",
"09e944775f89d688fd87cf7abc95a737dd4c54f6": "We can use Trans without i18nKey=\"...\" as well!",
"key9": "A boyfriend",
"key9_male": "A boyfriend"
"key9_male": "A boyfriend",
"key11": "The component might be self-closing",
"key12": "Hello <0>{{planet}}</0>!"
}
}
});
Expand Down

0 comments on commit 2f7e958

Please sign in to comment.