Example usage for java.util Locale getLanguage

List of usage examples for java.util Locale getLanguage

Introduction

In this page you can find the example usage for java.util Locale getLanguage.

Prototype

public String getLanguage() 

Source Link

Document

Returns the language code of this Locale.

Usage

From source file:eu.trentorise.opendata.commons.TodUtils.java

/**
 * Converts a Java locale to a String. On null input returns the empty
 * string (which corresponds to Java {@link Locale#ROOT})
 *
 * @see #languageTagToLocale(java.lang.String) #languageTagToLocale(java.lang.String) for the inverse operation
 *//*from ww  w.  j a  v  a 2  s .c  o m*/
public static String localeToLanguageTag(@Nullable Locale locale) {
    if (locale == null) {
        LOG.warning("Found null locale, returning empty string (which corresponds to Locale.ROOT)");
        return "";
    }
    return locale.getLanguage();
}

From source file:com.microsoft.alm.plugin.context.soap.CatalogServiceImpl.java

private static String localeToRFC5646LanguageTag(final Locale locale) throws IllegalArgumentException {

    // language[-variant][-region]

    String result = locale.getLanguage();

    if (locale.getVariant().length() > 0) {
        result = result + "-" + locale.getVariant(); //$NON-NLS-1$
    }//from   ww w .ja v  a  2s.c  om

    if (locale.getCountry().length() > 0) {
        result = result + "-" + locale.getCountry(); //$NON-NLS-1$
    }

    return result;
}

From source file:com.lm.lic.manager.util.GenUtil.java

/**
 * @param request//  w ww  .j av  a2 s .c  o m
 * @return
 */
public static String extractLocaleLang(HttpServletRequest request) {
    Locale locale = request.getLocale();
    String lang = locale.getLanguage();
    return lang;
}

From source file:Dates.java

public static String dateFormatForJSCalendar(Locale locale) {
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    String date = df.format(create(1, 2, 1971)); // d, m, y
    boolean always4InYear = "es".equals(locale.getLanguage()) || "pl".equals(locale.getLanguage());
    String result = date.replaceAll("01", "%d").replaceAll("02", "%m").replaceAll("1971", "%Y")
            .replaceAll("71", always4InYear ? "%Y" : "%y").replaceAll("1", "%d").replaceAll("2", "%m");
    return result;
}

From source file:com.examples.with.different.packagename.concolic.MathRuntimeException.java

/**
 * Translate a string to a given locale.
 * @param s string to translate/*  ww w.  java 2s .com*/
 * @param locale locale into which to translate the string
 * @return translated string or original string
 * for unsupported locales or unknown strings
 */
private static String translate(final String s, final Locale locale) {
    try {
        ResourceBundle bundle = ResourceBundle.getBundle("org.apache.commons.math.MessagesResources", locale);
        if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
            // the value of the resource is the translated string
            return bundle.getString(s);
        }

    } catch (MissingResourceException mre) {
        // do nothing here
    }

    // the locale is not supported or the resource is unknown
    // don't translate and fall back to using the string as is
    return s;

}

From source file:it.cnr.icar.eric.client.xml.registry.infomodel.InternationalStringImpl.java

/**
 * Calculate the possible keys List for a locale/charset pair. Candidate keys
 * are:/*from w ww.  ja v a2  s  .c  om*/
 * <ul>
 * <li> charset + "_" + language + "_" + country + "_" + variant
 * <li> charset + "_" + language + "_" + countryi
 * <li> charset + "_" + language
 * </ul>
 *
 * Candidate keys where the final component is an empty string are omitted.
 * For example, if country is an empty string, the second candidate key is omitted.
 *
 * @param charset the charset name
 * @param locale the locale
 * @return List with the keys along the search path.
 */
private static List<String> calculateKeys(Locale locale, String charset) {
    final ArrayList<String> result = new ArrayList<String>(3);
    final String language = locale.getLanguage();
    final int languageLength = language.length();
    final String country = locale.getCountry();
    final int countryLength = country.length();
    final String variant = locale.getVariant();
    final int variantLength = variant.length();

    if ((languageLength + countryLength + variantLength) == 0) {
        //The locale is "", "", "".
        return result;
    }

    final StringBuffer temp = new StringBuffer(charset);
    temp.append('_');
    temp.append(language);

    if (languageLength > 0) {
        result.add(0, temp.toString());
    }

    if ((countryLength + variantLength) == 0) {
        return result;
    }

    temp.append('_');
    temp.append(country);

    if (countryLength > 0) {
        result.add(0, temp.toString());
    }

    if (variantLength == 0) {
        return result;
    }

    temp.append('_');
    temp.append(variant);
    result.add(0, temp.toString());

    return result;
}

From source file:ch.entwine.weblounge.common.impl.language.LanguageUtils.java

/**
 * Returns the language object identified by the language identifier.
 * /*  w  w  w  . j a  va2s  .  c  om*/
 * @param languageCode
 *          the language identifier
 * @return the language
 * @throws UnknownLanguageException
 *           if there is no language for the given locale
 */
public static Language getLanguage(String languageCode) throws UnknownLanguageException {
    Language language = systemLanguages.get(languageCode);
    if (language != null)
        return language;
    for (Locale locale : Locale.getAvailableLocales()) {
        if (locale.getLanguage().equals(languageCode)) {
            language = new LanguageImpl(new Locale(languageCode, "", ""));
            systemLanguages.put(languageCode, language);
            break;
        }
    }
    if (language == null)
        throw new UnknownLanguageException(languageCode);
    return language;
}

From source file:com.libframework.annotation.util.OtherUtils.java

