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

Commit

Permalink
Merge 44b009e into 907b974
Browse files Browse the repository at this point in the history
  • Loading branch information
Kilowhisky committed Apr 25, 2016
2 parents 907b974 + 44b009e commit 05d6844
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 7 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ npm install -g ember-i18n-csv
Export your ember-i18n localization files to a CSV file for translators:

```sh
ember-i18n-csv to-csv --locales-path=path_to_locales_folder --csv-path=i18n.csv
ember-i18n-csv to-csv --locales-path=path_to_locales_folder --csv-path=i18n.csv [--missing-only]
```

and import back in when you get them back:

```sh
ember-i18n-csv to-js --csv-path=i18n.csv --locales-path=path_to_locales_folder [--jshint-ignore]
ember-i18n-csv to-js --csv-path=i18n.csv --locales-path=path_to_locales_folder [--jshint-ignore] [--merge]
```

`--missing-only` means the generated CSV will only contain keys where there is a missing translation in one of the locales
`--merge` means that any keys present in the CSV will overwrite the ones present in the destination locale files but if the key is not present then the existing key will be kept in place.

###Excel

It's not as simple as opening, editing, and saving in Excel.
Expand Down
12 changes: 11 additions & 1 deletion bin/ember-i18n-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ var argv = require('yargs')
.options({
'jshint-ignore': {
type: 'boolean'
},
'only-missing': {
type: 'boolean'
},
'merge': {
type: 'boolean'
}
})
.argv;
Expand All @@ -16,9 +22,13 @@ var direction = argv._[0];
var localesPath = argv['locales-path'];
var csvPath = argv['csv-path'];
var ignoreJshint = argv['jshint-ignore'];
var onlyMissing = argv['only-missing'];
var merge = argv['merge'];

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

emberI18nCsv(direction, localesPath, csvPath, options);
6 changes: 4 additions & 2 deletions lib/to-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const stat = denodeify(fs.stat);
const writeFile = denodeify(fs.writeFile);
const stringify = denodeify(csvStringify);

export default function(localesPath, csvPath) {
export default function(localesPath, csvPath, { onlyMissing } = {}) {
return readdir(localesPath).then(locales => {
let prmoises = locales.map(locale => {
let filePath = path.resolve(localesPath, locale, 'translations.js');
Expand Down Expand Up @@ -76,7 +76,9 @@ export default function(localesPath, csvPath) {
let lines = [];
lines.push(['key'].concat(locales.map(locale => locale.replace('.js', ''))));
for (let i in keys) {
lines.push([keys[i]].concat(rows[i]));
if (!onlyMissing || rows[i].indexOf('') > -1) {
lines.push([keys[i]].concat(rows[i]));
}
}

return stringify(lines);
Expand Down
11 changes: 9 additions & 2 deletions lib/to-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const readFile = denodeify(fs.readFile);
const writeFile = denodeify(fs.writeFile);
const parse = denodeify(csvParse);

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

let objs = [];
for (i in locales) {
objs.push({});
let obj = {};
if (merge) {
let filePath = path.resolve(localesPath, locales[i], 'translations.js');
if (fs.existsSync(filePath)) {
obj = require(filePath).default;
}
}
objs.push(obj);
}

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

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

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('');
areFilesEqual('tmp/i18n.csv', 'test/fixtures/i18n-only-missing.csv').then(areSame => {
expect(areSame).to.be.true;
done();
}).catch(done);
});
});
});
25 changes: 25 additions & 0 deletions test/acceptance/to-js-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,29 @@ describe('acceptance - to-js', function() {
}).catch(done);
});
});

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

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').then(areSame => {
expect(areSame).to.be.true;
done();
}).catch(done);
});
});
});
6 changes: 6 additions & 0 deletions test/fixtures/i18n-merge.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
key,en-us,nl-nl,pt-br
node1.node1-1,Industries,Industrieën,Indústrias
node1.node1-2.node1-2-2,test,test,test
node2.node2-1,test,test,test
node2.node2-2.node2-2-2,test,test,test
weekday.saturday,Saturday,Zaterdag,Sábado
5 changes: 5 additions & 0 deletions test/fixtures/i18n-only-missing.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
key,en-us,nl-nl,pt-br
node2.node2-1,test,test,
node2.node2-2.node2-2-2,test,test,
weekday.tuesday,Tuesday,,Terça
weekday.saturday,Saturday,,Sábado
24 changes: 24 additions & 0 deletions test/fixtures/locales-only-missing/en-us/translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* jshint ignore:start */

export default {
"node1": {
"node1-1": "Industries",
"node1-2": {
"node1-2-1": "test",
"node1-2-2": "test"
}
},
"node2": {
"node2-1": "test",
"node2-2": {
"node2-2-1": "test",
"node2-2-2": "test"
}
},
"weekday": {
"tuesday": "Tuesday",
"saturday": "Saturday"
}
};

/* jshint ignore:end */
20 changes: 20 additions & 0 deletions test/fixtures/locales-only-missing/nl-nl/translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* jshint ignore:start */

export default {
"node1": {
"node1-1": "Industrieën",
"node1-2": {
"node1-2-1": "test",
"node1-2-2": "test"
}
},
"node2": {
"node2-1": "test",
"node2-2": {
"node2-2-1": "test",
"node2-2-2": "test"
}
},
};

/* jshint ignore:end */
22 changes: 22 additions & 0 deletions test/fixtures/locales-only-missing/pt-br/translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* jshint ignore:start */

export default {
"node1": {
"node1-1": "Indústrias",
"node1-2": {
"node1-2-1": "test",
"node1-2-2": "test"
}
},
"node2": {
"node2-2": {
"node2-2-1": "test",
}
},
"weekday": {
"tuesday": "Terça",
"saturday": "Sábado"
}
};

/* jshint ignore:end */
25 changes: 25 additions & 0 deletions test/integration/ember-i18n-csv-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ describe('integration - ember-i18n-csv', function() {
});
});

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

describe('empty folders', function() {
beforeEach(function() {
fs.emptyDirSync('test/fixtures/locales/unknown');
Expand Down Expand Up @@ -56,6 +64,23 @@ describe('integration - ember-i18n-csv', function() {
});
});
});
describe('merge', function() {
beforeEach(function() {
fs.copySync('test/fixtures/locales', 'tmp/locales', { clobber: true });
});

afterEach(function() {
fs.emptyDirSync('tmp');
});

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

it('handles invalid', function() {
Expand Down

0 comments on commit 05d6844

Please sign in to comment.