+ Added file utils and updated localizer format
This commit is contained in:
+24
-58
@@ -3,17 +3,17 @@ package core;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
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.HashMap;
|
||||
import java.util.stream.Stream;
|
||||
import org.ini4j.InvalidFileFormatException;
|
||||
import org.ini4j.Profile.Section;
|
||||
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
|
||||
// generates/updates a file externally (phrases.config) with all the stub values as keys that
|
||||
// can then be localized.
|
||||
@@ -24,69 +24,34 @@ public class Localizer
|
||||
public final static String functionName = "Localizer.Stub";
|
||||
|
||||
// Local translation
|
||||
private static HashMap<String, LocPair> translated = new HashMap<String, LocPair>();
|
||||
private static HashMap<String, LocInfo> translated = new HashMap<String, LocInfo>();
|
||||
|
||||
// Logging
|
||||
private static void Log(String str) { System.out.println("[Log] " + str); }
|
||||
private static void Warn(String str) { System.out.println("[Warn] " + str); }
|
||||
private static void Error(String str) { System.err.println("[Error] " + str); }
|
||||
private static void Log(String str) { GlobalLog.Log(LogFilter.Strings, str); }
|
||||
private static void Warn(String str) { GlobalLog.Warn(LogFilter.Strings, str); }
|
||||
private static void Error(String str) { GlobalLog.Error(LogFilter.Strings, str); }
|
||||
|
||||
// Util: Reads all lines from a file as a string
|
||||
private 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;
|
||||
}
|
||||
|
||||
public static class LocPair
|
||||
// Structure used for holding a pair of strings and any other info we need
|
||||
// about localized information that is being looked up.
|
||||
private static class LocInfo
|
||||
{
|
||||
public String file;
|
||||
public String phrase;
|
||||
|
||||
public LocPair(String f, String p)
|
||||
public LocInfo(String f, String p)
|
||||
{
|
||||
this.file = f;
|
||||
this.phrase = p;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
if(filename.contains(".java"))
|
||||
{
|
||||
String contents = ReadContent(path);
|
||||
String contents = FileUtils.ReadContent(path);
|
||||
String[] split = contents.split(functionName);
|
||||
|
||||
// Identify all localizer function calls
|
||||
@@ -104,7 +69,7 @@ public class Localizer
|
||||
{
|
||||
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);
|
||||
}
|
||||
@@ -143,6 +108,7 @@ public class Localizer
|
||||
file.createNewFile();
|
||||
|
||||
Wini ini = new Wini(file);
|
||||
|
||||
for(String str : ini.keySet())
|
||||
{
|
||||
// Store everything in all .ini sections
|
||||
@@ -150,7 +116,7 @@ public class Localizer
|
||||
for(String s : sec.keySet())
|
||||
{
|
||||
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())
|
||||
{
|
||||
LocPair toWrite = translated.get(s);
|
||||
LocInfo toWrite = translated.get(s);
|
||||
ini.put(toWrite.file, s, toWrite.phrase);
|
||||
}
|
||||
|
||||
@@ -201,14 +167,14 @@ public class Localizer
|
||||
// This stubs out phrases to be localized.
|
||||
public static void ScrapeAll()
|
||||
{
|
||||
ArrayList<LocPair> localizeList = new ArrayList<LocPair>();
|
||||
AcquireAllFiles(".\\src").forEach((path) -> StripForContents(path, localizeList));
|
||||
ArrayList<LocInfo> localizeList = new ArrayList<LocInfo>();
|
||||
FileUtils.AcquireAllFiles(".\\src").forEach((path) -> StripForContents(path, localizeList));
|
||||
|
||||
for(LocPair toStub : localizeList)
|
||||
for(LocInfo toStub : localizeList)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user