/**
 * @param context if null, use the default format
 *                (Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 %sSafari/534.30).
 * @return/*from   w  w  w  .  ja va 2  s  .  co m*/
 */
public static String getUserAgent(Context context) {
    String webUserAgent = null;
    if (context != null) {
        try {
            Class<?> sysResCls = Class.forName("com.android.internal.R$string");
            Field webUserAgentField = sysResCls.getDeclaredField("web_user_agent");
            Integer resId = (Integer) webUserAgentField.get(null);
            webUserAgent = context.getString(resId);
        } catch (Throwable ignored) {
        }
    }
    if (TextUtils.isEmpty(webUserAgent)) {
        webUserAgent = "Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 %sSafari/533.1";
    }

    Locale locale = Locale.getDefault();
    StringBuffer buffer = new StringBuffer();
    // Add version
    final String version = Build.VERSION.RELEASE;
    if (version.length() > 0) {
        buffer.append(version);
    } else {
        // default to "1.0"
        buffer.append("1.0");
    }
    buffer.append("; ");
    final String language = locale.getLanguage();
    if (language != null) {
        buffer.append(language.toLowerCase(Locale.getDefault()));
        final String country = locale.getCountry();
        if (country != null) {
            buffer.append("-");
            buffer.append(country.toLowerCase(Locale.getDefault()));
        }
    } else {
        // default to "en"
        buffer.append("en");
    }
    // add the model for the release build
    if ("REL".equals(Build.VERSION.CODENAME)) {
        final String model = Build.MODEL;
        if (model.length() > 0) {
            buffer.append("; ");
            buffer.append(model);
        }
    }
    final String id = Build.ID;
    if (id.length() > 0) {
        buffer.append(" Build/");
        buffer.append(id);
    }
    return String.format(webUserAgent, buffer, "Mobile ");
}

From source file:com.ddiiyy.xydz.xutils.util.OtherUtils.java

/**
 * @param context if null, use the default format
 *                (Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 %sSafari/534.30).
 * @return/*from   www . ja  v  a2  s. c  o m*/
 */
@SuppressLint("DefaultLocale")
@SuppressWarnings("rawtypes")
public static String getUserAgent(Context context) {
    String webUserAgent = null;
    if (context != null) {
        try {
            Class sysResCls = Class.forName("com.android.internal.R$string");
            Field webUserAgentField = sysResCls.getDeclaredField("web_user_agent");
            Integer resId = (Integer) webUserAgentField.get(null);
            webUserAgent = context.getString(resId);
        } catch (Throwable ignored) {
        }
    }
    if (TextUtils.isEmpty(webUserAgent)) {
        webUserAgent = "Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 %sSafari/533.1";
    }

    Locale locale = Locale.getDefault();
    StringBuffer buffer = new StringBuffer();
    // Add version
    final String version = Build.VERSION.RELEASE;
    if (version.length() > 0) {
        buffer.append(version);
    } else {
        // default to "1.0"
        buffer.append("1.0");
    }
    buffer.append("; ");
    final String language = locale.getLanguage();
    if (language != null) {
        buffer.append(language.toLowerCase());
        final String country = locale.getCountry();
        if (country != null) {
            buffer.append("-");
            buffer.append(country.toLowerCase());
        }
    } else {
        // default to "en"
        buffer.append("en");
    }
    // add the model for the release build
    if ("REL".equals(Build.VERSION.CODENAME)) {
        final String model = Build.MODEL;
        if (model.length() > 0) {
            buffer.append("; ");
            buffer.append(model);
        }
    }
    final String id = Build.ID;
    if (id.length() > 0) {
        buffer.append(" Build/");
        buffer.append(id);
    }
    return String.format(webUserAgent, buffer, "Mobile ");
}

From source file:com.lidroid.xutils.utils.OtherUtils.java

/**
 * @param context if null, use the default format
 *                (Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 %sSafari/534.30).
 * @return/*from  w ww  .j a v  a2 s.  c om*/
 */
@SuppressLint("DefaultLocale")
public static String getUserAgent(Context context) {
    String webUserAgent = null;
    if (context != null) {
        try {
            Class<?> sysResCls = Class.forName("com.android.internal.R$string");
            Field webUserAgentField = sysResCls.getDeclaredField("web_user_agent");
            Integer resId = (Integer) webUserAgentField.get(null);
            webUserAgent = context.getString(resId);
        } catch (Throwable ignored) {
        }
    }
    if (TextUtils.isEmpty(webUserAgent)) {
        webUserAgent = "Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 %sSafari/533.1";
    }

    Locale locale = Locale.getDefault();
    StringBuffer buffer = new StringBuffer();
    // Add version
    final String version = Build.VERSION.RELEASE;
    if (version.length() > 0) {
        buffer.append(version);
    } else {
        // default to "1.0"
        buffer.append("1.0");
    }
    buffer.append("; ");
    final String language = locale.getLanguage();
    if (language != null) {
        buffer.append(language.toLowerCase());
        final String country = locale.getCountry();
        if (country != null) {
            buffer.append("-");
            buffer.append(country.toLowerCase());
        }
    } else {
        // default to "en"
        buffer.append("en");
    }
    // add the model for the release build
    if ("REL".equals(Build.VERSION.CODENAME)) {
        final String model = Build.MODEL;
        if (model.length() > 0) {
            buffer.append("; ");
            buffer.append(model);
        }
    }
    final String id = Build.ID;
    if (id.length() > 0) {
        buffer.append(" Build/");
        buffer.append(id);
    }
    return String.format(webUserAgent, buffer, "Mobile ");
}