Example usage for java.util Locale getVariant

List of usage examples for java.util Locale getVariant

Introduction

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

Prototype

public String getVariant() 

Source Link

Document

Returns the variant code for this locale.

Usage

From source file:org.dspace.core.I18nUtil.java

/**
 * Gets the appropriate supported Locale according for a given Locale If
 * no appropriate supported locale is found, the DEFAULTLOCALE is used
 *
 * @param locale//w  w  w  .ja va2s.  c  om
 *        Locale to find the corresponding Locale
 * @return supportedLocale
 *         Locale for session according to locales supported by this DSpace instance as set in dspace.cfg
 */

public static Locale getSupportedLocale(Locale locale) {

    Locale[] availableLocales = getSupportedLocales();
    boolean isSupported = false;
    Locale supportedLocale = null;
    String testLocale = "";
    if (availableLocales == null) {
        supportedLocale = DEFAULTLOCALE;
    } else {
        if (!locale.getVariant().equals("")) {
            testLocale = locale.toString();
            for (int i = 0; i < availableLocales.length; i++) {
                if (testLocale.equalsIgnoreCase(availableLocales[i].toString())) {
                    isSupported = true;
                    supportedLocale = availableLocales[i];
                }

            }
        }

        if (!(isSupported && locale.getCountry().equals(""))) {
            testLocale = locale.getLanguage() + "_" + locale.getCountry();

            for (int i = 0; i < availableLocales.length; i++) {
                if (testLocale.equalsIgnoreCase(availableLocales[i].toString())) {
                    isSupported = true;
                    supportedLocale = availableLocales[i];
                }
            }

        }
        if (!isSupported) {
            testLocale = locale.getLanguage();

            for (int i = 0; i < availableLocales.length; i++) {
                if (testLocale.equalsIgnoreCase(availableLocales[i].toString())) {
                    isSupported = true;
                    supportedLocale = availableLocales[i];
                }

            }
        }
        if (!isSupported) {
            supportedLocale = DEFAULTLOCALE;
        }
    }
    return supportedLocale;
}

From source file:nl.strohalm.cyclos.utils.PropertiesHelper.java

/**
 * Reads a resource bundle from a properties file encoded as utf-8
 *///from   w ww .  ja  v a 2  s  . c o  m
public static ResourceBundle readBundle(final String baseName, Locale locale) {
    if (locale == null) {
        locale = Locale.getDefault();
    }

    final List<String> suffixes = new ArrayList<String>();

    if (locale != null) {
        suffixes.add("");
        if (StringUtils.isNotEmpty(locale.getLanguage())) {
            suffixes.add("_" + locale.getLanguage());
        }
        if (StringUtils.isNotEmpty(locale.getCountry()) && StringUtils.isNotEmpty(locale.getLanguage())) {
            suffixes.add("_" + locale.getLanguage() + "_" + locale.getCountry());
        }
        if (StringUtils.isNotEmpty(locale.getVariant()) && StringUtils.isNotEmpty(locale.getCountry())
                && StringUtils.isNotEmpty(locale.getLanguage())) {
            suffixes.add("_" + locale.getLanguage() + "_" + locale.getCountry() + "_" + locale.getVariant());
        }
    }

    PropertiesResourceBundle bundle = null;
    for (final String suffix : suffixes) {
        final Properties properties = loadFromResource(baseName + suffix);
        if (properties != null) {
            final PropertiesResourceBundle current = new PropertiesResourceBundle(properties, bundle);
            bundle = current;
        }
    }
    if (bundle == null) {
        throw new IllegalArgumentException(
                "Error loading properties resource bundle for baseName=" + baseName + " and locale=" + locale);
    }
    return bundle;
}

From source file:org.pentaho.platform.util.xml.XmlHelper.java

public static InputStream getLocalizedFile(final String fullPath, final Locale locale,
        final IDocumentResourceLoader loader) {
    String language = locale.getLanguage();
    String country = locale.getCountry();
    String variant = locale.getVariant();

    // File file = new File(fullPath);

    String fileName = fullPath;/*  w  ww.j ava 2 s . com*/
    int dotIndex = fileName.indexOf('.');
    String baseName = dotIndex == -1 ? fileName : fileName.substring(0, dotIndex); // These two lines fix an
                                                                                   // index out
                                                                                   // of bounds
    String extension = dotIndex == -1 ? "" : fileName.substring(dotIndex); // Exception that occurs when a filename has no extension //$NON-NLS-1$

    InputStream in = null;
    try {
        if (!variant.equals("")) { //$NON-NLS-1$
            in = loader.loadXsl(baseName + "_" + language + "_" + country + "_" + variant + extension); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        }
        if (in == null) {
            in = loader.loadXsl(baseName + "_" + language + "_" + country + extension); //$NON-NLS-1$//$NON-NLS-2$
        }
        if (in == null) {
            in = loader.loadXsl(baseName + "_" + language + extension); //$NON-NLS-1$
        }
        if (in == null) {
            in = loader.loadXsl(baseName + extension);
        }
    } catch (Exception e) {
        Logger.error(XmlHelper.class.getName(), "Error loading localized file: " + fullPath); //$NON-NLS-1$
    }
    return in;
}

