Example usage for java.util Locale Locale

List of usage examples for java.util Locale Locale

Introduction

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

Prototype

public Locale(String language, String country) 

Source Link

Document

Construct a locale from language and country.

Usage

From source file:net.rrm.ehour.config.EhourConfigJdbc.java

@Override
public Locale getFormattingLocale() {
    String formattingLocale = this.getString(LOCALE_COUNTRY.getDbField(), "en_US");

    if (!formattingLocale.contains("-")) {
        return new Locale(formattingLocale, formattingLocale);
    } else {/*  ww w.j a v a  2s . co  m*/
        return LocaleUtil.forLanguageTag(formattingLocale);
    }
}

From source file:com.opensymphony.xwork2.conversion.impl.NumberConverterTest.java

public void testStringToBigDecimalConversionWithCommasEN() throws Exception {
    // given// w ww.  j a va  2 s.com
    NumberConverter converter = new NumberConverter();
    Map<String, Object> context = new HashMap<>();
    context.put(ActionContext.LOCALE, new Locale("en", "US"));

    // when
    Object value = converter.convertValue(context, null, null, null, "100,234.4", BigDecimal.class);

    // then
    assertEquals(BigDecimal.valueOf(100234.4), value);
}

From source file:org.openmrs.module.rwandaprimarycare.LoginController.java

@RequestMapping(method = RequestMethod.POST)
public String loginUser(ModelMap model, HttpSession session, HttpServletRequest request,
        HttpServletResponse response) {/*from w  ww. j  a  v  a2  s .c o  m*/

    String redirect = null;

    try {
        String username = request.getParameter("uname");
        String password = request.getParameter("pw");

        // get the place to redirect: for touch screen this is simple
        redirect = determineRedirect(request);

        // only try to authenticate if they actually typed in a username
        if (username != null && username.length() > 0) {

            Context.authenticate(username, password);

            if (Context.isAuthenticated()) {

                User user = Context.getAuthenticatedUser();

                if (user.getUserProperties() != null && !user.getUserProperties().containsKey("keyboardType")) {
                    user.getUserProperties().put("keyboardType", "QWERTY"); //ABC is the other option
                    user = Context.getUserService().saveUser(user, null);
                }
                session.setAttribute("keyboardType", user.getUserProperty("keyboardType"));

                // load the user's default locale if possible
                if (user.getUserProperties() != null) {
                    if (user.getUserProperties().containsKey(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE)) {
                        String localeString = user
                                .getUserProperty(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE);
                        Locale locale = null;
                        if (localeString.length() == 5) {
                            //user's locale is language_COUNTRY (i.e. en_US)
                            String lang = localeString.substring(0, 2);
                            String country = localeString.substring(3, 5);
                            locale = new Locale(lang, country);
                        } else {
                            // user's locale is only the language (language plus greater than 2 char country code
                            locale = new Locale(localeString);
                        }
                        OpenmrsCookieLocaleResolver oclr = new OpenmrsCookieLocaleResolver();
                        oclr.setLocale(request, response, locale);
                    }
                }

                // In case the user has no preferences, make sure that the context has some locale set
                if (Context.getLocale() == null) {
                    Context.setLocale(OpenmrsConstants.GLOBAL_DEFAULT_LOCALE);
                }

            }

            if (log.isDebugEnabled()) {
                log.debug("Redirecting after login to: " + redirect);
                log.debug("Locale address: " + request.getLocalAddr());
            }

            response.sendRedirect(redirect);

            //return redirect;
        }
    } catch (ContextAuthenticationException e) {
        // set the error message for the user telling them
        // to try again
        //session.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "auth.password.invalid");
        log.error("failed to authenticate: ", e);
    } catch (Exception e) {
        log.error("Uexpected auth error", e);
    }

    // send the user back the login page because they either 
    // had a bad password or are locked out

    session.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
            Context.getMessageSourceService().getMessage("rwandaprimarycare.loginFailed"));
    session.setAttribute(WebConstants.OPENMRS_LOGIN_REDIRECT_HTTPSESSION_ATTR, redirect);

    return "/module/rwandaprimarycare/login";
}

From source file:io.mapzone.arena.geocode.GeocodeServlet.java

