This repository has been archived by the owner on Feb 15, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kelly Selden
committed
Jan 5, 2016
1 parent
80f7bf1
commit b0bc31d
Showing
4 changed files
with
124 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var stringify = require('csv-stringify'); | ||
var eol = require('eol'); | ||
require('6to5/register'); | ||
|
||
module.exports = function(localesPath, csvPath) { | ||
var locales = fs.readdirSync(localesPath); | ||
|
||
var keys = []; | ||
var rows = []; | ||
|
||
function recurse(json, columnIndex, rowIndex, currentKey) { | ||
for (var key in json) { | ||
var value = json[key]; | ||
var newKey = currentKey; | ||
if (newKey) { | ||
newKey += '.'; | ||
} | ||
newKey += key; | ||
if (typeof value === 'object') { | ||
rowIndex = recurse(value, columnIndex, rowIndex, newKey); | ||
} else { | ||
var row; | ||
var index = keys.indexOf(newKey); | ||
if (index !== -1) { | ||
row = rows[index]; | ||
} else { | ||
row = []; | ||
rows.splice(rowIndex, 0, row); | ||
keys.splice(rowIndex, 0, newKey); | ||
} | ||
for (var i in locales) { | ||
if (i == columnIndex) { | ||
row[i] = value; | ||
} else if (!row[i]) { | ||
row[i] = ''; | ||
} | ||
} | ||
rowIndex++; | ||
} | ||
} | ||
return rowIndex; | ||
} | ||
|
||
for (var columnIndex in locales) { | ||
var locale = locales[columnIndex]; | ||
var filePath = path.resolve(localesPath, locale, 'translations.js'); | ||
var json = require(filePath); | ||
recurse(json, columnIndex, 0, ''); | ||
} | ||
|
||
var lines = []; | ||
lines.push(['key'].concat(locales.map(function(locale) { return locale.replace('.js', ''); }))); | ||
for (var i in keys) { | ||
lines.push([keys[i]].concat(rows[i])); | ||
} | ||
|
||
stringify(lines, function(err, csv) { | ||
var normalized = eol.auto(csv); | ||
fs.writeFileSync(csvPath, normalized); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var parse = require('csv-parse'); | ||
var eol = require('eol'); | ||
|
||
module.exports = function(csvPath, localesPath, ignoreJshint) { | ||
var csv = fs.readFileSync(csvPath, 'utf8'); | ||
|
||
parse(csv, function(err, lines) { | ||
var i, columnIndex; | ||
var locales = lines.shift().slice(1); | ||
|
||
var objs = []; | ||
for (i in locales) { | ||
objs.push({}); | ||
} | ||
|
||
function recurse(keySections, obj, value) { | ||
var key = keySections[0]; | ||
if (keySections.length > 1) { | ||
if (!obj[key]) { | ||
obj[key] = {}; | ||
} | ||
recurse(keySections.slice(1), obj[key], value); | ||
} else { | ||
obj[key] = value; | ||
} | ||
} | ||
|
||
for (i in lines) { | ||
var line = lines[i]; | ||
var key = line.shift(); | ||
var keySections = key.split('.'); | ||
for (columnIndex in line) { | ||
var obj = objs[columnIndex]; | ||
var value = line[columnIndex]; | ||
recurse(keySections, obj, value); | ||
} | ||
} | ||
|
||
for (columnIndex in locales) { | ||
var locale = locales[columnIndex]; | ||
var localePath = path.join(localesPath, locale); | ||
if (!fs.existsSync(localePath)) { | ||
fs.mkdirSync(localePath); | ||
} | ||
var filePath = path.join(localePath, 'translations.js'); | ||
var jsonString = JSON.stringify(objs[columnIndex], null, 2); | ||
var string = 'export default ' + jsonString + ';\n'; | ||
if (ignoreJshint) { | ||
string = '/* jshint ignore:start */\n\n' + string + '\n/* jshint ignore:end */\n'; | ||
} | ||
string = eol.auto(string); | ||
fs.writeFileSync(filePath, string); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,7 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var stringify = require('csv-stringify'); | ||
var eol = require('eol'); | ||
var argv = require('yargs').argv; | ||
require('6to5/register'); | ||
var toCsv = require('./lib/to-csv'); | ||
|
||
var localesPath = argv._[0]; | ||
var csvPath = argv._[1]; | ||
|
||
var locales = fs.readdirSync(localesPath); | ||
|
||
var keys = []; | ||
var rows = []; | ||
|
||
function recurse(json, columnIndex, rowIndex, currentKey) { | ||
for (var key in json) { | ||
var value = json[key]; | ||
var newKey = currentKey; | ||
if (newKey) { | ||
newKey += '.'; | ||
} | ||
newKey += key; | ||
if (typeof value === 'object') { | ||
rowIndex = recurse(value, columnIndex, rowIndex, newKey); | ||
} else { | ||
var row; | ||
var index = keys.indexOf(newKey); | ||
if (index !== -1) { | ||
row = rows[index]; | ||
} else { | ||
row = []; | ||
rows.splice(rowIndex, 0, row); | ||
keys.splice(rowIndex, 0, newKey); | ||
} | ||
for (var i in locales) { | ||
if (i == columnIndex) { | ||
row[i] = value; | ||
} else if (!row[i]) { | ||
row[i] = ''; | ||
} | ||
} | ||
rowIndex++; | ||
} | ||
} | ||
return rowIndex; | ||
} | ||
|
||
for (var columnIndex in locales) { | ||
var locale = locales[columnIndex]; | ||
var filePath = path.join(localesPath, locale, 'translations.js'); | ||
if (!path.isAbsolute(filePath)) { | ||
filePath = './' + filePath; | ||
} | ||
var json = require(filePath); | ||
recurse(json, columnIndex, 0, ''); | ||
} | ||
|
||
var lines = []; | ||
lines.push(['key'].concat(locales.map(function(locale) { return locale.replace('.js', ''); }))); | ||
for (var i in keys) { | ||
lines.push([keys[i]].concat(rows[i])); | ||
} | ||
|
||
stringify(lines, function(err, csv) { | ||
var normalized = eol.auto(csv); | ||
fs.writeFileSync(csvPath, normalized); | ||
}); | ||
toCsv(localesPath, csvPath); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,8 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var parse = require('csv-parse'); | ||
var eol = require('eol'); | ||
var argv = require('yargs').argv; | ||
var toJs = require('./lib/to-js'); | ||
|
||
var csvPath = argv._[0]; | ||
var localesPath = argv._[1]; | ||
var ignoreJshint = argv['jshint-ignore']; | ||
|
||
var csv = fs.readFileSync(csvPath, 'utf8'); | ||
|
||
parse(csv, function(err, lines) { | ||
var i, columnIndex; | ||
var locales = lines.shift().slice(1); | ||
|
||
var objs = []; | ||
for (i in locales) { | ||
objs.push({}); | ||
} | ||
|
||
function recurse(keySections, obj, value) { | ||
var key = keySections[0]; | ||
if (keySections.length > 1) { | ||
if (!obj[key]) { | ||
obj[key] = {}; | ||
} | ||
recurse(keySections.slice(1), obj[key], value); | ||
} else { | ||
obj[key] = value; | ||
} | ||
} | ||
|
||
for (i in lines) { | ||
var line = lines[i]; | ||
var key = line.shift(); | ||
var keySections = key.split('.'); | ||
for (columnIndex in line) { | ||
var obj = objs[columnIndex]; | ||
var value = line[columnIndex]; | ||
recurse(keySections, obj, value); | ||
} | ||
} | ||
|
||
for (columnIndex in locales) { | ||
var locale = locales[columnIndex]; | ||
var localePath = path.join(localesPath, locale); | ||
if (!fs.existsSync(localePath)) { | ||
fs.mkdirSync(localePath); | ||
} | ||
var filePath = path.join(localePath, 'translations.js'); | ||
var jsonString = JSON.stringify(objs[columnIndex], null, 2); | ||
var string = 'export default ' + jsonString + ';\n'; | ||
if (ignoreJshint) { | ||
string = '/* jshint ignore:start */\n\n' + string + '\n/* jshint ignore:end */\n'; | ||
} | ||
string = eol.auto(string); | ||
fs.writeFileSync(filePath, string); | ||
} | ||
}); | ||
toJs(csvPath, localesPath, ignoreJshint); |