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:org.musicrecital.webapp.filter.LocaleFilter.java

/**
 * This method looks for a "locale" request parameter. If it finds one, it sets it as the preferred locale
 * and also configures it to work with JSTL.
 * /*from  w w w  .ja v a  2  s. c  om*/
 * @param request the current request
 * @param response the current response
 * @param chain the chain
 * @throws IOException when something goes wrong
 * @throws ServletException when a communication failure happens
 */
@SuppressWarnings("unchecked")
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    String locale = request.getParameter("locale");
    Locale preferredLocale = null;

    if (locale != null) {
        int indexOfUnderscore = locale.indexOf('_');
        if (indexOfUnderscore != -1) {
            String language = locale.substring(0, indexOfUnderscore);
            String country = locale.substring(indexOfUnderscore + 1);
            preferredLocale = new Locale(language, country);
        } else {
            preferredLocale = new Locale(locale);
        }
    }

    HttpSession session = request.getSession(false);

    if (session != null) {
        if (preferredLocale == null) {
            preferredLocale = (Locale) session.getAttribute(Constants.PREFERRED_LOCALE_KEY);
        } else {
            session.setAttribute(Constants.PREFERRED_LOCALE_KEY, preferredLocale);
            Config.set(session, Config.FMT_LOCALE, preferredLocale);
        }

        if (preferredLocale != null && !(request instanceof LocaleRequestWrapper)) {
            request = new LocaleRequestWrapper(request, preferredLocale);
            LocaleContextHolder.setLocale(preferredLocale);
        }
    }

    chain.doFilter(request, response);

    // Reset thread-bound LocaleContext.
    LocaleContextHolder.setLocaleContext(null);
}

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

/**
 * Loads the resource bundle with all translations for the given language and region
 *
 * @param language the language to load, ex: en, sv, de
 * @param region the region of the language to load, ex: US, SE, DE
 * @throws MissingResourceException if the resource bundle couldn't be found
 *//* w ww  .j  a v a2 s.  c  o  m*/
private static void loadResourceBundle(String language, String region) throws MissingResourceException {
    Localization.region = region;
    Locale locale = new Locale(language, region);
    Locale.setDefault(locale);
    bundle = ResourceBundle.getBundle("resources.MessagesBundle", locale);
}

From source file:org.vietspider.content.export.ExcelExportDataDialog.java

public void exportMore(File file, String data) {
    String[] elements = data.split("\n");
    WritableWorkbook workbook = null;//from www .  java 2  s.c  o  m

    try {
        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        workbook = Workbook.createWorkbook(file, ws);
        int sheetIndex = 0;
        TextSpliter spliter = new TextSpliter();
        for (int i = 0; i < elements.length - 1; i += 2) {
            export(workbook, sheetIndex, elements[i], spliter.toArray(elements[i + 1], ','));
            sheetIndex++;
        }
        workbook.write();
    } catch (Exception e) {
        ClientLog.getInstance().setException(null, e);
    } finally {
        try {

            if (workbook != null)
                workbook.close();
        } catch (Exception e) {
            ClientLog.getInstance().setException(null, e);
        }
    }
}

From source file:com.gcrm.security.AuthenticationFilter.java

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
        throws AuthenticationException {
    if (!request.getMethod().equals("POST")) {
        throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
    }//from   w ww .ja va 2 s  . co  m

    String username = obtainUsername(request);
    String password = obtainPassword(request);

    // Validates username and password
    username = username.trim();

    String localValue = obtainLanguage(request);
    String[] locals = localValue.split("_");
    Locale locale = new Locale(locals[0], locals[1]);
    request.getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale);
    request.getSession().setAttribute("locale", localValue);
    Locale.setDefault(locale);

    User user = UserUtil.getUser(username);
    Md5PasswordEncoder encoder = new Md5PasswordEncoder();
    password = encoder.encodePassword(password, AuthenticationFilter.SALT);
    if (user == null || !user.getPassword().equals(password)) {
        ResourceBundle rb = CommonUtil.getResourceBundle();
        String errorMessage = rb.getString("error.login.denied");
        throw new AuthenticationServiceException(errorMessage);
    }

    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username,
            password);
    setDetails(request, authRequest);

    // return authRequest;
    return this.getAuthenticationManager().authenticate(authRequest);
}

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);//from   w  w  w  .ja v  a2s .  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;
}

From source file:com.xpn.xwiki.render.XWikiRadeoxRenderer.java

private void initRadeoxEngine() {
    // This is needed so that our local config is used
    InitialRenderContext ircontext = new BaseInitialRenderContext();
    Locale locale = new Locale("xwiki", "xwiki");
    ircontext.set(RenderContext.INPUT_LOCALE, locale);
    ircontext.set(RenderContext.OUTPUT_LOCALE, locale);
    ircontext.set(RenderContext.LANGUAGE_LOCALE, locale);
    ircontext.setParameters(new HashMap());

    this.initialRenderContext = ircontext;
    this.filterPipe = initFilterPipe(ircontext);
}

From source file:com.github.caldav4j.methods.PutGetTest.java

@Test
public void testResourceBundle() {
    // load an ICS and substitute summary with non-latin chars
    Locale mylocale = new Locale("ru", "RU");
    ResourceBundle messages = PropertyResourceBundle.getBundle("messages", mylocale);
    String myLocalSummary = messages.getString("summary");
    log.info("default charset: " + Charset.defaultCharset());
    assertTrue(true);//  w w  w.  j ava2  s  .c om
}

From source file:quanlyhocvu.api.mongodb.service.HandleExcelFile.java

public void createWorkbook() throws IOException {
    File file = new File(inputFile);
    WorkbookSettings wbSettings = new WorkbookSettings();
    wbSettings.setLocale(new Locale("en", "EN"));
    setWorkbook(Workbook.createWorkbook(file, wbSettings));
}

From source file:info.magnolia.cms.i18n.LocaleDefinition.java

/**
 * Creates the locale for this definition if not yet set.
 *///  www. j a  va 2  s .c  om
public Locale getLocale() {
    if (locale == null && getLanguage() != null) {
        locale = new Locale(getLanguage(), StringUtils.defaultString(getCountry()));
    }
    return locale;
}

From source file:net.sourceforge.atunes.kernel.modules.state.LocaleBean.java

/**
 * Get suitable java.util.Locale object// ww  w.  java  2s.  c  om
 * 
 * @return A suitable java.util.Locale object
 */
@Override
public Locale getLocale() {
    return new Locale(this.language, this.country);
}