Skip to content

Instantly share code, notes, and snippets.

@wilbefast
Last active March 31, 2018 19:00
Show Gist options
  • Save wilbefast/52481b817d8f849c3bcdb554c1feb304 to your computer and use it in GitHub Desktop.
Save wilbefast/52481b817d8f849c3bcdb554c1feb304 to your computer and use it in GitHub Desktop.
ExportLocalisation.gs
// This assumes that your sheet looks something like this (in CSV format)
// key, en-gb, en-us, fr-fr
// title, Colour Wars, Color War, Guerres de Coleur
// press_start, Press start, Press start, appuyez sur start
// ... etc
// The script will create a bunch of JSON files at the root of your drive, one for each locale
function exportLocalisation() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var row_count = rows.getNumRows();
var row_values = rows.getValues();
// Header rows
var locales = {}
var col_locales = {};
var headers = row_values[0];
for (var col = 1; col < headers.length; col++) {
var header = headers[col];
col_locales[col] = header;
locales[header] = {};
}
// Keys
for (var row = 1; row < row_count; row++) {
var row_columns = row_values[row];
var key = row_columns[0];
for(var col = 1; col < row_columns.length; col++) {
var value = row_columns[col];
var locale = locales[col_locales[col]];
locale[key] = value;
}
}
// Export
var suffix = "_" + new Date() + ".json";
for (var l in locales) {
var export_filename = l + suffix;
var export_data = JSON.stringify(locales[l]);
Logger.log("Creating file '" + export_filename + "'");
DriveApp.createFile(export_filename, export_data);
}
};
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Export localisation",
functionName : "exportLocalisation"
}];
sheet.addMenu("Custom", entries);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment