Example usage for java.util Locale getLanguage

List of usage examples for java.util Locale getLanguage

Introduction

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

Prototype

public String getLanguage() 

Source Link

Document

Returns the language code of this Locale.

Usage

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

public static String getProductName(Product product, Locale locale) {

    try {/* ww w  .  ja v  a 2  s. c  o  m*/

        Set descriptionset = product.getDescriptions();
        int lang = LanguageUtil.getLanguageNumberCode(locale.getLanguage());
        if (descriptionset != null) {
            Iterator i = descriptionset.iterator();
            while (i.hasNext()) {
                ProductDescription desc = (ProductDescription) i.next();
                if (desc.getId().getLanguageId() == lang) {
                    return desc.getProductName();
                }
            }
        }

    } catch (Exception e) {
        log.error(e);
    }

    return "";

}

From source file:hu.petabyte.redflags.web.ctrl.LangAdvice.java

@ModelAttribute("lang")
public String lang(Locale loc) {
    return loc.getLanguage();
}

From source file:I18NUtil.java

/**
 * Searches for the nearest locale from the available options.  To match any locale, pass in
 * <tt>null</tt>./*www  .  ja  v a  2  s . c  o  m*/
 * 
 * @param templateLocale the template to search for or <tt>null</tt> to match any locale
 * @param options the available locales to search from
 * @return Returns the best match from the available options, or the <tt>null</tt> if
 *      all matches fail
 */
public static Locale getNearestLocale(Locale templateLocale, Set<Locale> options) {
    if (options.isEmpty()) // No point if there are no options
    {
        return null;
    } else if (templateLocale == null) {
        for (Locale locale : options) {
            return locale;
        }
    } else if (options.contains(templateLocale)) // First see if there is an exact match
    {
        return templateLocale;
    }
    // make a copy of the set
    Set<Locale> remaining = new HashSet<Locale>(options);

    // eliminate those without matching languages
    Locale lastMatchingOption = null;
    String templateLanguage = templateLocale.getLanguage();
    if (templateLanguage != null && !templateLanguage.equals("")) {
        Iterator<Locale> iterator = remaining.iterator();
        while (iterator.hasNext()) {
            Locale option = iterator.next();
            if (option != null && !templateLanguage.equals(option.getLanguage())) {
                iterator.remove(); // It doesn't match, so remove
            } else {
                lastMatchingOption = option; // Keep a record of the last match
            }
        }
    }
    if (remaining.isEmpty()) {
        return null;
    } else if (remaining.size() == 1 && lastMatchingOption != null) {
        return lastMatchingOption;
    }

    // eliminate those without matching country codes
    lastMatchingOption = null;
    String templateCountry = templateLocale.getCountry();
    if (templateCountry != null && !templateCountry.equals("")) {
        Iterator<Locale> iterator = remaining.iterator();
        while (iterator.hasNext()) {
            Locale option = iterator.next();
            if (option != null && !templateCountry.equals(option.getCountry())) {
                // It doesn't match language - remove
                // Don't remove the iterator. If it matchs a langage but not the country, returns any matched language                     
                // iterator.remove();
            } else {
                lastMatchingOption = option; // Keep a record of the last match
            }
        }
    }
    /*if (remaining.isEmpty())
    {
    return null;
    }
    else */
    if (remaining.size() == 1 && lastMatchingOption != null) {
        return lastMatchingOption;
    } else {
        // We have done an earlier equality check, so there isn't a matching variant
        // Also, we know that there are multiple options at this point, either of which will do.

        // This gets any country match (there will be worse matches so we take the last the country match)
        if (lastMatchingOption != null) {
            return lastMatchingOption;
        } else {
            for (Locale locale : remaining) {
                return locale;
            }
        }
    }
    // The logic guarantees that this code can't be called
    throw new RuntimeException("Logic should not allow code to get here.");
}

From source file:ca.sqlpower.dao.session.LocaleConverter.java

@Override
public String convertToSimpleType(Locale convertFrom, Object... additionalInfo) {
    return convertFrom.getLanguage() + SEPARATOR + convertFrom.getCountry() + SEPARATOR
            + convertFrom.getVariant();/* ww w  .  ja  v  a  2  s  .com*/
}

From source file:fi.helsinki.opintoni.domain.LocalizedText.java

public String getByLocale(Locale locale) {
    Language language = Language.fromCode(locale.getLanguage());
    return localizations.get(language);
}

From source file:cn.edu.zjnu.acm.judge.controller.ProblemController.java

@GetMapping(value = "{id}", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<?> findOne(@PathVariable("id") long id, Locale locale) {
    Problem problem = problemMapper.findOne(id, locale.getLanguage());
    return problem != null ? ResponseEntity.ok(problem) : ResponseEntity.notFound().build();
}

From source file:com.terradue.jcatalogue.client.internal.converters.LocaleConverter.java

public Object convert(@SuppressWarnings("rawtypes") Class type, Object value) {
    if (value == null) {
        throw new ConversionException("Null values not supported in this version.");
    }/*from   ww  w  . ja  v  a2s .c o  m*/

    if (String.class == type) {
        if (value instanceof Locale) {
            Locale locale = (Locale) value;
            return locale.getLanguage() + SEPARATOR + locale.getCountry();
        }
    } else if (Locale.class == type) {
        if (value instanceof String) {
            StringTokenizer tokenizer = new StringTokenizer((String) value, SEPARATOR);
            return new Locale(tokenizer.nextToken(), tokenizer.nextToken());
        }
    }
    throw new ConversionException(format("type %s and value %s not supported", type, value));
}

From source file:fi.helsinki.opintoni.service.converter.CoursePageUriLocalizer.java

public String localize(String originalUrl) {
    Locale locale = LocaleContextHolder.getLocale();
    return localizers.get(locale.getLanguage()).apply(stripLanguageFromUrl(originalUrl));
}

From source file:cn.vlabs.duckling.vwb.ui.rsi.profile.SiteLanguageService.java

public Object getLanguage(RestSession session, Object message) throws ServiceException {
    LanguageRequest request = (LanguageRequest) message;
    VWBContainer container = VWBContainerImpl.findContainer();
    String language = container.getSiteConfig().getProperty(request.getSiteId(), KeyConstants.SITE_LANGUAGE);
    if (StringUtils.isEmpty(language)) {
        Locale locale = this.getRequest().getLocale();
        language = locale.getLanguage() + "_" + locale.getCountry();
    }//from   w  w w .  j  a  va 2  s. c o  m

    return language;
}

From source file:fi.helsinki.opintoni.integration.flamma.FlammaRestClient.java

public Feed getStudentFeed(Locale locale) {
    String uri = getFeedUri(studentFeedsByLocale.get(locale.getLanguage()));
    return restTemplate.getForObject(uri, Feed.class);
}