2017-01-15 10:24:36 +01:00
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
2017-01-15 20:10:12 +01:00
|
|
|
|
import java.text.Normalizer;
|
2017-01-15 10:24:36 +01:00
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
|
|
|
|
|
|
public class LanguageUtils {
|
|
|
|
|
//transliteration map with english equivalent for unsupported chars
|
|
|
|
|
private static Map<Character, String> transliterateMap = new HashMap<Character, String>(){
|
|
|
|
|
{
|
2017-01-15 20:10:12 +01:00
|
|
|
|
//extended ASCII characters
|
2017-01-15 20:46:30 +01:00
|
|
|
|
put('æ', "ae"); put('œ', "oe"); put('ß', "B"); put('ª', "a"); put('º', "o");
|
2017-01-15 20:10:12 +01:00
|
|
|
|
|
2017-01-15 10:24:36 +01:00
|
|
|
|
//russian chars
|
|
|
|
|
put('а', "a"); put('б', "b"); put('в', "v"); put('г', "g"); put('д', "d"); put('е', "e"); put('ё', "jo"); put('ж', "zh");
|
|
|
|
|
put('з', "z"); put('и', "i"); put('й', "jj"); put('к', "k"); put('л', "l"); put('м', "m"); put('н', "n"); put('о', "o");
|
|
|
|
|
put('п', "p"); put('р', "r"); put('с', "s"); put('т', "t"); put('у', "u"); put('ф', "f"); put('х', "kh"); put('ц', "c");
|
|
|
|
|
put('ч', "ch");put('ш', "sh");put('щ', "shh");put('ъ', "\"");put('ы', "y"); put('ь', "'"); put('э', "eh"); put('ю', "ju");
|
|
|
|
|
put('я', "ja");
|
|
|
|
|
|
|
|
|
|
//continue for other languages...
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//check transliterate option status
|
|
|
|
|
public static boolean transliterate()
|
|
|
|
|
{
|
|
|
|
|
return GBApplication.getPrefs().getBoolean("transliteration", true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//replace unsupported symbols to english analog
|
|
|
|
|
public static String transliterate(String txt){
|
|
|
|
|
if (txt == null || txt.isEmpty()) {
|
|
|
|
|
return txt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder message = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
char[] chars = txt.toCharArray();
|
|
|
|
|
|
|
|
|
|
for (char c : chars)
|
|
|
|
|
{
|
|
|
|
|
message.append(transliterate(c));
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-15 20:10:12 +01:00
|
|
|
|
return flattenToAscii(message.toString());
|
2017-01-15 10:24:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//replace unsupported symbol to english analog text
|
|
|
|
|
private static String transliterate(char c){
|
|
|
|
|
char lowerChar = Character.toLowerCase(c);
|
|
|
|
|
|
|
|
|
|
if (transliterateMap.containsKey(lowerChar)) {
|
|
|
|
|
String replace = transliterateMap.get(lowerChar);
|
|
|
|
|
|
|
|
|
|
if (lowerChar != c)
|
|
|
|
|
{
|
|
|
|
|
return replace.toUpperCase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return replace;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.valueOf(c);
|
|
|
|
|
}
|
2017-01-15 20:10:12 +01:00
|
|
|
|
|
|
|
|
|
//convert diacritic
|
|
|
|
|
private static String flattenToAscii(String string) {
|
|
|
|
|
string = Normalizer.normalize(string, Normalizer.Form.NFD);
|
2017-01-15 20:46:30 +01:00
|
|
|
|
return string.replaceAll("\\p{M}", "");
|
2017-01-15 20:10:12 +01:00
|
|
|
|
}
|
2017-01-15 10:24:36 +01:00
|
|
|
|
}
|