Skip to content

Commit

Permalink
Merge pull request #84 from i18next/fallbackKey
Browse files Browse the repository at this point in the history
[Trans Component] The fallback key should not be separated with key separator
  • Loading branch information
cheton committed Jun 26, 2018
2 parents 51e755d + bbb16c5 commit 0eaa7e9
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 30 deletions.
24 changes: 12 additions & 12 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "i18next-scanner",
"version": "2.6.0",
"version": "2.6.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 Expand Up @@ -48,19 +48,19 @@
],
"dependencies": {
"acorn-jsx": "^4.1.1",
"chalk": "^2.3.0",
"clone-deep": "^3.0.1",
"commander": "^2.13.0",
"deepmerge": "^2.0.1",
"chalk": "^2.4.1",
"clone-deep": "^4.0.0",
"commander": "^2.15.1",
"deepmerge": "^2.1.1",
"ensure-array": "^1.0.0",
"eol": "^0.9.1",
"esprima": "^4.0.0",
"gulp-sort": "^2.0.0",
"lodash": "^4.0.0",
"parse5": "^4.0.0",
"parse5": "^5.0.0",
"sortobject": "^1.1.1",
"through2": "^2.0.3",
"vinyl": "^2.1.0",
"vinyl": "^2.2.0",
"vinyl-fs": "^3.0.1"
},
"devDependencies": {
Expand All @@ -70,16 +70,16 @@
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"coveralls": "^3.0.0",
"eslint": "^4.15.0",
"eslint-config-trendmicro": "^1.3.0",
"eslint-plugin-import": "^2.8.0",
"eslint": "^5.0.0",
"eslint-config-trendmicro": "^1.3.1",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.5.1",
"eslint-plugin-react": "^7.10.0",
"gulp": "^3.9.1",
"gulp-tap": "^1.0.1",
"gulp-util": "^3.0.8",
"sha1": "^1.1.1",
"tap": "^11.0.1",
"tap": "^12.0.1",
"text-table": "^0.2.0"
}
}
31 changes: 19 additions & 12 deletions src/parser.js
Expand Up @@ -660,15 +660,25 @@ class Parser {
key = parts[1];
}

if (!key && options.fallbackKey === true) {
key = options.defaultValue;
}
if (!key && typeof options.fallbackKey === 'function') {
key = options.fallbackKey(ns, options.defaultValue);
}
if (!key) {
// Ignore empty key
return;
let keys = [];

if (key) {
keys = _.isString(keySeparator) ? key.split(keySeparator) : [key];
} else {
// fallback key
if (options.fallbackKey === true) {
key = options.defaultValue;
}
if (typeof options.fallbackKey === 'function') {
key = options.fallbackKey(ns, options.defaultValue);
}

if (!key) {
// Ignore empty key
return;
}

keys = [key];
}

const {
Expand All @@ -682,9 +692,6 @@ class Parser {
defaultLng,
defaultValue
} = this.options;
const keys = _.isString(keySeparator)
? key.split(keySeparator)
: [key];

lngs.forEach((lng) => {
let resLoad = this.resStore[lng] && this.resStore[lng][ns];
Expand Down
12 changes: 6 additions & 6 deletions test/parser.js
Expand Up @@ -105,7 +105,7 @@ test('Parse Trans components', (t) => {
fallbackKey: true
},
nsSeparator: false,
keySeparator: false,
keySeparator: '.', // Specify the keySeparator for this test to make sure the fallbackKey won't be separated
fallbackLng: 'en'
});

Expand Down Expand Up @@ -162,7 +162,7 @@ test('Parse Trans components with fallback key', (t) => {
}
},
nsSeparator: false,
keySeparator: false,
keySeparator: '.', // Specify the keySeparator for this test to make sure the fallbackKey won't be separated
fallbackLng: 'en'
});

Expand Down Expand Up @@ -219,8 +219,8 @@ test('Parse wrapped Trans components', (t) => {
fallbackKey: true
},
nsSeparator: false,
keySeparator: false,
fallbackLng: 'en'
keySeparator: '.', // Specify the keySeparator for this test to make sure the fallbackKey won't be separated
fallbackLng: 'en',
});

