nodomain.freeyourgadget.gadgetbridge.util.LanguageUtils.java Source code

Java tutorial

Introduction

Here is the source code for nodomain.freeyourgadget.gadgetbridge.util.LanguageUtils.java

Source

/*  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/>. */
package nodomain.freeyourgadget.gadgetbridge.util;

import org.apache.commons.lang3.text.WordUtils;

import java.util.HashMap;
import java.util.Map;
import java.text.Normalizer;
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>(){
    {
        //extended ASCII characters
        put('', "ae"); put('', "oe"); put('', "B"); put('', "a"); put('', "o"); put('',"\""); put('',"\"");

        //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");
            
        //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");
        //continue for other languages...
    }
};

    /**
     * Checks the status of transliteration option
     * @return true if transliterate option is On, and false, if Off or not exist
     */
    public static boolean transliterate() {
        return GBApplication.getPrefs().getBoolean("transliteration", false);
    }

    /**
     * Replaces unsupported symbols to english
     * @param txt input text
     * @return transliterated text
     */
    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));
        }

        return flattenToAscii(message.toString());
    }

    /**
     * Replaces unsupported symbol to english by {@code transliterateMap}
     * @param c input char
     * @return replacement 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 WordUtils.capitalize(replace);
            }

            return replace;
        }

        return String.valueOf(c);
    }

    /**
     * Converts the diacritics
     * @param string input text
     * @return converted text
     */
    private static String flattenToAscii(String string) {
        string = Normalizer.normalize(string, Normalizer.Form.NFD);
        return string.replaceAll("\\p{M}", "");
    }
}