From source file:psiprobe.Utils.java

/**
 * Gets the names for locale.//from  ww w.j av a  2s  .c  o m
 *
 * @param baseName the base name
 * @param locale the locale
 * @return the names for locale
 */
public static List<String> getNamesForLocale(String baseName, Locale locale) {
    List<String> result = new ArrayList<>(3);
    String language = locale.getLanguage();
    String country = locale.getCountry();
    String variant = locale.getVariant();
    StringBuilder temp = new StringBuilder(baseName);

    if (language.length() > 0) {
        temp.append('_').append(language);
        result.add(0, temp.toString());
    }

    if (country.length() > 0) {
        temp.append('_').append(country);
        result.add(0, temp.toString());
    }

    if (variant.length() > 0) {
        temp.append('_').append(variant);
        result.add(0, temp.toString());
    }

    return result;
}

From source file:org.atricore.idbus.bundles.apache.tiles.OsgiDefinitionsFactory.java

/**
 * Calculate the postfixes along the search path from the base bundle to the
 * bundle specified by baseName and locale.
 * Method copied from java.util.ResourceBundle
 *
 * @param locale the locale/*from  w  ww  . j a v a  2  s  . c om*/
 * @return a list of
 */
protected static List<String> calculatePostfixes(Locale locale) {
    final List<String> result = new ArrayList<String>();
    final String language = locale.getLanguage();
    final int languageLength = language.length();
    final String country = locale.getCountry();
    final int countryLength = country.length();
    final String variant = locale.getVariant();
    final int variantLength = variant.length();

    // The default configuration file must be loaded to allow correct
    // definition inheritance.
    result.add("");
    if (languageLength + countryLength + variantLength == 0) {
        //The locale is "", "", "".
        return result;
    }

    final StringBuffer temp = new StringBuffer();
    temp.append('_');
    temp.append(language);

    if (languageLength > 0) {
        result.add(temp.toString());
    }

    if (countryLength + variantLength == 0) {
        return result;
    }

    temp.append('_');
    temp.append(country);

    if (countryLength > 0) {
        result.add(temp.toString());
    }

    if (variantLength == 0) {
        return result;
    } else {
        temp.append('_');
        temp.append(variant);
        result.add(temp.toString());
        return result;
    }
}

From source file:com.salesmanager.core.util.ShippingUtil.java

/**
 * Builds a Map<String,String> of services available (id, code) example
 * 01-EXPRESS SHIPPING//from  w w  w. ja v  a2 s .  c o  m
 * 
 * @param serviceline
 * @param moduleid
 * @param locale
 * @return
 */
public static Map<String, String> buildServiceMapByCode(String moduleid, Locale locale) throws Exception {

    ReferenceService rservice = (ReferenceService) ServiceFactory.getService(ServiceFactory.ReferenceService);

    String country = locale.getCountry();
    if (locale.getVariant().equals("EUR")) {
        country = "X1";
    }

    ModuleConfiguration serviceconfig = rservice.getModuleConfiguration(moduleid, "service", country);

    if (serviceconfig == null) {
        serviceconfig = rservice.getModuleConfiguration(moduleid, "service", "XX");// generic
    }

    if (serviceconfig == null) {
        throw new Exception(
                "ModuleConfiguration does not exist for " + moduleid + "-service-XX-" + locale.getCountry());
    }

    String serviceline = serviceconfig.getConfigurationValue();

    Map returnmap = getConfigurationMap(serviceline, ";", "|");

    return returnmap;

}

From source file:com.salesmanager.core.util.ShippingUtil.java

/**
 * Builds a Map<String,String> of services avilables ID, CODE from the
 * service .properties file//from w  ww.j a  va  2s . c o  m
 * 
 * @param serviceline
 * @param moduleid
 * @param locale
 * @return
 */
public static Map<String, String> buildServiceMap(String moduleid, Locale locale) throws Exception {

    ReferenceService rservice = (ReferenceService) ServiceFactory.getService(ServiceFactory.ReferenceService);

    String country = locale.getCountry();
    if (locale.getVariant().equals("EUR")) {
        country = "X1";
    }

    ModuleConfiguration serviceconfig = rservice.getModuleConfiguration(moduleid, "service", country);

    if (serviceconfig == null) {
        serviceconfig = rservice.getModuleConfiguration(moduleid, "service", "XX");// generic
    }

    if (serviceconfig == null) {
        throw new Exception("ModuleConfiguration does not exist for " + moduleid + "-service-XX-" + country);
    }

    String serviceline = serviceconfig.getConfigurationValue();

    ResourceBundle bundle = ResourceBundle.getBundle(moduleid, locale);

    List pkgids = getConfigurationList(serviceline);

    // List returnlist = new ArrayList();
    Map returnmap = new HashMap();

    Iterator it = pkgids.iterator();
    while (it.hasNext()) {
        String pkgid = (String) it.next();
        String pkg = bundle.getString("shipping.quote.services.label." + pkgid);
        returnmap.put(pkgid, pkg);
    }

    // return returnlist;
    return returnmap;
}

From source file:password.pwm.util.LocaleHelper.java

public static Locale localeResolver(final Locale desiredLocale, final Collection<Locale> localePool) {
    if (desiredLocale == null || localePool == null || localePool.isEmpty()) {
        return PwmConstants.DEFAULT_LOCALE;
    }/*from   w  w  w. j a  v a2 s  .  c  o m*/

    for (final Locale loopLocale : localePool) {
        if (loopLocale.getLanguage().equalsIgnoreCase(desiredLocale.getLanguage())) {
            if (loopLocale.getCountry().equalsIgnoreCase(desiredLocale.getCountry())) {
                if (loopLocale.getVariant().equalsIgnoreCase(desiredLocale.getVariant())) {
                    return loopLocale;
                }
            }
        }
    }

    for (final Locale loopLocale : localePool) {
        if (loopLocale.getLanguage().equalsIgnoreCase(desiredLocale.getLanguage())) {
            if (loopLocale.getCountry().equalsIgnoreCase(desiredLocale.getCountry())) {
                return loopLocale;
            }
        }
    }

    for (final Locale loopLocale : localePool) {
        if (loopLocale.getLanguage().equalsIgnoreCase(desiredLocale.getLanguage())) {
            return loopLocale;
        }
    }

    if (localePool.contains(PwmConstants.DEFAULT_LOCALE)) {
        return PwmConstants.DEFAULT_LOCALE;
    }

    if (localePool.contains(new Locale(""))) {
        return PwmConstants.DEFAULT_LOCALE;
    }

    return PwmConstants.DEFAULT_LOCALE;
}

From source file:com.salesmanager.core.util.ShippingUtil.java

/**
 * Builds a Map<String,String> of services available (id, label example
 * 01-EXPRESS SHIPPING//from   w ww. j  a v  a2s.c o  m
 * 
 * @param serviceline
 * @param moduleid
 * @param locale
 * @return
 */
public static Map<String, String> buildServiceMapLabelByCode(String moduleid, Locale locale) throws Exception {

    ReferenceService rservice = (ReferenceService) ServiceFactory.getService(ServiceFactory.ReferenceService);

    String country = locale.getCountry();
    if (locale.getVariant().equals("EUR")) {
        country = "X1";
    }

    ModuleConfiguration serviceconfig = rservice.getModuleConfiguration(moduleid, "service", country);

    if (serviceconfig == null) {
        serviceconfig = rservice.getModuleConfiguration(moduleid, "service", "XX");// generic
    }

    if (serviceconfig == null) {
        throw new Exception(
                "ModuleConfiguration does not exist for " + moduleid + "-service-XX-" + locale.getCountry());
    }

    String serviceline = serviceconfig.getConfigurationValue();

    Map amap = getConfigurationMap(serviceline, ";", "|");
    Map returnMap = new HashMap();

    ResourceBundle bundle = ResourceBundle.getBundle(moduleid, locale);

    if (amap != null) {
        Iterator i = amap.keySet().iterator();
        while (i.hasNext()) {
            String key = (String) i.next();
            String pkg = bundle.getString("shipping.quote.services.label." + key);
            returnMap.put(key, pkg);

        }
    }

    return returnMap;

}

From source file:it.cnr.icar.eric.client.xml.registry.infomodel.InternationalStringImpl.java

/**
 * Calculate the possible keys List for a locale/charset pair. Candidate keys
 * are://  w  w w  .j a v a2s.c o m
 * <ul>
 * <li> charset + "_" + language + "_" + country + "_" + variant
 * <li> charset + "_" + language + "_" + countryi
 * <li> charset + "_" + language
 * </ul>
 *
 * Candidate keys where the final component is an empty string are omitted.
 * For example, if country is an empty string, the second candidate key is omitted.
 *
 * @param charset the charset name
 * @param locale the locale
 * @return List with the keys along the search path.
 */
private static List<String> calculateKeys(Locale locale, String charset) {
    final ArrayList<String> result = new ArrayList<String>(3);
    final String language = locale.getLanguage();
    final int languageLength = language.length();
    final String country = locale.getCountry();
    final int countryLength = country.length();
    final String variant = locale.getVariant();
    final int variantLength = variant.length();

    if ((languageLength + countryLength + variantLength) == 0) {
        //The locale is "", "", "".
        return result;
    }

    final StringBuffer temp = new StringBuffer(charset);
    temp.append('_');
    temp.append(language);

    if (languageLength > 0) {
        result.add(0, temp.toString());
    }

    if ((countryLength + variantLength) == 0) {
        return result;
    }

    temp.append('_');
    temp.append(country);

    if (countryLength > 0) {
        result.add(0, temp.toString());
    }

    if (variantLength == 0) {
        return result;
    }

    temp.append('_');
    temp.append(variant);
    result.add(0, temp.toString());

    return result;
}