Skip to content

Instantly share code, notes, and snippets.

@radfast
Created July 25, 2014 09:46
Show Gist options
  • Save radfast/93d9437672f8e29c43f3 to your computer and use it in GitHub Desktop.
Save radfast/93d9437672f8e29c43f3 to your computer and use it in GitHub Desktop.
Automatic Language files update syncer for Galacticraft
package langs;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.MalformedInputException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Proc
{
private static String stringBOM = "";
private static File langEnUS;
private static int lastline;
public static void main(String[] args)
{
System.out.println("hi");
doMod("galacticraftcore");
doMod("galacticraftmoon");
doMod("galacticraftmars");
doMod("galacticraftasteroids");
}
public static void doMod (String domain)
{
try
{
File langFolder = new File("/Users/radfast/Galacticraft/src/main/resources/assets/"+domain, "lang");
if (!langFolder.exists())
{
System.out.println("No lang folder found, tried "+langFolder.getAbsolutePath());
return;
}
List<File> languages = Arrays.asList(langFolder.listFiles());
for (File lang:languages)
{
if ("en_US.lang".equals(lang.getName()))
{
langEnUS = lang;
break;
}
}
for (File lang:languages)
{
if ("en_US.lang".equals(lang.getName())) continue;
doFile(lang);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void doFile (File lang)
{
String strLine;
ArrayList<String> langLines = new ArrayList<String>();
try
{
try
{
System.out.println("Processing "+lang.getName());
FileInputStream in = new FileInputStream(lang);
BufferedReader br = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8").newDecoder()));
while((strLine = br.readLine())!= null)
{
langLines.add(strLine);
}
br.close();
}
catch(MalformedInputException ex)
{
System.out.println("Re-reading without UTF-8: "+lang.getName());
BufferedReader br = new BufferedReader(new FileReader(lang));
while((strLine = br.readLine())!= null)
{
langLines.add(strLine);
}
br.close();
}
FileInputStream inUS = new FileInputStream(langEnUS);
BufferedReader brUS = new BufferedReader(new InputStreamReader(inUS));
String prevLine = null;
while((strLine = brUS.readLine())!= null)
{
if (strLine.startsWith(stringBOM)) strLine = strLine.substring(3, strLine.length());
if (strLine.replace("\t", "").replace(" ", "").isEmpty()) continue;
if (strLine.replace("\t", "").replace(" ", "").startsWith("#")) continue;
int i = strLine.indexOf("=");
if (i < 0)
{
System.out.println("---Strange line in en_US: ");
System.out.println(strLine);
continue;
}
String searchLine = strLine.substring(0, i).replace(" ", "");
if (scanFor(langLines, searchLine) > 0)
{
prevLine = new String(searchLine);
continue;
}
//The US key was not found in this lang file
int line = prevLine == null ? 2 : scanFor(langLines, prevLine);
if (line < 0)
{
line = lastline;
}
langLines.add(line, strLine+" ##NEEDS TRANSLATE##");
prevLine = new String(searchLine);
}
brUS.close();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(lang), Charset.forName("UTF-8").newEncoder()));
for (int i = 0; i < langLines.size(); i++)
writer.write(langLines.get(i) + "\n");
writer.close();
}
catch(Exception e){
System.out.println(e);
}
}
public static int scanFor (ArrayList<String> lines, String target)
{
try
{
int line = 0;
for (String strLine : lines)
{
line ++;
if (strLine.startsWith(stringBOM)) strLine = strLine.substring(3, strLine.length());
if (strLine.replace("\t", "").replace(" ", "").isEmpty()) continue;
if (strLine.replace("\t", "").replace(" ", "").startsWith("#")) continue;
int i = strLine.indexOf("=");
if (i < 0)
{
System.out.println("---Strange line in other lang:");
System.out.println(strLine);
break;
}
String strLine2 = strLine.substring(0, i).replace(" ", "");
if (strLine2.equals(target))
{
return line;
}
if (strLine2.equalsIgnoreCase(target))
{
System.out.println("---Case match problem---");
System.out.println(strLine);
}
}
lastline = line;
return -1;
}
catch(Exception e){
System.out.println(e);
return -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment