Example usage for java.util Locale getAvailableLocales

List of usage examples for java.util Locale getAvailableLocales

Introduction

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

Prototype

public static Locale[] getAvailableLocales() 

Source Link

Document

Returns an array of all installed locales.

Usage

From source file:de.skubware.opentraining.activity.create_exercise.NameFragment.java

public NameFragment() {
    super(R.layout.fragment_create_exercise_name);

    // set up language c spinnner
    mLanguageCodeMap = new HashMap<String, String>();
    Set<String> localeStringSet = new TreeSet<String>();
    for (Locale l : Locale.getAvailableLocales()) {
        if (localeStringSet.add(l.getDisplayLanguage())) {
            mLanguageCodeMap.put(l.getDisplayLanguage(), l.getLanguage());
        }//from   w w w .j  a v  a 2  s .c  o  m
    }

    mSpinnerDataList = new ArrayList<String>(localeStringSet);
}

From source file:org.primeframework.mvc.control.form.LocaleSelect.java

/**
 * Adds the countries Map and then calls super.
 *///w  w  w  .  jav a  2 s  . co m
@Override
protected Map<String, Object> makeParameters() {
    LinkedHashMap<String, String> locales = new LinkedHashMap<>();
    String preferred = (String) attributes.get("preferredLocales");
    if (preferred != null) {
        String[] parts = preferred.split(",");
        for (String part : parts) {
            Locale locale = LocaleUtils.toLocale(part);
            locales.put(locale.toString(), locale.getDisplayName(locale));
        }
    }

    boolean includeCountries = attributes.containsKey("includeCountries")
            ? (Boolean) attributes.get("includeCountries")
            : true;
    List<Locale> allLocales = new ArrayList<>();
    Collections.addAll(allLocales, Locale.getAvailableLocales());
    allLocales.removeIf((locale) -> locale.getLanguage().isEmpty() || locale.hasExtensions()
            || !locale.getScript().isEmpty() || !locale.getVariant().isEmpty()
            || (!includeCountries && !locale.getCountry().isEmpty()));
    allLocales.sort((one, two) -> one.getDisplayName(locale).compareTo(two.getDisplayName(locale)));

    for (Locale locale : allLocales) {
        if (!locales.containsKey(locale.getCountry())) {
            locales.put(locale.toString(), locale.getDisplayName(this.locale));
        }
    }

    attributes.put("items", locales);

    return super.makeParameters();
}

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

/**
 * Returns the language object that represents the given locale.
 * /*from   www.j  a  v a  2  s  .c  om*/
 * @param locale
 *          the locale
 * @return the language
 * @throws UnknownLanguageException
 *           if there is no language for the given locale
 */
public static Language getLanguage(Locale locale) throws UnknownLanguageException {

    // Do we know this language already?
    Language language = systemLanguages.get(locale.getLanguage());
    if (language != null)
        return language;

    // Makes sure we get the locale in the right format (might be hand crafted)
    Matcher matcher = ACCEPT_LANGUAGE_HEADER.matcher(locale.getLanguage());
    if (matcher.matches()) {
        locale = new Locale(matcher.group(2), matcher.group(1));
    }

    // Check the system locales for a match
    Locale systemLocale = null;
    try {
        for (Locale l : Locale.getAvailableLocales()) {
            if (l.getISO3Language().equals(locale.getISO3Language())) {
                systemLocale = l;
                break;
            } else if (l.getLanguage().equals(l.getLanguage())) {
                systemLocale = l;
                break;
            }
        }
    } catch (MissingResourceException e) {
        logger.debug("No 3 found for '{}': {}", locale, e.getMessage());
    }

    // Is there a matching system locale?
    if (systemLocale != null) {
        language = new LanguageImpl(locale);
        systemLanguages.put(locale.getLanguage(), language);
        return language;
    }

    // Apparently not...
    throw new UnknownLanguageException(locale.getLanguage());
}

From source file:org.appd.login.V_Login.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    et_User = (EditText) getActivity().findViewById(R.id.et_User);
    et_Pass = (EditText) getActivity().findViewById(R.id.et_Pass);
    ch_SavePass = (CheckBox) getActivity().findViewById(R.id.ch_SavePass);
    ch_AutomaticVisitClosing = (CheckBox) getActivity().findViewById(R.id.ch_AutomaticVisitClosing);
    sp_Language = (Cust_Spinner) getActivity().findViewById(R.id.sp_Language);

    ArrayList<DisplaySpinner> data = new ArrayList<DisplaySpinner>();
    for (Locale loc : Locale.getAvailableLocales()) {
        data.add(new DisplaySpinner(0, loc.getDisplayName(), loc.toString()));
    }/*from   w  w w.  j  av a 2s  .c om*/
    sp_Language.load(data);

}

