Skip to content
This repository has been archived by the owner on Feb 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #57 from osmanorhan/master
Browse files Browse the repository at this point in the history
added dotted key export notation to to-js.
  • Loading branch information
Kelly Selden committed Jun 24, 2016
2 parents 2144b21 + 63c1ace commit 2d33372
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 9 deletions.
7 changes: 6 additions & 1 deletion bin/ember-i18n-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var argv = require('yargs')
},
'merge': {
type: 'boolean'
},
'dotted':{
type: 'boolean'
}
})
.argv;
Expand All @@ -24,11 +27,13 @@ var csvPath = argv['csv-path'];
var ignoreJshint = argv['jshint-ignore'];
var onlyMissing = argv['only-missing'];
var merge = argv['merge'];
var dotted = argv['dotted'];

var options = {
ignoreJshint: ignoreJshint,
onlyMissing: onlyMissing,
merge: merge
merge: merge,
dotted:dotted
};

emberI18nCsv(direction, localesPath, csvPath, options);
20 changes: 12 additions & 8 deletions lib/to-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const readFile = denodeify(fs.readFile);
const writeFile = denodeify(fs.writeFile);
const parse = denodeify(csvParse);

export default function(csvPath, localesPath, { ignoreJshint, merge } = {}) {
export default function(csvPath, localesPath, { ignoreJshint, merge, dotted } = {}) {
return readFile(csvPath, 'utf8').then(csv => {
return parse(csv);
}).then(lines => {
Expand All @@ -30,14 +30,18 @@ export default function(csvPath, localesPath, { ignoreJshint, merge } = {}) {
}

function recurse(keySections, obj, value) {
let key = keySections[0];
if (keySections.length > 1) {
if (!obj[key]) {
obj[key] = {};
if(dotted){
obj[keySections.join('.')] = value;
}else{
let key = keySections[0];
if (keySections.length > 1) {
if (!obj[key]) {
obj[key] = {};
}
recurse(keySections.slice(1), obj[key], value);
} else {
obj[key] = value;
}
recurse(keySections.slice(1), obj[key], value);
} else {
obj[key] = value;
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/acceptance/to-js-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,28 @@ describe('acceptance - to-js', function() {
}).catch(done);
});
});

it('handles option --dotted', function(done) {
let ps = spawn(process.execPath, [
'bin/ember-i18n-csv.js',
'to-js',
'--csv-path=test/fixtures/i18n-dotted.csv',
'--locales-path=tmp/locales',
'--dotted'
]);

let out = '';
let err = '';
ps.stdout.on('data', buffer => out += buffer);
ps.stderr.on('data', buffer => err += buffer);

ps.on('exit', () => {
expect(out).to.equal('');
expect(err).to.equal('');
areDirsEqual('tmp/locales', 'test/fixtures/locales-dotted').then(areSame => {
expect(areSame).to.be.true;
done();
}).catch(done);
});
});
});
5 changes: 5 additions & 0 deletions test/fixtures/i18n-dotted.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
key,en-us,nl-nl,pt-br
node1.node1-1,Industries,Industrieën,Indústrias
node1.node1-2.node1-2-1,test,test,test


4 changes: 4 additions & 0 deletions test/fixtures/locales-dotted/en-us/translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
"node1.node1-1":"Industries",
"node1.node1-2.node1-2-1":"test"
};
4 changes: 4 additions & 0 deletions test/fixtures/locales-dotted/nl-nl/translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
"node1.node1-1":"Industrieën",
"node1.node1-2.node1-2-1":"test"
};
5 changes: 5 additions & 0 deletions test/fixtures/locales-dotted/pt-br/translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
"node1.node1-1":"Indústrias",
"node1.node1-2.node1-2-1":"test"
};

9 changes: 9 additions & 0 deletions test/integration/ember-i18n-csv-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('integration - ember-i18n-csv', function() {
});
});
});

describe('merge', function() {
beforeEach(function() {
fs.copySync('test/fixtures/locales-with-missing-keys', 'tmp/locales', { clobber: true });
Expand All @@ -80,6 +81,14 @@ describe('integration - ember-i18n-csv', function() {
});
});
});

it('handles option --dotted', function() {
return emberI18nCsv('to-js', 'tmp/locales', 'test/fixtures/i18n-dotted.csv', { dotted: true }).then(() => {
return areDirsEqual('tmp/locales', 'test/fixtures/locales-dotted').then(areSame => {
expect(areSame).to.be.true;
});
});
});
});
});

Expand Down

0 comments on commit 2d33372

Please sign in to comment.