const content = fs.readFileSync(path.resolve(__dirname, 'fixtures/app.jsx'), 'utf-8');
Expand Down Expand Up @@ -566,7 +566,7 @@ test('Override keySeparator with a false value', (t) => {

test('Multiline (Line Endings: LF)', (t) => {
const parser = new Parser({
nsSeparator: false,
nsSeparator: false
});
const content = fs.readFileSync(path.resolve(__dirname, 'fixtures/multiline-unix.js'), 'utf-8');
parser.parseFuncFromString(content);
Expand All @@ -583,7 +583,7 @@ test('Multiline (Line Endings: LF)', (t) => {

test('Multiline (Line Endings: CRLF)', (t) => {
const parser = new Parser({
nsSeparator: false,
nsSeparator: false
});
const content = fs.readFileSync(path.resolve(__dirname, 'fixtures/multiline-dos.js'), 'utf-8');
parser.parseFuncFromString(content);
Expand Down
65 changes: 65 additions & 0 deletions test/transform-stream.js
Expand Up @@ -12,6 +12,9 @@ const defaults = {
func: {
list: ['_t', 't']
},
trans: {
fallbackKey: true
},
lngs: ['en','de'],
ns: [
'locale',
Expand Down Expand Up @@ -152,6 +155,68 @@ test('[Key Based Fallback] defaultValue as function', function(t) {
}));
});

test('[Trans Component] fallbackKey', function(t) {
const options = _.merge({}, defaults, {
trans: {
extensions: ['.js', '.jsx'], // with extensions
fallbackKey: function(ns, value) {
return value;
}
},
nsSeparator: false,
keySeparator: '.' // Specify the keySeparator for this test to make sure the fallbackKey won't be separated
});

gulp.src('test/fixtures/**/app.jsx')
.pipe(scanner(options))
.on('end', function() {
t.end();
})
.pipe(tap(function(file) {
const contents = file.contents.toString();

if (file.path === 'i18n/en/resource.json') {
const found = JSON.parse(contents);
const wanted = {
// quote style
"jsx-quotes-double": "Use double quotes for the i18nKey attribute",
"jsx-quotes-single": "Use single quote for the i18nKey attribute",

// plural
"plural": "You have <1>{{count}}</1> apples",
"plural_plural": "You have <1>{{count}}</1> apples",

// context
"context": "A boyfriend",
"context_male": "A boyfriend",

// i18nKey
"multiline-text-string": "multiline text string",
"string-literal": "This is a <1>test</1>",
"object-expression": "This is a <1><0>{{test}}</0></1>",
"arithmetic-expression": "2 + 2 = <1>{{result}}</1>",
"components": "Go to <1>Administration > Tools</1> to download administrative tools.",
"lorem-ipsum": "<0>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</0>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<2>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</2>",
"lorem-ipsum-nested": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.<1>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<1>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</1></1><2>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</2>",

// fallback key
"multiline text string": "multiline text string",
"This is a <1>test</1>": "This is a <1>test</1>",
"This is a <1><0>{{test}}</0></1>": "This is a <1><0>{{test}}</0></1>",
"2 + 2 = <1>{{result}}</1>": "2 + 2 = <1>{{result}}</1>",
"Go to <1>Administration > Tools</1> to download administrative tools.": "Go to <1>Administration > Tools</1> to download administrative tools.",
"<0>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</0>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<2>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</2>": "<0>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</0>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<2>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</2>",
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.<1>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<1>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</1></1><2>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</2>": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.<1>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<1>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</1></1><2>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</2>",

// defaults
"The component might be self-closing": "The component might be self-closing",
"Hello <1>{{planet}}</1>!": "Hello <1>{{planet}}</1>!",
};
t.same(found, wanted);
}
}));
});

test('Empty result', function(t) {
const options = _.merge({}, defaults, {
func: {
Expand Down

0 comments on commit 0eaa7e9

Please sign in to comment.