Last active
August 29, 2015 14:00
-
-
Save danielleswank/11385335 to your computer and use it in GitHub Desktop.
iOS to Android string converter in JS
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
/* | |
based on https://gist.github.com/florianmski/3442664 | |
and https://github.com/tmurakam/cashflow/blob/0a01ac9e0350dfb04979986444244f8daf4cb5a8/android/convertStrings.rb | |
support for existing directories | |
output by default to res/values-lang/strings.xml | |
usage: node gist.js inputDir(optional) outputDir(optional) | |
ex 1: node gist.js | |
ex 2: node gist.js Localizations res | |
*/ | |
var fs = require('fs'); | |
function makeDirectory (path) { | |
if (fs.existsSync(path)) return; | |
fs.mkdirSync(path, 0766, function(err){ | |
if (err) { | |
console.log(err); | |
} | |
}); | |
} | |
function extractData (string) { | |
var data, regex, name, value; | |
regex = /\"(.*)\"\s*=\s*\"(.*)\"/; | |
data = regex.exec(string); | |
if (!data) return null; | |
name = data[1].replace(/[ .]/, "_"); | |
value = data[2].replace(/&/, "&"); | |
value = value.replace(/</, "<"); | |
return { | |
name: name, | |
value: value | |
}; | |
} | |
function writeFile (input, output) { | |
var file, strings, stream, data; | |
file = fs.readFileSync(input); | |
strings = file.toString().split("\n"); | |
stream = fs.createWriteStream(output + "/strings.xml"); | |
stream.once('open', function(fd) { | |
stream.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); | |
stream.write("<resources>\n"); | |
for (var i = 0; i < strings.length; i++) { | |
data = extractData(strings[i]); | |
if (data) { | |
stream.write("\t<string name=\"" + data.name + "\">" + data.value + "</string>\n"); | |
} | |
} | |
stream.write("</resources>\n"); | |
stream.end(); | |
}); | |
} | |
function iOStoAndroid () { | |
var inputs, outputs, languages; | |
inputs = process.argv[2] || "./"; | |
outputs = process.argv[3] || "res"; | |
languages = fs.readdirSync(inputs); | |
makeDirectory(outputs); | |
languages.forEach(function(lang, index, array) { | |
var input, output; | |
input = inputs + "/" + lang + "/Localizable.strings"; | |
output = outputs + "/values-" + lang.replace(/(.*)\.[^.]+$/, "$1"); | |
if (!fs.existsSync(input)) return; | |
makeDirectory(output); | |
writeFile(input, output); | |
}); | |
} | |
iOStoAndroid(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment