+ Added file utils and updated localizer format
This commit is contained in:
+22
-56
@@ -3,17 +3,17 @@ package core;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.stream.Stream;
|
|
||||||
import org.ini4j.InvalidFileFormatException;
|
import org.ini4j.InvalidFileFormatException;
|
||||||
import org.ini4j.Profile.Section;
|
import org.ini4j.Profile.Section;
|
||||||
import org.ini4j.Wini;
|
import org.ini4j.Wini;
|
||||||
|
|
||||||
|
import utils.FileUtils;
|
||||||
|
import utils.GlobalLog;
|
||||||
|
import utils.LogFilter;
|
||||||
|
|
||||||
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
|
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
|
||||||
// generates/updates a file externally (phrases.config) with all the stub values as keys that
|
// generates/updates a file externally (phrases.config) with all the stub values as keys that
|
||||||
// can then be localized.
|
// can then be localized.
|
||||||
@@ -24,56 +24,21 @@ public class Localizer
|
|||||||
public final static String functionName = "Localizer.Stub";
|
public final static String functionName = "Localizer.Stub";
|
||||||
|
|
||||||
// Local translation
|
// Local translation
|
||||||
private static HashMap<String, LocPair> translated = new HashMap<String, LocPair>();
|
private static HashMap<String, LocInfo> translated = new HashMap<String, LocInfo>();
|
||||||
|
|
||||||
// Logging
|
// Logging
|
||||||
private static void Log(String str) { System.out.println("[Log] " + str); }
|
private static void Log(String str) { GlobalLog.Log(LogFilter.Strings, str); }
|
||||||
private static void Warn(String str) { System.out.println("[Warn] " + str); }
|
private static void Warn(String str) { GlobalLog.Warn(LogFilter.Strings, str); }
|
||||||
private static void Error(String str) { System.err.println("[Error] " + str); }
|
private static void Error(String str) { GlobalLog.Error(LogFilter.Strings, str); }
|
||||||
|
|
||||||
// Util: Reads all lines from a file as a string
|
// Structure used for holding a pair of strings and any other info we need
|
||||||
private static String ReadContent(Path filePath)
|
// about localized information that is being looked up.
|
||||||
{
|
private static class LocInfo
|
||||||
StringBuilder contentBuilder = new StringBuilder();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Stream<String> stream = Files.lines(filePath, StandardCharsets.UTF_8);
|
|
||||||
|
|
||||||
stream.forEach(str -> contentBuilder.append(str).append("\n"));
|
|
||||||
stream.close();
|
|
||||||
}
|
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return contentBuilder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recursively acquires all files at and below the specified directory, returning them as an arraylist of paths.
|
|
||||||
public static ArrayList<Path> AcquireAllFiles(String startingDir)
|
|
||||||
{
|
|
||||||
ArrayList<Path> items = new ArrayList<Path>();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Files.find(Paths.get(startingDir), 999, (path, attributes) -> attributes.isRegularFile()).forEach(items::add);
|
|
||||||
}
|
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return items;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class LocPair
|
|
||||||
{
|
{
|
||||||
public String file;
|
public String file;
|
||||||
public String phrase;
|
public String phrase;
|
||||||
|
|
||||||
public LocPair(String f, String p)
|
public LocInfo(String f, String p)
|
||||||
{
|
{
|
||||||
this.file = f;
|
this.file = f;
|
||||||
this.phrase = p;
|
this.phrase = p;
|
||||||
@@ -81,12 +46,12 @@ public class Localizer
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Do processing on each path in the scraped directory here, assuming it's .java
|
// Do processing on each path in the scraped directory here, assuming it's .java
|
||||||
public static void StripForContents(Path path, ArrayList<LocPair> strings)
|
public static void StripForContents(Path path, ArrayList<LocInfo> strings)
|
||||||
{
|
{
|
||||||
String filename = path.getFileName().toString();
|
String filename = path.getFileName().toString();
|
||||||
if(filename.contains(".java"))
|
if(filename.contains(".java"))
|
||||||
{
|
{
|
||||||
String contents = ReadContent(path);
|
String contents = FileUtils.ReadContent(path);
|
||||||
String[] split = contents.split(functionName);
|
String[] split = contents.split(functionName);
|
||||||
|
|
||||||
// Identify all localizer function calls
|
// Identify all localizer function calls
|
||||||
@@ -104,7 +69,7 @@ public class Localizer
|
|||||||
{
|
{
|
||||||
String toLocalize = split[i].substring(2, loc - 1);
|
String toLocalize = split[i].substring(2, loc - 1);
|
||||||
|
|
||||||
strings.add(new LocPair(filename.substring(0, filename.lastIndexOf('.')), toLocalize));
|
strings.add(new LocInfo(filename.substring(0, filename.lastIndexOf('.')), toLocalize));
|
||||||
|
|
||||||
Log("Found stubbed phrase in " + path + " : " + toLocalize);
|
Log("Found stubbed phrase in " + path + " : " + toLocalize);
|
||||||
}
|
}
|
||||||
@@ -143,6 +108,7 @@ public class Localizer
|
|||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
|
|
||||||
Wini ini = new Wini(file);
|
Wini ini = new Wini(file);
|
||||||
|
|
||||||
for(String str : ini.keySet())
|
for(String str : ini.keySet())
|
||||||
{
|
{
|
||||||
// Store everything in all .ini sections
|
// Store everything in all .ini sections
|
||||||
@@ -150,7 +116,7 @@ public class Localizer
|
|||||||
for(String s : sec.keySet())
|
for(String s : sec.keySet())
|
||||||
{
|
{
|
||||||
if(!translated.containsKey(s))
|
if(!translated.containsKey(s))
|
||||||
translated.put(s, new LocPair(sec.getName(), sec.get(s, String.class)));
|
translated.put(s, new LocInfo(sec.getName(), sec.get(s, String.class)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,7 +147,7 @@ public class Localizer
|
|||||||
|
|
||||||
for(String s : translated.keySet())
|
for(String s : translated.keySet())
|
||||||
{
|
{
|
||||||
LocPair toWrite = translated.get(s);
|
LocInfo toWrite = translated.get(s);
|
||||||
ini.put(toWrite.file, s, toWrite.phrase);
|
ini.put(toWrite.file, s, toWrite.phrase);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,14 +167,14 @@ public class Localizer
|
|||||||
// This stubs out phrases to be localized.
|
// This stubs out phrases to be localized.
|
||||||
public static void ScrapeAll()
|
public static void ScrapeAll()
|
||||||
{
|
{
|
||||||
ArrayList<LocPair> localizeList = new ArrayList<LocPair>();
|
ArrayList<LocInfo> localizeList = new ArrayList<LocInfo>();
|
||||||
AcquireAllFiles(".\\src").forEach((path) -> StripForContents(path, localizeList));
|
FileUtils.AcquireAllFiles(".\\src").forEach((path) -> StripForContents(path, localizeList));
|
||||||
|
|
||||||
for(LocPair toStub : localizeList)
|
for(LocInfo toStub : localizeList)
|
||||||
{
|
{
|
||||||
if(!translated.containsKey(toStub.phrase))
|
if(!translated.containsKey(toStub.phrase))
|
||||||
{
|
{
|
||||||
translated.put(toStub.phrase, new LocPair(toStub.file, ""));
|
translated.put(toStub.phrase, new LocInfo(toStub.file, ""));
|
||||||
Log("Found new stubbed phrase '" + toStub.phrase + "' in " + toStub.file);
|
Log("Found new stubbed phrase '" + toStub.phrase + "' in " + toStub.file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package utils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class FileUtils
|
||||||
|
{
|
||||||
|
// Reads all lines from a file as a string
|
||||||
|
public static String ReadContent(Path filePath)
|
||||||
|
{
|
||||||
|
StringBuilder contentBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Stream<String> stream = Files.lines(filePath, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
stream.forEach(str -> contentBuilder.append(str).append("\n"));
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return contentBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursively acquires all files at and below the specified directory, returning them as an arraylist of paths.
|
||||||
|
public static ArrayList<Path> AcquireAllFiles(String startingDir)
|
||||||
|
{
|
||||||
|
ArrayList<Path> items = new ArrayList<Path>();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Files.find(Paths.get(startingDir), 999, (path, attributes) -> attributes.isRegularFile()).forEach(items::add);
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ package utils;
|
|||||||
|
|
||||||
public enum LogFilter
|
public enum LogFilter
|
||||||
{
|
{
|
||||||
Debug(0), Command(1), Core(2), Database(4), Response(8), Network(16); //8, 16, 32... (flags, so we can | together later)
|
Debug(0), Command(1), Core(2), Database(4), Response(8), Network(16), Strings(32); //8, 16, 32... (flags, so we can | together later)
|
||||||
|
|
||||||
private final int value;
|
private final int value;
|
||||||
private LogFilter(int value)
|
private LogFilter(int value)
|
||||||
|
|||||||
Reference in New Issue
Block a user