Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert LocBaml translations to CSV to import from Localization Manager
"use strict";
var _ = require("lodash");
var fs = require("fs");
var csvjson = require("csvjson");
var locales = ["cs", "de", "es"]; // customize based on locales of your translations
translate();
function translate() {
var translations = locales.map(culture => {
var csv = fs.readFileSync(`./Strings.${culture}.csv`).toString(); // LocBaml output file (translations)
var translRaw = csvjson.toObject(csv, { delimiter: ",", quote: '"' });
var transl = translRaw.map(r => {
var ctrl = r.usercontrol.split(':')[1];
var uid = r.uid.split(':')[0];
var key = `${ctrl.replace(".", "_")}_${uid}`; // the key to use, must be unique per RESX file
return { key, value: r.value };
});
return { culture, words: transl };
});
var dictionary = [];
for (var t of translations) {
for (var w of t.words) {
var r = dictionary.find(e => e.key === w.key);
if (!r) {
r = { key: w.key };
dictionary.push(r);
}
r[t.culture] = w.value;
}
}
console.log('Path,Name,"en-US","cs","de","es"');
for (var d of dictionary) {
// replace AssemblyName with the name of your assembly
console.log(`AssemblyName/i18n/Strings,${d.key},,${valueOrEmpty(d.cs)},${valueOrEmpty(d.de)},${valueOrEmpty(d.es)}`);
}
}
function valueOrEmpty(s) {
return _.isUndefined(s) ? "" : `"${s}"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.