Skip to content

Commit

Permalink
Add a defaultLng option for checking default values (closes #69)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Jan 20, 2018
1 parent 09abd72 commit 891fa2c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ module.exports = {
'locale',
'resource'
],
defaultLng: 'en',
defaultNs: 'resource',
defaultValue: '__STRING_NOT_TRANSLATED__',
resource: {
Expand Down Expand Up @@ -498,6 +499,7 @@ Below are the configuration options with their default values:
},
lngs: ['en'],
ns: ['translation'],
defaultLng: 'en',
defaultNs: 'translation',
defaultValue: '',
resource: {
Expand Down Expand Up @@ -620,6 +622,12 @@ Type: `String` or `Array` Default: `['translation']`

A namespace string or an array of namespaces.

#### defaultLng

Type: `String` Default: `'en'`

The default language used for checking default values.

#### defaultNs

Type: `String` Default: `'translation'`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "i18next-scanner",
"version": "2.4.0",
"version": "2.4.1",
"description": "Scan your code, extract translation keys/values, and merge them into i18n resource files.",
"homepage": "https://github.com/i18next/i18next-scanner",
"author": "Cheton Wu <cheton@gmail.com>",
Expand Down
10 changes: 6 additions & 4 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const defaults = {

ns: [], // string or array of namespaces

defaultLng: 'en', // default language used for checking default values

defaultNs: 'translation', // default namespace used if not passed to translation function

defaultValue: '', // default value used if not passed to `parser.set`
Expand Down Expand Up @@ -636,6 +638,7 @@ class Parser {
plural,
pluralFallback,
pluralSeparator,
defaultLng,
defaultValue
} = this.options;
const keys = _.isString(keySeparator)
Expand Down Expand Up @@ -743,10 +746,9 @@ class Parser {
if (!resLoad[resKey]) {
// Use `options.defaultValue` if specified
resLoad[resKey] = options.defaultValue;
} else if (resLoad[resKey] !== options.defaultValue) {
// We already had a different default.
const k = chalk.yellow(JSON.stringify(resKey));
this.log(`i18next-scanner: Translation key ${k} has multiple different default values. Using first default`);
} else if ((resLoad[resKey] !== options.defaultValue) && (lng === defaultLng)) {
// A default value has provided but it's different with the expected default
this.log(`i18next-scanner: The translation key ${chalk.yellow(JSON.stringify(resKey))} has a different default value, you may need to check the translation key of default language (${defaultLng})`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test('set warns about conflicting defaults', (t) => {
parser.set('key', { defaultValue: 'Default text' });
parser.set('key', { defaultValue: 'Another text' });
t.same(parser.get('key'), 'Default text');
t.match(logText, /different default/);
t.match(logText, /different default value/);
t.end();
});

Expand Down

0 comments on commit 891fa2c

Please sign in to comment.