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, String variant) 

Source Link

Document

Construct a locale from language, country and variant.

Usage

From source file:fr.cls.atoll.motu.library.converter.jaxb.LocaleAdapter.java

/**
 * Convert a given string locale representation into an instance of {@link Locale}.
 * //from ww  w  .j a  v a  2  s  . c o m
 * @param localeString the string to convert into an locale.
 * 
 * @return a {@link Locale} instance.
 */
@Override
public Locale unmarshal(String localeString) {
    if (localeString == null) {
        return null;
    }
    String[] parts = tokenizeToStringArray(localeString, "_ ", false, false);
    String language = (parts.length > 0 ? parts[0] : "");
    String country = (parts.length > 1 ? parts[1] : "");
    String variant = "";
    if (parts.length >= 2) {
        // There is definitely a variant, and it is everything after the country
        // code sans the separator between the country code and the variant.
        int endIndexOfCountryCode = localeString.indexOf(country) + country.length();
        // Strip off any leading '_' and whitespace, what's left is the variant.
        variant = (localeString.substring(endIndexOfCountryCode));
        if (variant.startsWith("_")) {
            variant = trimLeadingCharacter(variant, '_');
        }
    }
    return (language.length() > 0 ? new Locale(language, country, variant) : null);
}

From source file:com.adito.language.actions.SelectLanguageAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    String referer = DAVUtilities.encodePath(CoreUtil.getRequestReferer(request), false);
    if (referer == null) {
        throw new CoreException(ErrorConstants.ERR_MISSING_REQUEST_PARAMETER, ErrorConstants.CATEGORY_NAME,
                "referer");
    }/*  w ww .j av  a2s. c  om*/
    String localeCode = request.getParameter("locale");
    if (localeCode == null) {
        throw new CoreException(ErrorConstants.ERR_MISSING_REQUEST_PARAMETER, ErrorConstants.CATEGORY_NAME,
                "locale");
    }

    /* Tokenize the locale parameter so we only get the first line. This prevents
     * a header injection exploit as the (not validated) locale gets added as 
     * a cookie.
     */
    StringTokenizer t = new StringTokenizer(localeCode);
    String locale = t.nextToken();

    // Parse the locale code
    String country = "";
    String variant = "";
    String lang = locale;
    int idx = locale.indexOf("_");
    if (idx != -1) {
        country = lang.substring(idx + 1);
        lang = lang.substring(0, idx);
    }
    idx = country.indexOf('_');
    if (idx != -1) {
        variant = country.substring(idx + 1);
        country = country.substring(0, idx);
    }

    // Store the new locale in the session and set a persistant cookie
    Locale l = new Locale(lang, country, variant);
    request.getSession().setAttribute(Globals.LOCALE_KEY, l);
    Cookie cookie = new Cookie(SystemProperties.get("adito.cookie", "SSLX_SSESHID") + "_LANG",
            locale.toString());
    cookie.setMaxAge(60 * 60 * 24 * 7); // a week
    cookie.setPath("/");
    cookie.setSecure(true);
    response.addCookie(cookie);
    return referer == null ? mapping.findForward("home") : new ActionForward(referer, true);
}

