Skip to content

Instantly share code, notes, and snippets.

@wiedymi
Last active October 3, 2019 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wiedymi/9bc2a6fab9b8c5206a7ea758759fa13f to your computer and use it in GitHub Desktop.
Save wiedymi/9bc2a6fab9b8c5206a7ea758759fa13f to your computer and use it in GitHub Desktop.
A difference android locale checker
const fs = require("fs");
const convert = require("xml-js");
const getFile = name => {
if (name === "en") {
return `${process.cwd()}/app/src/main/res/values/strings.xml`;
}
return `${process.cwd()}/app/src/main/res/values-${name}/strings.xml`;
};
const be = fs.readFileSync(getFile("be"), "utf-8");
const en = fs.readFileSync(getFile("en"), "utf-8");
const beRAW = convert.xml2json(be, { compact: true, spaces: 4 });
const enRAW = convert.xml2json(en, { compact: true, spaces: 4 });
const beJSON = JSON.parse(beRAW).resources.string;
const enJSON = JSON.parse(enRAW).resources.string;
const difference = (be, en) => {
const mapper = value => {
return value._attributes.name;
};
const d1 = be.map(mapper);
const d2 = en.map(mapper);
const filter = d2.filter(value => {
if (!d1.includes(value)) {
return value;
}
});
return filter;
};
const addFifferencesToFile = (fileName, differences) => {
const filePath = getFile(fileName);
const be = fs.readFileSync(filePath, "utf-8");
const en = fs.readFileSync(getFile("en"), "utf-8");
const enRAW = convert.xml2json(en, { spaces: 4 });
const enJSON = JSON.parse(enRAW);
const elements = enJSON.elements[0].elements
.map((el, i) => {
if (differences.includes(el.attributes.name)) {
return el;
}
return;
})
.filter(el => el != null);
const result = convert.json2xml({
...enJSON,
elements
});
return result
.replace('<?xml version="1.0" encoding="utf-8"?>', "")
.replace("</string>", "</string>\n")
.replace("<string>", "\xa0\xa0\xa0\xa0<string>");
};
const differences = difference(beJSON, enJSON);
console.log(addFifferencesToFile("be", differences));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment