-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/main/java/com/nuspectra/translation/FixTranslation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.nuspectra.translation; | ||
|
||
public enum FixTranslation { | ||
instance; | ||
|
||
String[] knownMacros = {"%s", "%d", "\n"}; | ||
|
||
public int count(String text, String find) { | ||
int index = 0, count = 0, length = find.length(); | ||
while( (index = text.indexOf(find, index)) != -1 ) { | ||
index += length; count++; | ||
} | ||
return count; | ||
} | ||
|
||
int countMacros(String in) | ||
{ | ||
int c = 0; | ||
for (String m:knownMacros) | ||
{ | ||
c += count(in, m); | ||
} | ||
return c; | ||
} | ||
|
||
boolean checkOutputOK(String translated, String orig) | ||
{ | ||
int oc = countMacros(orig); | ||
int tc = countMacros(translated); | ||
if (oc!=tc) | ||
{ | ||
System.out.println("Macro Error!\n"+orig+"\n"+translated); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
|
||
private String fixPercent(String s, char c) { | ||
String n = "%" + c; | ||
String[] bad = {"% " + c, "% " + Character.toUpperCase(c)}; | ||
for (String b : bad) { | ||
s = s.replace(b, "%s"); | ||
} | ||
// make sure there is one space in front of %s (so not, XYZ%s) | ||
s = s.replace(n, " " + n); | ||
return s; | ||
} | ||
|
||
public String fixLine(String s, String inputString) { | ||
boolean ok = checkOutputOK(s, inputString); | ||
assert(ok); | ||
|
||
s = s.replace("\\ N", "\\n"); | ||
s = s.replace("\\ n", "\\n"); | ||
s = s.replace("\\ t", "\\t"); | ||
s = fixPercent(s, 's'); | ||
s = fixPercent(s, 'd'); | ||
|
||
s = s.replace(" ...", "..."); | ||
s = s.replace(" \\n", "\\n"); | ||
s = s.replace("\\n ", "\\n"); | ||
s = s.replace(" ", " "); | ||
|
||
s = s.replace('\uFF05', '%'); // full width percent, used in .jp. | ||
|
||
s = s.replace(""", "\""); | ||
s = s.replace("'", "'"); | ||
|
||
if (s.contains("&")) | ||
{ | ||
// Unescape failed... ? | ||
System.out.println("& found in output:"+s); | ||
} | ||
|
||
s = s.replace(" .", "."); | ||
|
||
s = s.trim(); | ||
|
||
boolean ok2 = checkOutputOK(s, inputString); | ||
if (!ok2) | ||
{ | ||
System.err.println("Failure with macro counts:"+s+" from "+inputString); | ||
assert(ok2); | ||
} | ||
return s; | ||
} | ||
} |