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:grandroid.geo.Geocoder.java

public static List<Address> getFromLocation(Locale locale, double lat, double lng, int maxResult) {
    String language = locale.getLanguage();
    if (locale == Locale.TAIWAN) {
        language = "zh-TW";
    }// w  ww . j a v  a 2s  .  com
    String address = String.format(locale,
            "http://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f&sensor=false&language="
                    + language,
            lat, lng);//locale.getCountry()
    HttpGet httpGet = new HttpGet(address);
    HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(AllClientPNames.USER_AGENT,
            "Mozilla/5.0 (Java) Gecko/20081007 java-geocoder");
    //client.getParams().setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, 5 * 1000);
    //client.getParams().setIntParameter(AllClientPNames.SO_TIMEOUT, 25 * 1000);
    HttpResponse response;

    List<Address> retList = null;

    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String json = EntityUtils.toString(entity, "UTF-8");

        JSONObject jsonObject = new JSONObject(json);

        retList = new ArrayList<Address>();

        if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) {
            JSONArray results = jsonObject.getJSONArray("results");
            if (results.length() > 0) {
                for (int i = 0; i < results.length() && i < maxResult; i++) {
                    JSONObject result = results.getJSONObject(i);
                    //Log.e(MyGeocoder.class.getName(), result.toString());
                    Address addr = new Address(Locale.getDefault());
                    // addr.setAddressLine(0, result.getString("formatted_address"));

                    JSONArray components = result.getJSONArray("address_components");
                    String streetNumber = "";
                    String route = "";
                    for (int a = 0; a < components.length(); a++) {
                        JSONObject component = components.getJSONObject(a);
                        JSONArray types = component.getJSONArray("types");
                        for (int j = 0; j < types.length(); j++) {
                            String type = types.getString(j);
                            if (type.equals("locality") || type.equals("administrative_area_level_3")) {
                                addr.setLocality(component.getString("long_name"));
                            } else if (type.equals("street_number")) {
                                streetNumber = component.getString("long_name");
                            } else if (type.equals("route")) {
                                route = component.getString("long_name");
                            } else if (type.equals("administrative_area_level_1")) {
                                addr.setAdminArea(component.getString("long_name"));
                            } else if (type.equals("country")) {
                                addr.setCountryName(component.getString("long_name"));
                                addr.setCountryCode(component.getString("short_name"));
                            }
                        }
                    }
                    addr.setAddressLine(0, route + " " + streetNumber);
                    if (result.has("formatted_address")) {
                        addr.setFeatureName(result.getString("formatted_address"));
                    }
                    addr.setLatitude(
                            result.getJSONObject("geometry").getJSONObject("location").getDouble("lat"));
                    addr.setLongitude(
                            result.getJSONObject("geometry").getJSONObject("location").getDouble("lng"));
                    if (addr.getAdminArea() == null) {
                        addr.setAdminArea("");
                    }
                    retList.add(addr);
                }
            }
        }
    } catch (ClientProtocolException e) {
        Log.e("grandroid", "Error calling Google geocode webservice.", e);
    } catch (IOException e) {
        Log.e("grandroid", "Error calling Google geocode webservice.", e);
    } catch (JSONException e) {
        Log.e("grandroid", "Error parsing Google geocode webservice response.", e);
    }
    return retList;
}

From source file:com.salesmanager.core.util.www.ajax.CustomerUtil.java

public static void setGeoLocationCustomerInformation(String country, String region, String city,
        String language) {/*from  www . j a  v  a  2  s .com*/

    HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
    HttpSession session = WebContextFactory.get().getSession();

    try {

        log.info("Setting LOCALE Country -> " + country + " region -> " + region + " city -> " + city);

        if (!StringUtils.isBlank(country)) {

            CountryDescription desc = CountryUtil.getCountryByIsoCode(country, req.getLocale());

            if (desc != null) {

                log.info(" Country Description -> " + desc.getCountryName());

                Customer customer = SessionUtil.getCustomer(req);
                if (customer == null) {
                    customer = new Customer();
                }
                customer.setCountryName(desc.getCountryName());
                customer.setCustomerBillingCountryName(desc.getCountryName());
                customer.setCustomerBillingCountryId(desc.getId().getCountryId());
                customer.setCustomerCountryId(desc.getId().getCountryId());

                // get the zone
                Zone zone = CountryUtil.getZoneCodeByCode(region, req.getLocale());
                if (zone != null) {
                    customer.setCustomerBillingZoneId(zone.getZoneId());
                    customer.setStateProvinceName(zone.getZoneName());
                    customer.setCustomerZoneId(zone.getZoneId());
                    customer.setCustomerState(zone.getZoneName());
                }

                MerchantStore store = SessionUtil.getMerchantStore(req);
                if (store != null) {
                    customer.setMerchantId(store.getMerchantId());
                }

                // set Locale
                Locale locale = LocaleUtil.getLocale(req);
                log.info("Actual locale (" + locale.toString());
                String l = locale.getLanguage();

                locale = new Locale(l, country);
                log.info("Setting locale (" + l + "_" + country + ")");
                log.info("New locale (" + locale.toString() + ")");

                LocaleUtil.setLocale(req, locale);

                customer.setLocale(locale);
                customer.setCustomerLang(locale.getLanguage());

                SessionUtil.setCustomer(customer, req);

            } else {
                log.info("Setting default locale (1)");
                Locale locale = LocaleUtil.getDefaultLocale();
                LocaleUtil.setLocale(req, locale);
            }

        }

    } catch (Exception e) {
        log.error(e);
    }
}

