-
Notifications
You must be signed in to change notification settings - Fork 1
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
0 parents
commit a99634e
Showing
2 changed files
with
116 additions
and
0 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,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); |
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,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" | ||
} |