From: Oskar Wiksten Date: Thu, 13 Sep 2012 19:10:13 +0000 (+0200) Subject: Updated translation tool to accomodate in-place translation of strings. Major thanks... X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=6d1c5ebe9f68e50a8029fd1292f44b002dd308e6;p=users%2Fmchehab%2Fandors-trail.git Updated translation tool to accomodate in-place translation of strings. Major thanks to surgecurrent for the code! --- diff --git a/AndorsTrailEdit/AndorsTrailTranslations.js b/AndorsTrailEdit/AndorsTrailTranslations.js index 7054941..5107445 100644 --- a/AndorsTrailEdit/AndorsTrailTranslations.js +++ b/AndorsTrailEdit/AndorsTrailTranslations.js @@ -111,50 +111,99 @@ function pushMessage(res, msg) { return res; } -function compareAndorsTrailResourceRow(result, fieldList, id, obj1, obj2) { +function isTranslatableField(fieldName) { + return fieldName == "name" || fieldName == "logText" || fieldName == "message" || fieldName == "text"; +} + +function compareAndorsTrailResourceHeader(result, id, header1, header2) { + if (header1.length != header2.length) { + pushMessage(result, "Row \"" + id + "\" was expected to contain " + f1.length + " sub-entries, but only " + f2.length + " was found."); + return; + } + for (var i = 0; i < header1._fields.length; ++i) { + var f1 = header1._fields[i]; + var fieldName1 = header1.getFieldName(i); + var f2 = header2._fields[i]; + var fieldName2 = header2.getFieldName(i); + if (fieldName1 != fieldName2) { + pushMessage(result, "Row \"" + id + "\", field \"" + fieldName + "\" was expected to contain \"" + f1 + "\", but \"" + f2 + "\" was found."); + } + var fieldName2 = header2.getFieldName(i); + if (f1 instanceof FieldList) { + compareAndorsTrailResourceHeader(result, id+":"+fieldName1, f1, f2); + } + } +} +function extractTranslatableFields_(id, result, fieldList, prefix, obj) { + if (!result) { + result = {}; + result.id = id; + result.fields = []; + } + if (!prefix) { + prefix = ""; + } for (var i = 0; i < fieldList._fields.length; ++i) { var f = fieldList._fields[i]; - var fieldName = fieldList.getFieldName(i); if (f instanceof FieldList) { - fieldName = f._name; - } - var isTranslatableField = (fieldName == "name" || fieldName == "logText" || fieldName == "message" || fieldName == "text"); - var f1 = obj1[fieldName]; - var f2 = obj2[fieldName]; - - if (f instanceof FieldList) { - if (!f2) { f2 = []; } - if (f1.length != f2.length) { - pushMessage(result, "Row \"" + id + "\", field \"" + fieldName + "\" was expected to contain " + f1.length + " sub-entries, but only " + f2.length + " was found."); - continue; - } - $.each(f1, function(i, obj) { - var id_ = id + ":" + obj[f._fields[0]]; - compareAndorsTrailResourceRow(result, f, id_, f1[i], f2[i]); + // f is subfieldlist + $.each(obj[f._name], function(j, elem) { + extractTranslatableFields_(id, result, f, prefix+f._name+"["+j+"].", elem); }); } else { - if (isTranslatableField && f1.length > 1) { - if (f1 == f2) { - pushMessage(result, "Row \"" + id + "\", field \"" + fieldName + "\" does not seem to be translated. Both texts are \"" + f1 + "\"."); - } - } else { - if (f1 != f2) { - pushMessage(result, "Row \"" + id + "\", field \"" + fieldName + "\" was expected to contain \"" + f1 + "\", but \"" + f2 + "\" was found."); - } + // f is field name + if (isTranslatableField(f)) { + result.fields.push({ + "name":prefix+f, + "value":obj[f] + }); } } } + return result; +} +function extractTranslatableFields(id, fieldList, obj) { + return extractTranslatableFields_(id, undefined, fieldList, "", obj); +} + +function compareAndorsTrailResourceRow(result, fieldList, id, obj1, obj2) { + // Assume the headers of both objects are correctly matched + trans1 = extractTranslatableFields(id, fieldList, obj1); + trans2 = extractTranslatableFields(id, fieldList, obj2); + $.each(trans1.fields, function(i, f1) { + f2 = trans2.fields[i]; + if (f1.name != f2.name) { + pushMessage(result, "Row \"" + id + "\", field \"" + f1.name + "\" does not match in translated data field\"" + f2.name + "\"."); + } + if (f1.value.length > 1) { + if (f1.value == f2.value) { + pushMessage(result, "Row \"" + id + "\", field \"" + f1.name + "\" does not seem to be translated. Both texts are \"" + f1.value + "\"."); + } + } + }); } function compareAndorsTrailResourceFormat(text1, text2) { - var result = { isResource: true, class1: "ok", class2: "ok", messages: [] }; + var result = { + isResource: true, + class1: "ok", + class2: "ok", + messages: [], + header: undefined, + ds_english: undefined, + ds_translated: undefined + }; var header1 = findHeader(text1); if (!header1) { return { isResource: false }; } + result.header = header1; var header2 = findHeader(text2); if (!header2) { result.class2 = "red"; return result; } + compareAndorsTrailResourceHeader(result, "", header1, header2); + if (result.class2 != "ok") { return result; } + var ds1 = new DataStore({}); var ds2 = new DataStore({}); ds1.deserialize(text1); @@ -163,42 +212,153 @@ function compareAndorsTrailResourceFormat(text1, text2) { var obj1 = obj; var obj2 = ds2.get(i); var id1 = obj1[header1._fields[0]]; - if (!obj2) { return pushMessage(result, "Row " + i + ": expected to find an object with id \"" + id1 + "\", but such row was found."); } + if (!obj2) { return pushMessage(result, "Row " + i + ": expected to find an object with id \"" + id1 + "\", but such row was not found."); } var id2 = obj2[header1._fields[0]]; if (id2 != id1) { return pushMessage(result, "Row " + i + ": Expected to find id \"" + id1 + "\", but found \"" + id2 + "\" instead."); } compareAndorsTrailResourceRow(result, header1, id1, obj1, obj2); }); + + result.ds_english = ds1; + result.ds_translated = ds2; return result; } +function applyChangeToObject(trans_obj, fieldName, newValue) { + (new Function("x", "v", "x."+fieldName+" = v;"))(trans_obj, newValue); +} + +function appendEditRow(editTable, updateHook, trans_obj, id, fieldName, english_text, translated_text) { + if (english_text.length <= 1) { + // there's no meaningful text + return; + } + var row_id = name+"__row"; + var cell_id = name+"__cell"; + var edit_id = name+"__edit"; + var cell = $("").text(translated_text) + .attr("id", cell_id) + .attr("class", "clickToEdit"); + var editor = $("