From source file:de.blizzy.documentr.web.Functions.java

public static String getLanguage() {
    Locale locale = LocaleContextHolder.getLocale();
    return locale.getLanguage();
}

From source file:Main.java

public static NdefRecord createTextRecord(String text, Locale locale, boolean encodeInUtf8) {
    if (text == null) {
        throw new NullPointerException("text is MUST require!!");
    }//from  ww w . jav a 2s. c  o  m
    if (locale == null) {
        throw new NullPointerException("locale is MUST require!!");
    }

    byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));

    Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
    text = convertToCrlf(text);
    byte[] textBytes = text.getBytes(utfEncoding);

    int utfBit = encodeInUtf8 ? 0 : (1 << 7);
    char status = (char) (utfBit + langBytes.length);

    byte[] data = bytesConcat(new byte[] { (byte) status }, langBytes, textBytes);

    return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
}

From source file:com.aoindustries.website.signup.ServerConfirmationCompletedActionHelper.java

public static String getHtmlLang(Locale locale) {
    String language = locale.getLanguage();
    if (language != null) {
        String country = locale.getCountry();
        if (country != null) {
            String variant = locale.getVariant();
            if (variant != null)
                return language + "-" + country + "-" + variant;
            return language + "-" + country;
        }/*from   w w w .  j  av a  2s .com*/
        return language;
    }
    return null;
}

From source file:org.tinymediamanager.scraper.entities.MediaGenres.java

/**
 * Iterates over all found languages and gets the "alternative name" of specified property
 * //from  www  .j  a v  a  2 s .  com
 * @param propName
 *          the property
 * @return array of alternate names
 */
public static String[] loadAlternateNames(String propName) {
    ArrayList<String> alt = new ArrayList<>();
    for (Locale loc : getLanguages()) {
        if (loc == null || loc.getLanguage().equals("en")) {
            // English not needed, since it's in default properties
            // and for invalid languages (like NB) it will be null
            continue;
        }
        ResourceBundle b = ApiResourceBundle.getResourceBundle(loc);
        try {
            alt.add(loc.getLanguage() + "-" + b.getString("Genres." + propName)); // just genres
        } catch (Exception e) {
            // not found or localized - ignore
        }
    }
    return alt.toArray(new String[alt.size()]);
}

From source file:LocaleUtils.java

/**
 * <p>Obtains the list of countries supported for a given language.</p>
 * // ww  w.  j  a  v a  2  s  .  co m
 * <p>This method takes a language code and searches to find the
 * countries available for that language. Variant locales are removed.</p>
 *
 * @param languageCode  the 2 letter language code, null returns empty
 * @return an unmodifiable List of Locale objects, never null
 */
public static List countriesByLanguage(String languageCode) {
    List countries = (List) cCountriesByLanguage.get(languageCode); //syncd
    if (countries == null) {
        if (languageCode != null) {
            countries = new ArrayList();
            List locales = availableLocaleList();
            for (int i = 0; i < locales.size(); i++) {
                Locale locale = (Locale) locales.get(i);
                if (languageCode.equals(locale.getLanguage()) && locale.getCountry().length() != 0
                        && locale.getVariant().length() == 0) {
                    countries.add(locale);
                }
            }
            countries = Collections.unmodifiableList(countries);
        } else {
            countries = Collections.EMPTY_LIST;
        }
        cCountriesByLanguage.put(languageCode, countries); //syncd
    }
    return countries;
}

From source file:net.geoprism.localization.LocaleManager.java

public static String getLocaleName(Locale _locale) {
    StringBuffer buffer = new StringBuffer();

    if (_locale.getLanguage() != null && _locale.getLanguage().length() > 0) {
        buffer.append(_locale.getLanguage());
    }/*  w  ww  . j  a v a  2s .  co  m*/

    if (_locale.getCountry() != null && _locale.getCountry().length() > 0) {
        buffer.append("-" + _locale.getCountry());
    }

    if (_locale.getVariant() != null && _locale.getVariant().length() > 0) {
        buffer.append("-" + _locale.getVariant());
    }

    return buffer.toString();
}

From source file:com.jslsolucoes.tagria.lib.util.TagUtil.java

public static String localization(JspContext jspContext) {
    Locale locale = locale(jspContext);
    List<String> fullLocale = Lists.newArrayList(locale.getLanguage());
    if (!StringUtils.isEmpty(locale.getCountry())) {
        fullLocale.add(locale.getCountry());
    }//  w w  w .  j  a  v a2 s  . c o  m
    return StringUtils.join(fullLocale, "-");
}

From source file:architecture.common.util.LocaleUtils.java

public static List<Locale> getCandidateLocales(Locale locale) {
    if (cachedCandidateLocales.containsKey(locale))
        return cachedCandidateLocales.get(locale);

    List<Locale> results = new ArrayList<Locale>();
    results.add(locale);//w  ww.  ja  va 2s  .c  o  m

    if (locale.getVariant().length() > 0)
        results.add(new Locale(locale.getLanguage(), locale.getCountry()));

    if (locale.getCountry().length() > 0)
        results.add(new Locale(locale.getLanguage()));

    results = Collections.unmodifiableList(results);
    cachedCandidateLocales.put(locale, results);

    return results;
}