Skip to content

Commit

Permalink
Create FixTranslation.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhlowe committed Jan 19, 2022
1 parent b6aee72 commit 334882c
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/main/java/com/nuspectra/translation/FixTranslation.java
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;
}
}

0 comments on commit 334882c

Please sign in to comment.