private GeocodeQuery extractQuery(HttpServletRequest req) throws Exception {
    GeocodeQuery query = new GeocodeQuery();
    // extract params
    if (!StringUtils.isBlank(req.getParameter("country"))) {
        query.country.set(new Locale("", req.getParameter("country")));
    }/*www . ja  v  a 2 s  .  co  m*/
    if (!StringUtils.isBlank(req.getParameter("targetLanguage"))) {
        query.targetLanguage.set(new Locale(req.getParameter("targetLanguage")));
    }
    if (!StringUtils.isBlank(req.getParameter("text"))) {
        query.text.set(URLDecoder.decode(req.getParameter("text"), "utf-8"));
    } else {
        if (!StringUtils.isBlank(req.getParameter("postalCode"))) {
            query.postalCode.set(URLDecoder.decode(req.getParameter("postalCode"), "utf-8"));
        }
        if (!StringUtils.isBlank(req.getParameter("city"))) {
            query.city.set(URLDecoder.decode(req.getParameter("city"), "utf-8"));
        }
        if (!StringUtils.isBlank(req.getParameter("street"))) {
            query.street.set(URLDecoder.decode(req.getParameter("street"), "utf-8"));
        }
        if (!StringUtils.isBlank(req.getParameter("houseNumber"))) {
            query.houseNumber.set(URLDecoder.decode(req.getParameter("houseNumber"), "utf-8"));
        }
    }
    return query;
}

From source file:architecture.ee.util.ApplicationHelper.java

public static Locale getLocale() {
    if (isReady()) {
        return getConfigService().getLocale();
    } else {//from  w  w  w. j  a va2 s.  c om
        String language = getConfigService().getLocalProperty(ApplicationConstants.LOCALE_LANGUAGE_PROP_NAME);
        if (language == null)
            language = "";
        String country = getConfigService().getLocalProperty(ApplicationConstants.LOCALE_COUNTRY_PROP_NAME);
        if (country == null)
            country = "";
        if (language.equals("") && country.equals(""))
            return Locale.getDefault();
        else
            return new Locale(language, country);
    }
}

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

public static void setGeoLocationCustomerInformation(String country, String region, String city,
        String language) {/*from   w w  w . ja  v  a 2  s .c  o  m*/

    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:gob.dp.simco.comun.FunctionUtil.java

public static String formateaDecimal(double numero) {
    if (numero > 0) {
        Locale locale = new Locale("en", "UK");
        String pattern = "###,###.##";

        DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(locale);
        decimalFormat.applyPattern(pattern);

        String format = decimalFormat.format(numero);
        return format;
    }/*from ww  w.  ja  v  a 2  s  . co m*/
    return null;
}

From source file:it.cnr.icar.eric.client.ui.thin.UserPreferencesBean.java

/** Sets the locales to 1st supported locale (directly or indirectly). */
private void initLocales() {
    Iterator<?> requestLocales = FacesContext.getCurrentInstance().getExternalContext().getRequestLocales();
    Collection<Locale> supportedLocales = getSupportedUiLocales();
    //iterate though client preferred locales until find supported 
    while (requestLocales.hasNext()) {
        Locale loc = (Locale) requestLocales.next();
        // try direct match
        if (supportedLocales.contains(loc)) {
            init(loc);/*from  ww w .ja  v a 2s .co  m*/
            return;
        }
        // try to use language country, without variant
        if (loc.getVariant() != null && !"".equals(loc.getVariant())) {
            loc = new Locale(loc.getLanguage(), loc.getCountry());
            if (supportedLocales.contains(loc)) {
                init(loc);
                return;
            }
        }
        // try to use language without country and variant
        if (loc.getCountry() != null && !"".equals(loc.getCountry())) {
            loc = new Locale(loc.getLanguage());
            if (supportedLocales.contains(loc)) {
                init(loc);
                return;
            }
        }
    }
    // fall to default locale, from properties (or en_US, if not defined)
    init(getDefaultLocale());
}

From source file:com.willwinder.universalgcodesender.i18n.Localization.java

private static int getEnglishKeyCount() {
    if (englishKeyCount > 0)
        return englishKeyCount;
    ResourceBundle b = ResourceBundle.getBundle("resources.MessagesBundle", new Locale("en", "US"));
    englishKeyCount = getKeyCount(b);//from   ww  w .ja v a  2s  .  c  o m
    return englishKeyCount;
}

From source file:py.una.pol.karaku.util.I18nHelper.java

protected Locale getLocale() {

    return new Locale("es", "PY");
}