Translations refactoring
This commit is contained in:
parent
f9a017cc9f
commit
74799b08a6
1 changed files with 40 additions and 59 deletions
|
@ -15,7 +15,6 @@ class PropInfo {
|
|||
}
|
||||
}
|
||||
|
||||
function onlineTranslation() {
|
||||
var pageTranslations = new Map();
|
||||
var overridenTranslations = new Map();
|
||||
var startSep = '\u25a4';
|
||||
|
@ -23,8 +22,6 @@ function onlineTranslation() {
|
|||
var intSep = '\u25a6';
|
||||
var resultSep = '\u200b\uFEFF\u200b\uFEFF\u200b';
|
||||
var resultSepChars = '\u200b\uFEFF';
|
||||
translationsParsing();
|
||||
createTranslationsInterface();
|
||||
|
||||
function saveTranslations() {
|
||||
var storage = window.localStorage;
|
||||
|
@ -40,16 +37,16 @@ function onlineTranslation() {
|
|||
}
|
||||
}
|
||||
|
||||
function createTranslationsInterface() {
|
||||
function createTranslationPanel() {
|
||||
var devPanel = document.getElementById("developerPanel");
|
||||
if (devPanel !== null) {
|
||||
var container = document.createElement("div");
|
||||
container.setAttribute("id", "translationsContainer");
|
||||
container.setAttribute("id", "translationPanel");
|
||||
container.setAttribute("style", "font-size:0.8em !important;width: 440px; resize: horizontal; \
|
||||
overflow: auto; padding: 10px; position: absolute;background-color:#f7dd8a;border:1px dotted;z-index:10000");
|
||||
devPanel.parentNode.insertBefore(container, devPanel.nextSibling);
|
||||
createTranslationControls(container);
|
||||
createTableFromPageTranslations(container);
|
||||
createPageTranslationsTable(container);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,32 +58,31 @@ function onlineTranslation() {
|
|||
|
||||
var cleanButton = document.createElement("button");
|
||||
cleanButton.textContent = "Clean All";
|
||||
cleanButton.setAttribute("onclick", "cleanTranslations()");
|
||||
cleanButton.setAttribute("onclick", "cleanTranslationStorage()");
|
||||
cleanButton.setAttribute("style", "margin-right:10px;");
|
||||
|
||||
controls.appendChild(cleanButton);
|
||||
|
||||
var exportAllButton = document.createElement("button");
|
||||
exportAllButton.textContent = "Export All";
|
||||
exportAllButton.setAttribute("onclick", "exportAll()");
|
||||
exportAllButton.setAttribute("onclick", "exportTranslations()");
|
||||
exportAllButton.setAttribute("style", "margin-right:10px;");
|
||||
controls.appendChild(exportAllButton);
|
||||
|
||||
var exportFileInput = document.createElement("input");
|
||||
var exportFileButton = document.createElement("button");
|
||||
exportFileButton.setAttribute("style", "margin-right:10px;");
|
||||
exportFileInput.type = "file";
|
||||
exportFileInput.setAttribute("id", "exportFile");
|
||||
exportFileInput.setAttribute("style", "display:none;");
|
||||
exportFileInput.setAttribute("accept", ".properties");
|
||||
var exportFileLabel = document.createElement("label");
|
||||
exportFileLabel.setAttribute("for", "exportFile");
|
||||
exportFileLabel.textContent = "Update file";
|
||||
exportFileLabel.setAttribute("style", "margin:0px;color:black;")
|
||||
exportFileButton.appendChild(exportFileLabel);
|
||||
controls.appendChild(exportFileButton);
|
||||
controls.appendChild(exportFileInput);
|
||||
exportFileInput.addEventListener("change", onExportFileUpload);
|
||||
var updateFileInput = document.createElement("input");
|
||||
var updateFileButton = document.createElement("button");
|
||||
updateFileButton.setAttribute("style", "margin-right:10px;");
|
||||
updateFileInput.type = "file";
|
||||
updateFileInput.setAttribute("id", "exportFile");
|
||||
updateFileInput.setAttribute("style", "display:none;");
|
||||
updateFileInput.setAttribute("accept", ".properties");
|
||||
var updateFileLabel = document.createElement("label");
|
||||
updateFileLabel.setAttribute("for", "exportFile");
|
||||
updateFileLabel.textContent = "Update file";
|
||||
updateFileLabel.setAttribute("style", "margin:0px;color:black;")
|
||||
updateFileButton.appendChild(updateFileLabel);
|
||||
controls.appendChild(updateFileButton);
|
||||
controls.appendChild(updateFileInput);
|
||||
updateFileInput.addEventListener("change", updateTranslationsFile);
|
||||
|
||||
var importFileInput = document.createElement("input");
|
||||
var importFileButton = document.createElement("button");
|
||||
|
@ -101,16 +97,16 @@ function onlineTranslation() {
|
|||
importFileButton.appendChild(importFileLabel);
|
||||
controls.appendChild(importFileButton);
|
||||
controls.appendChild(importFileInput);
|
||||
importFileInput.addEventListener("change", onImportFileUpload);
|
||||
importFileInput.addEventListener("change", importTranslationsFromFile);
|
||||
}
|
||||
|
||||
function cleanTranslations() {
|
||||
function cleanTranslationStorage() {
|
||||
overridenTranslations.clear();
|
||||
saveTranslations();
|
||||
location.reload();
|
||||
}
|
||||
|
||||
function onImportFileUpload(e) {
|
||||
function importTranslationsFromFile(e) {
|
||||
const fileList = e.target.files;
|
||||
const numFiles = fileList.length;
|
||||
if (numFiles > 0) {
|
||||
|
@ -143,7 +139,7 @@ function onlineTranslation() {
|
|||
}
|
||||
}
|
||||
|
||||
function onExportFileUpload(e) {
|
||||
function updateTranslationsFile(e) {
|
||||
const fileList = e.target.files;
|
||||
const numFiles = fileList.length;
|
||||
if (numFiles > 0) {
|
||||
|
@ -179,23 +175,23 @@ function onlineTranslation() {
|
|||
}
|
||||
}
|
||||
}
|
||||
exportFile(fileName, lines);
|
||||
saveFile(fileName, lines);
|
||||
}
|
||||
reader.readAsText(file);
|
||||
}
|
||||
}
|
||||
|
||||
function exportAll() {
|
||||
function exportTranslations() {
|
||||
var date = new Date;
|
||||
var fileName = "export_" + date.toLocaleString() + "_all.properties";
|
||||
var lines = [];
|
||||
for (let [key, value] of overridenTranslations) {
|
||||
lines.push(key + " = " + value);
|
||||
}
|
||||
exportFile(fileName, lines);
|
||||
saveFile(fileName, lines);
|
||||
}
|
||||
|
||||
function exportFile(fileName, lines) {
|
||||
function saveFile(fileName, lines) {
|
||||
var blob = new Blob([lines.join("\n")], { type: 'text/plain;charset=utf-8' });
|
||||
saveAs(blob, fileName);
|
||||
}
|
||||
|
@ -225,12 +221,12 @@ function onlineTranslation() {
|
|||
return line.match(/^\s*[#!]/) != null;
|
||||
}
|
||||
|
||||
function createTableFromPageTranslations(container) {
|
||||
function createPageTranslationsTable(container) {
|
||||
var table = document.createElement("table");
|
||||
table.setAttribute("id", "translationsTable");
|
||||
table.setAttribute("style", "width:100%;");
|
||||
|
||||
document.getElementById("translationsContainer").appendChild(table);
|
||||
document.getElementById("translationPanel").appendChild(table);
|
||||
for (let [key, propInfo] of pageTranslations) {
|
||||
var tr = document.createElement("tr");
|
||||
table.appendChild(tr);
|
||||
|
@ -255,12 +251,12 @@ function onlineTranslation() {
|
|||
td2.appendChild(rawTextHidden);
|
||||
tr.appendChild(td2);
|
||||
rawText.addEventListener("blur", function() {
|
||||
onTranslationChange(this);
|
||||
updateTranslation(this);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function onTranslationChange(input) {
|
||||
function updateTranslation(input) {
|
||||
if (input.value != input.nextSibling.value) {
|
||||
var key = input.parentElement.previousSibling.firstChild.textContent;
|
||||
if (input.value == "") {
|
||||
|
@ -325,7 +321,7 @@ function onlineTranslation() {
|
|||
return value;
|
||||
}
|
||||
|
||||
function translationsParsing() {
|
||||
function parseHTMLTranslations() {
|
||||
var translatedTexts = [];
|
||||
var translatedAttrs = [];
|
||||
var xpath = "//attribute::*[contains(., '" + startSep + "')]";
|
||||
|
@ -334,13 +330,13 @@ function onlineTranslation() {
|
|||
var node = null;
|
||||
while (node = result.iterateNext()) {
|
||||
translatedAttrs.push(node);
|
||||
addPropsFromAttribute(node);
|
||||
parsePropsInNode(node);
|
||||
}
|
||||
xpath = "//*[text()[contains(.,'" + startSep + "')]]";
|
||||
result = document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null);
|
||||
while (node = result.iterateNext()) {
|
||||
translatedTexts.push(node);
|
||||
addPropsFromTextNode(node);
|
||||
parsePropsInNode(node);
|
||||
}
|
||||
readTranslations();
|
||||
removePropInfoFromPage();
|
||||
|
@ -408,42 +404,28 @@ function onlineTranslation() {
|
|||
}
|
||||
}
|
||||
|
||||
function addPropsFromTextNode(node) {
|
||||
function parsePropsInNode(node) {
|
||||
var textString = node.textContent;
|
||||
var i = 0;
|
||||
while (textString.indexOf(startSep) >= 0) {
|
||||
textString = textString.substring(textString.indexOf(startSep) + startSep.length);
|
||||
var prop = textString.substring(0, textString.indexOf(endSep));
|
||||
var address = new PropAddr(node, i, []);
|
||||
addProp(prop, address);
|
||||
addToPageTranslations(prop, address);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
function addPropsFromAttribute(node) {
|
||||
var attrString = node.textContent;
|
||||
var i = 0;
|
||||
while (attrString.indexOf(startSep) >= 0) {
|
||||
attrString = attrString.substring(attrString.indexOf(startSep) + startSep.length);
|
||||
var prop = attrString.substring(0, attrString.indexOf(endSep));
|
||||
var address = new PropAddr(node, i, []);
|
||||
addProp(prop, address);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
function addProp(prop, address) {
|
||||
function addToPageTranslations(prop, address) {
|
||||
var key = prop.substring(0, prop.indexOf(intSep));
|
||||
prop = prop.substring(prop.indexOf(intSep) + intSep.length);
|
||||
var rawText = prop.substring(0, prop.indexOf(intSep));
|
||||
prop = prop.substring(prop.indexOf(intSep) + intSep.length);
|
||||
var textArgs = [];
|
||||
var i = 0;
|
||||
while (prop.indexOf(intSep) >= 0) {
|
||||
var textArg = prop.substring(0, prop.indexOf(intSep));
|
||||
prop = prop.substring(prop.indexOf(intSep) + intSep.length);
|
||||
textArgs.push(textArg);
|
||||
i++;
|
||||
}
|
||||
address.args = textArgs;
|
||||
var formText = prop;
|
||||
|
@ -455,15 +437,14 @@ function onlineTranslation() {
|
|||
propInfo = new PropInfo(rawText, formText, address);
|
||||
pageTranslations.set(key, propInfo);
|
||||
}
|
||||
return propInfo;
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('load', function() {
|
||||
setTimeout(function() {
|
||||
var developerSetting = document.getElementById("developer_i18n_onlineTranslation");
|
||||
if (developerSetting !== null && developerSetting.checked) {
|
||||
onlineTranslation();
|
||||
parseHTMLTranslations();
|
||||
createTranslationPanel();
|
||||
}
|
||||
}, 1000);
|
||||
})
|
Loading…
Add table
Reference in a new issue