From source file:com.sslexplorer.language.actions.SelectLanguageAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    String referer = DAVUtilities.encodePath(CoreUtil.getRequestReferer(request), false);
    if (referer == null) {
        throw new CoreException(ErrorConstants.ERR_MISSING_REQUEST_PARAMETER, ErrorConstants.CATEGORY_NAME,
                "referer");
    }/*from  w  w w  .j av a 2  s .  c  o  m*/
    String localeCode = request.getParameter("locale");
    if (localeCode == null) {
        throw new CoreException(ErrorConstants.ERR_MISSING_REQUEST_PARAMETER, ErrorConstants.CATEGORY_NAME,
                "locale");
    }

    /* Tokenize the locale parameter so we only get the first line. This prevents
     * a header injection exploit as the (not validated) locale gets added as 
     * a cookie.
     */
    StringTokenizer t = new StringTokenizer(localeCode);
    String locale = t.nextToken();

    // Parse the locale code
    String country = "";
    String variant = "";
    String lang = locale;
    int idx = locale.indexOf("_");
    if (idx != -1) {
        country = lang.substring(idx + 1);
        lang = lang.substring(0, idx);
    }
    idx = country.indexOf('_');
    if (idx != -1) {
        variant = country.substring(idx + 1);
        country = country.substring(0, idx);
    }

    // Store the new locale in the session and set a persistant cookie
    Locale l = new Locale(lang, country, variant);
    request.getSession().setAttribute(Globals.LOCALE_KEY, l);
    Cookie cookie = new Cookie(SystemProperties.get("sslexplorer.cookie", "SSLX_SSESHID") + "_LANG",
            locale.toString());
    cookie.setMaxAge(60 * 60 * 24 * 7); // a week
    cookie.setPath("/");
    cookie.setSecure(true);
    response.addCookie(cookie);
    return referer == null ? mapping.findForward("home") : new ActionForward(referer, true);
}

From source file:com.limegroup.gnutella.gui.LanguageUtils.java

/**
 * Returns the languages as found from the classpath in messages.jar
 *///from w ww.j  a  v  a  2s.c  om
static void addLocalesFromJar(List<Locale> locales, File jar) {
    ZipFile zip = null;
    try {
        zip = new ZipFile(jar);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            String name = entries.nextElement().getName();
            if (!name.startsWith(BUNDLE_PREFIX) || !name.endsWith(BUNDLE_POSTFIX) || name.indexOf("$") != -1) {
                continue;
            }

            String iso = name.substring(BUNDLE_PREFIX.length(), name.length() - BUNDLE_POSTFIX.length());
            List<String> tokens = new ArrayList<String>(Arrays.asList(iso.split("_", 3)));
            if (tokens.size() < 1) {
                continue;
            }
            while (tokens.size() < 3) {
                tokens.add("");
            }

            Locale locale = new Locale(tokens.get(0), tokens.get(1), tokens.get(2));
            locales.add(locale);
        }
    } catch (IOException e) {
        LOG.warn("Could not determine locales", e);
    } finally {
        if (zip != null) {
            try {
                zip.close();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:org.keyboardplaying.xtt.ui.i18n.I18nHelper.java

/**
 * Parses the locale from the supplied representation.
 *
 * @param locale//w  w  w  .j  av a2s.  c  o  m
 *            the string representation of a locale (e.g.: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr_MAC")
 * @return the associated {@link Locale} or default locale if format is incorrect
 */
public Locale parseLocale(String locale) {
    Matcher m = Pattern.compile(LOCALE_REGEX).matcher(locale);
    Locale l;

    if (!m.matches()) {
        LOG.warn(locale + " does not match the expected format for a locale");
        l = Locale.getDefault();
    } else {
        String language = m.group(1);
        String country = m.group(2);
        String variant = m.group(3);
        if (variant != null) {
            l = new Locale(language, country, variant);
        } else if (country != null) {
            l = new Locale(language, country);
        } else {
            l = new Locale(language);
        }
    }

    return l;
}

From source file:com.fredhopper.connector.config.DefaultIndexConfigService.java

private HashSet<Locale> getLocales(final FredhopperIndexConfigModel indexConfigModel) {

    final Collection<String> localesList = indexConfigModel.getLocales();
    final HashSet<Locale> locales = new HashSet<>();
    for (final String localeString : localesList) {
        final String[] loc = Utilities.parseLocaleCodes(localeString);
        final Locale locale = new Locale(loc[0], loc[1], loc[2]);
        locales.add(locale);/*from  w ww  .  j  a  v  a2 s  .co  m*/
    }
    return locales;
}

From source file:com.adrguides.model.Guide.java

public Locale getLocale() {
    return new Locale(language, country, variant);
}