Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
omair100 committed Jan 18, 2016
0 parents commit a99634e
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
96 changes: 96 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env node

var program = require('commander');
var fs = require('fs');
var path = require('path');

function merge(obj1, obj2){
var obj3 = {};
for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
for (var attrname2 in obj2) { obj3[attrname2] = obj2[attrname2]; }
return obj3;
}

function upperCaseText(match, group1) {
return group1.toUpperCase();
}

function eachRecursive(obj, key) {
var newObject = {};

for (var k in obj) {
var newKey = k;
newKey = (key) ? key + "." + newKey : newKey;

if (typeof obj[k] == "object" && obj[k] !== null) {
newObject = merge(newObject, eachRecursive(obj[k], newKey));
}

if (typeof obj[k] === "string") {
var keyId = newKey.replace(/\.(.)/g, upperCaseText);

newObject[keyId] = {
id: newKey,
defaultMessage: obj[k]
};
}
}

return newObject;
}

function eachTranslation(obj, key) {
var newObject = {};

for (var k in obj) {
var newKey = k;
newKey = (key) ? key + "." + newKey : newKey;

if (typeof obj[k] == "object" && obj[k] !== null) {
newObject = merge(newObject, eachTranslation(obj[k], newKey));
}

if (typeof obj[k] === "string") {
newObject[newKey] = obj[k];
}
}

return newObject;
}

function flatten(args) {
var filename = args[0];

fs.exists(filename, function(exists) {
if (exists) {
var fileObj = require(filename);

var newObj = eachRecursive(fileObj);

var newFilename = path.basename(filename, path.extname(filename))
+ "-default-flat.json";

var translation = eachTranslation(fileObj);

console.log(translation);

fs.writeFile(newFilename, JSON.stringify(newObj), function(err) {
if (err) {
return console.log(err);
}

console.log(newFilename + " saved");
});
} else {
console.error('File does not exist');
}
});
}

program
.version('0.0.1')
.usage('[options] <file ...>')
.option('-f, --flatten', 'Flatten')
.parse(process.argv);

if (program.flatten) flatten(program.args);
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "flatten-to-react-intl2",
"version": "0.0.1",
"description": "Helps to transform nested translation into flat translation compatible with react-intl2",
"main": "index.js",
"dependencies": {},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"react-intl2",
"flat",
"json",
"transfrom",
"json"
],
"author": "Omair Qadir <[email protected]> (http://www.inception.io)",
"license": "ISC"
}

0 comments on commit a99634e

Please sign in to comment.