From source file:org.pentaho.platform.web.http.context.SolutionContextListener.java

public void contextInitialized(final ServletContextEvent event) {

    ServletContext context = event.getServletContext();

    String encoding = context.getInitParameter("encoding"); //$NON-NLS-1$
    if (encoding != null) {
        LocaleHelper.setSystemEncoding(encoding);
    }/* w  w w .  j  a va2  s. c  om*/

    String textDirection = context.getInitParameter("text-direction"); //$NON-NLS-1$
    if (textDirection != null) {
        LocaleHelper.setTextDirection(textDirection);
    }

    String localeLanguage = context.getInitParameter("locale-language"); //$NON-NLS-1$
    String localeCountry = context.getInitParameter("locale-country"); //$NON-NLS-1$
    boolean localeSet = false;
    if ((localeLanguage != null) && !"".equals(localeLanguage) && (localeCountry != null)
            && !"".equals(localeCountry)) { //$NON-NLS-1$ //$NON-NLS-2$
        Locale[] locales = Locale.getAvailableLocales();
        if (locales != null) {
            for (Locale element : locales) {
                if (element.getLanguage().equals(localeLanguage)
                        && element.getCountry().equals(localeCountry)) {
                    LocaleHelper.setLocale(element);
                    localeSet = true;
                    break;
                }
            }
        }
    }
    if (!localeSet) {
        // do this thread in the default locale
        LocaleHelper.setLocale(Locale.getDefault());
    }
    LocaleHelper.setDefaultLocale(LocaleHelper.getLocale());
    // log everything that goes on here
    Logger.info(SolutionContextListener.class.getName(),
            Messages.getInstance().getString("SolutionContextListener.INFO_INITIALIZING")); //$NON-NLS-1$
    Logger.info(SolutionContextListener.class.getName(),
            Messages.getInstance().getString("SolutionContextListener.INFO_SERVLET_CONTEXT") + context); //$NON-NLS-1$
    SolutionContextListener.contextPath = context.getRealPath(""); //$NON-NLS-1$
    Logger.info(SolutionContextListener.class.getName(),
            Messages.getInstance().getString("SolutionContextListener.INFO_CONTEXT_PATH") //$NON-NLS-1$
                    + SolutionContextListener.contextPath);

    SolutionContextListener.solutionPath = PentahoHttpSessionHelper.getSolutionPath(context);
    if (StringUtils.isEmpty(SolutionContextListener.solutionPath)) {
        String errorMsg = Messages.getInstance()
                .getErrorString("SolutionContextListener.ERROR_0001_NO_ROOT_PATH"); //$NON-NLS-1$
        Logger.error(getClass().getName(), errorMsg);
        /*
         * Since we couldn't find solution repository path there is no point in going forward and the user should know
         * that a major config setting was not found. So we are throwing in a RunTimeException with the requisite message.
         */
        throw new RuntimeException(errorMsg);
    }

    Logger.info(getClass().getName(), Messages.getInstance().getString("SolutionContextListener.INFO_ROOT_PATH")
            + SolutionContextListener.solutionPath); //$NON-NLS-1$

    String fullyQualifiedServerUrl = context.getInitParameter("fully-qualified-server-url"); //$NON-NLS-1$
    if (fullyQualifiedServerUrl == null) {
        // assume this is a demo installation
        // TODO: Create a servlet that's loaded on startup to set this value
        fullyQualifiedServerUrl = "http://localhost:8080/pentaho/"; //$NON-NLS-1$
    }

    IApplicationContext applicationContext = new WebApplicationContext(SolutionContextListener.solutionPath,
            fullyQualifiedServerUrl, context.getRealPath(""), context); //$NON-NLS-1$

    /*
     * Copy out all the initParameter values from the servlet context and put them in the application context.
     */
    Properties props = new Properties();
    Enumeration<?> initParmNames = context.getInitParameterNames();
    String initParmName;
    while (initParmNames.hasMoreElements()) {
        initParmName = (String) initParmNames.nextElement();
        props.setProperty(initParmName, context.getInitParameter(initParmName));
    }
    ((WebApplicationContext) applicationContext).setProperties(props);

    setSystemCfgFile(context);
    setObjectFactory(context);

    boolean initOk = PentahoSystem.init(applicationContext);

    this.showInitializationMessage(initOk, fullyQualifiedServerUrl);
}

