mirror of
https://github.com/OMGeeky/ATCS.git
synced 2025-12-28 07:17:42 +01:00
Level 2 of weblate integration. Rudimentary UI, but working well.
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package com.gpl.rpg.atcontentstudio.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.select.Elements;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.Workspace;
|
||||
import com.gpl.rpg.atcontentstudio.utils.WeblateIntegration.WeblateTranslationUnit.Status;
|
||||
|
||||
public class WeblateIntegration {
|
||||
|
||||
static final String WEBLATE_SIPASH_KEY = "Weblate Sip Hash";
|
||||
|
||||
public static String weblateHash(String str, String ctx) {
|
||||
|
||||
byte[] data = null;
|
||||
|
||||
if (str != null) {
|
||||
byte[] strBytes;
|
||||
try {
|
||||
strBytes = str.getBytes("UTF-8");
|
||||
byte[] ctxBytes = ctx.getBytes("UTF-8");
|
||||
data = new byte[strBytes.length + ctxBytes.length];
|
||||
System.arraycopy(strBytes, 0, data, 0, strBytes.length);
|
||||
System.arraycopy(ctxBytes, 0, data, strBytes.length, ctxBytes.length);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
} else {
|
||||
try {
|
||||
data = ctx.getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return HashUtils.siphash(WEBLATE_SIPASH_KEY, data);
|
||||
}
|
||||
|
||||
public static String getWeblateLabelURI(String text) {
|
||||
return "https://hosted.weblate.org/translate/andors-trail/game-content/"+Workspace.activeWorkspace.settings.translatorLanguage.getCurrentValue()+"/?checksum="+weblateHash(text, "");
|
||||
}
|
||||
|
||||
public static class WeblateTranslationUnit {
|
||||
public enum Status {
|
||||
absent, notTranslated, warning, fuzzy, done
|
||||
}
|
||||
|
||||
public Status status;
|
||||
public String translatedText;
|
||||
}
|
||||
|
||||
public static WeblateTranslationUnit getTranslationUnit(String text) {
|
||||
WeblateTranslationUnit unit = new WeblateTranslationUnit();
|
||||
unit.status = Status.absent;
|
||||
unit.translatedText = "Cannot find this translated on weblate";
|
||||
String hash = weblateHash(text, "");
|
||||
try {
|
||||
Document wlDoc = Jsoup.connect(getWeblateLabelURI(text)).get();
|
||||
Element textArea = wlDoc.getElementById("id_"+hash+"_0");
|
||||
if (textArea != null) {
|
||||
String trans = textArea.text();
|
||||
if (trans != null) {
|
||||
unit.translatedText = trans.trim();
|
||||
if (unit.translatedText.isEmpty()) {
|
||||
unit.translatedText = "Not yet translated";
|
||||
unit.status = Status.notTranslated;
|
||||
} else {
|
||||
unit.status = Status.done;
|
||||
}
|
||||
}
|
||||
Element fuzzyBox = wlDoc.getElementById("id_"+hash+"_fuzzy");
|
||||
if (fuzzyBox != null && fuzzyBox.hasAttr("checked")) {
|
||||
if ("checked".equals(fuzzyBox.attr("checked"))) {
|
||||
unit.status = Status.fuzzy;
|
||||
}
|
||||
} else {
|
||||
Elements dangerZone = wlDoc.getElementsByAttributeValue("class", "panel panel-danger");
|
||||
if (dangerZone != null && !dangerZone.isEmpty()) {
|
||||
unit.status = Status.warning;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user