2017-03-10 14:53:19 +01:00
|
|
|
|
/* Copyright (C) 2017 ivanovlev, Yaron Shahrabani
|
|
|
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2017-01-15 10:24:36 +01:00
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
|
|
|
|
|
2017-02-04 07:53:07 +01:00
|
|
|
|
import org.apache.commons.lang3.text.WordUtils;
|
|
|
|
|
|
2017-01-15 10:24:36 +01:00
|
|
|
|
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-17 21:07:12 +01:00
|
|
|
|
put('æ', "ae"); put('œ', "oe"); put('ß', "B"); put('ª', "a"); put('º', "o"); put('«',"\""); put('»',"\"");
|
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");
|
2017-03-04 22:08:24 +01:00
|
|
|
|
|
|
|
|
|
//hebrew chars
|
|
|
|
|
put('א', "a"); put('ב', "b"); put('ג', "g"); put('ד', "d"); put('ה', "h"); put('ו', "u"); put('ז', "z"); put('ח', "kh");
|
|
|
|
|
put('ט', "t"); put('י', "y"); put('כ', "c"); put('ל', "l"); put('מ', "m"); put('נ', "n"); put('ס', "s"); put('ע', "'");
|
|
|
|
|
put('פ', "p"); put('צ', "ts"); put('ק', "k"); put('ר', "r"); put('ש', "sh"); put('ת', "th"); put('ף', "f"); put('ץ', "ts");
|
|
|
|
|
put('ך', "ch");put('ם', "m");put('ן', "n");
|
2017-01-15 10:24:36 +01:00
|
|
|
|
//continue for other languages...
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-01-27 21:16:19 +01:00
|
|
|
|
/**
|
|
|
|
|
* Checks the status of transliteration option
|
|
|
|
|
* @return true if transliterate option is On, and false, if Off or not exist
|
|
|
|
|
*/
|
2017-01-15 10:24:36 +01:00
|
|
|
|
public static boolean transliterate()
|
|
|
|
|
{
|
2017-01-19 06:09:36 +01:00
|
|
|
|
return GBApplication.getPrefs().getBoolean("transliteration", false);
|
2017-01-15 10:24:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 21:16:19 +01:00
|
|
|
|
/**
|
|
|
|
|
* Replaces unsupported symbols to english
|
|
|
|
|
* @param txt input text
|
|
|
|
|
* @return transliterated text
|
|
|
|
|
*/
|
2017-01-15 10:24:36 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 21:16:19 +01:00
|
|
|
|
/**
|
|
|
|
|
* Replaces unsupported symbol to english by {@code transliterateMap}
|
|
|
|
|
* @param c input char
|
|
|
|
|
* @return replacement text
|
|
|
|
|
*/
|
2017-01-15 10:24:36 +01:00
|
|
|
|
private static String transliterate(char c){
|
|
|
|
|
char lowerChar = Character.toLowerCase(c);
|
|
|
|
|
|
|
|
|
|
if (transliterateMap.containsKey(lowerChar)) {
|
|
|
|
|
String replace = transliterateMap.get(lowerChar);
|
|
|
|
|
|
|
|
|
|
if (lowerChar != c)
|
|
|
|
|
{
|
2017-02-04 07:53:07 +01:00
|
|
|
|
return WordUtils.capitalize(replace);
|
2017-01-15 10:24:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return replace;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.valueOf(c);
|
|
|
|
|
}
|
2017-01-15 20:10:12 +01:00
|
|
|
|
|
2017-01-27 21:16:19 +01:00
|
|
|
|
/**
|
|
|
|
|
* Converts the diacritics
|
|
|
|
|
* @param string input text
|
|
|
|
|
* @return converted text
|
|
|
|
|
*/
|
2017-01-15 20:10:12 +01:00
|
|
|
|
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
|
|
|
|
}
|