From source file:org.projectforge.web.wicket.converter.LanguageConverter.java

/**
 * Uses all available locales and compares the string value with the display name (in the given locale).
 * /*w  ww.  ja  va 2 s . c o m*/
 * @param value The string representation.
 * @param locale The user's locale to use the correct translation.
 * @see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String, java.util.Locale)
 */
@Override
public Object convertToObject(final String value, final Locale locale) {
    if (StringUtils.isEmpty(value) == true) {
        return null;
    }
    final String lvalue = value.toLowerCase(locale);
    for (final Locale lc : Locale.getAvailableLocales()) {
        if (getLanguageAsString(lc, locale).toLowerCase().equals(lvalue) == true) {
            return lc;
        }
    }
    error();
    return null;
}

From source file:org.unitime.localization.impl.Localization.java

private static Locale guessJavaLocale(String locale) {
    for (StringTokenizer s = new StringTokenizer(locale, ",;"); s.hasMoreTokens();) {
        String lang = s.nextToken();
        String cc = null;/*from www .jav  a 2 s  .c  om*/
        if (lang.indexOf('_') >= 0) {
            cc = lang.substring(lang.indexOf('_') + 1);
            lang = lang.substring(0, lang.indexOf('_'));
        }
        for (Locale loc : Locale.getAvailableLocales())
            if ((lang == null || lang.isEmpty() || lang.equals(loc.getLanguage()))
                    && (cc == null || cc.isEmpty() || cc.equals(loc.getCountry()))) {
                return loc;
            }
    }
    return Locale.getDefault();
}

From source file:cn.vlabs.duckling.vwb.VWBFilter.java

private static Locale getLocale(String lstr) {
    if (lstr == null || lstr.length() < 1) {
        return null;
    }//w  ww .j a  v a 2  s  .  c o  m
    Locale locale = locales.get(lstr.toLowerCase());
    if (locale != null) {
        return locale;
    } else {
        StringTokenizer localeTokens = new StringTokenizer(lstr, "_");
        String lang = null;
        String country = null;
        if (localeTokens.hasMoreTokens()) {
            lang = localeTokens.nextToken();
        }
        if (localeTokens.hasMoreTokens()) {
            country = localeTokens.nextToken();
        }
        locale = new Locale(lang, country);
        Locale crtls[] = Locale.getAvailableLocales();
        for (int i = 0; i < crtls.length; i++) {
            if (crtls[i].equals(locale)) {
                locale = crtls[i];
                break;
            }
        }
        locales.put(lstr.toLowerCase(), locale);
    }
    return locale;
}

From source file:org.jboss.dashboard.LocaleManager.java

/**
 * Locales supported by the VM
 */
public Locale[] getAllLocales() {
    return Locale.getAvailableLocales();
}

From source file:org.jamwiki.servlets.RegisterServlet.java

/**
 *
 *//*from  w w  w . ja  v a 2 s  .c  om*/
private void loadDefaults(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo, WikiUser user)
        throws Exception {
    if (StringUtils.isBlank(user.getDefaultLocale()) && request.getLocale() != null) {
        user.setDefaultLocale(request.getLocale().toString());
    }
    TreeMap<String, String> locales = new TreeMap<String, String>();
    Map<String, String> translations = WikiConfiguration.getInstance().getTranslations();
    for (String key : translations.keySet()) {
        String value = key + " - " + translations.get(key);
        locales.put(value, key);
    }
    Locale[] localeArray = Locale.getAvailableLocales();
    for (int i = 0; i < localeArray.length; i++) {
        String key = localeArray[i].toString();
        String value = key + " - " + localeArray[i].getDisplayName(localeArray[i]);
        locales.put(value, key);
    }
    next.addObject("locales", locales);
    Map editors = WikiConfiguration.getInstance().getEditors();
    next.addObject("editors", editors);
    next.addObject("newuser", user);
    pageInfo.setSpecial(true);
    pageInfo.setContentJsp(JSP_REGISTER);
    pageInfo.setPageTitle(new WikiMessage("register.title"));
}