LEGO® Universe Newly Imagined
Menu
  • Home
  • Download
    • Musik
  • Foren
  • Info
    • Mitwirkende
    • Welt
    • FAQ
    • Objects
  • Tutorial
    • Kompilieren in Visual Studio
    • Server 0.3
    • Server 0.4 (Pre-2)
    • Server 0.5.1
  • Home
  • Download
    • Musik
  • Foren
  • Info
    • Mitwirkende
    • Welt
    • FAQ
    • Objects
  • Tutorial
    • Kompilieren in Visual Studio
    • Server 0.3
    • Server 0.4 (Pre-2)
    • Server 0.5.1

Script I used to add languages to locale.xml


Home › Foren › General Discussion › Tinkerer’s Corner › Script I used to add languages to locale.xml

  • Dieses Thema hat 0 Antworten sowie 1 Teilnehmer und wurde zuletzt vor vor 8 Jahren, 4 Monaten von Timtech aktualisiert.
Ansicht von 1 Beitrag (von insgesamt 1)
  • Autor
    Beiträge
  • Dezember 19, 2016 um 8:16 pm Uhr #7168
    Timtech
    Administrator

    This is the script I used to connect to the Google Translate API and add languages to locale.xml. This is mostly for historical interest as it is very crude and doesn’t properly translate everything, like font and variable fields. It was also coded sloppily (which I suppose is obvious).

    Don’t forget to replace APIKey with your key if you actually want to use this. Properly escaped locale.xml path should be set in fXmlFile and you edit espanol.setAttribute(„locale“, „fr_FR“) to change the language. (Confusing, right? I started off just translating to Spanish so that’s why).

    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import org.json.JSONException;
    import org.json.JSONObject;
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Node;
    import org.w3c.dom.Element;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.net.URL;
    import java.nio.charset.Charset;
    
    public class Main {
    	String toTranslate, APIKey = "replaceThisWithYourApiKey";
    	int length = 0;
    	public static String encodeURIcomponent(String s) {
    		StringBuilder o = new StringBuilder();
    		for (char ch : s.toCharArray()) {
    			if (!isUnsafe(ch)) {
    				if (ch==' '||ch=='%') {
    					o.append('%');
    					o.append(toHex(ch / 16));
    					o.append(toHex(ch % 16));
    				}
    				else o.append(ch);
    			}
    		}
    		return o.toString();
    	}
    
    	private static char toHex(int ch) {
    		return (char)(ch < 10 ? '0' + ch : 'A' + ch - 10);
    	}
    
    	private static boolean isUnsafe(char ch) {
    		if (ch > 128 || ch < 0)
    			return true;
    		return false;
    	}
    
    	public String Translate (String EnglishText, String TargetLanguage) throws IOException, JSONException {
    		JSONObject json = readJsonFromUrl(encodeURIcomponent("https://www.googleapis.com/language/translate/v2?key="+APIKey+"&source=en&target="+TargetLanguage+"&q="+EnglishText.replace("\n", " ").replace("\r", " ")));
    		return json.getJSONObject("data").getJSONArray("translations").getJSONObject(0).get("translatedText").toString();
    	}
    	private static String readAll(Reader rd) throws IOException {
    		StringBuilder sb = new StringBuilder();
    		int cp;
    		while ((cp = rd.read()) != -1) {
    			sb.append((char) cp);
    		}
    		return sb.toString();
    	}
    
    	public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    		InputStream is = new URL(url).openStream();
    		try {
    			BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
    			String jsonText = readAll(rd);
    			JSONObject json = new JSONObject(jsonText);
    			return json;
    		} finally {
    			is.close();
    		}
    	}
    
    	public static void main(String argv[]) {
    		Main main = new Main();
    		main.MainConstructor();
    	}
    	public void MainConstructor() {
    		int i=9000;
    		while (i < 24708) {
    		try {
    			File fXmlFile = new File("C:\\LEGO\\locale.xml");
    			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    			Document doc = dBuilder.parse(fXmlFile);
    
    			doc.getDocumentElement().normalize();
    
    			System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    
    			NodeList nList = doc.getElementsByTagName("phrase");
    
    			System.out.println("----------------------------");
    
    			for (i = 0; i < nList.getLength() /*24708*/; i++) {
    
    				try {
    				    Thread.sleep(100);
    				} catch(InterruptedException ex) {
    				    Thread.currentThread().interrupt();
    				}
    				
    				Node nNode = nList.item(i);
    
    				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
    
    					Element eElement = (Element) nNode;
    					System.out.println("ID:                  " + eElement.getAttribute("id"));
    					/* Translate */
    					Element espanol = doc.createElement("translation");
    					espanol.setAttribute("locale", "cy_GB");
    					/* End translation */
    					for (int j=0;j<eElement.getElementsByTagName("translation").getLength();j++) {
    						if (eElement.getElementsByTagName("translation").getLength() == 2)
    							System.out.println(eElement.getAttribute("id"));
    						Element translation = (Element) eElement.getElementsByTagName("translation").item(j);
    						if (translation.getAttribute("locale").equals("en_US")) {
    							System.out.println("English translation: " + translation.getTextContent());
    							length += translation.getTextContent().length();
    							eElement.appendChild(espanol);
    							toTranslate = translation.getTextContent();
    						}
    						if (translation.getAttribute("locale").equals("de_DE"))
    							System.out.println("German translation:  " + translation.getTextContent());
    						if (translation.getAttribute("locale").equals("en_GB"))
    							System.out.println("British translation: " + translation.getTextContent());
    					}
    				}
    			}
    			// writing xml file
    			TransformerFactory transformerFactory = TransformerFactory.newInstance();
    			Transformer transformer = transformerFactory.newTransformer();
    			DOMSource source = new DOMSource(doc);
    			File outputFile = new File("C:\\LEGO\\locale.xml");
    			StreamResult result = new StreamResult(outputFile);
    			// creating output stream
    			transformer.transform(source, result);
    			System.out.println(length);
    			System.exit(0);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}}
    }
    • Dieses Thema wurde geändert vor 8 Jahren, 4 Monaten von Timtech. Grund: had to change lt and gt to their HTML counterparts
  • Autor
    Beiträge
Ansicht von 1 Beitrag (von insgesamt 1)
  • Du musst angemeldet sein, um auf dieses Thema antworten zu können.
Anmelden

Comments are currently closed.

Bevorzugte Sprache


  • English (en) English (en)
  • Deutsch (de) Deutsch (de)
  • Español (es) Español (es)

Die Foren durchsuchen


Neueste Themen


  • Happy New Year
  • FORUMS CLOSED

Foren

  • General Discussion

Andere Seiten

  • LU Server Projects Github online
  • Community Discord online
  • Suche im LUNIversum

    Bitte zur Kenntnis nehmen!

    Die LEGO Group hat den Betrieb dieses Spiels nicht offiziell genehmigt und übernimmt in keiner Weise Verantwortung für mögliche Sicherheitsprobleme im Zusammenhang mit diesem Spiel.

    Urheberrecht © 2015-2021 LUNI™ Server Projekt

    Gehostet von TimTech Software