Java Code Examples for java.util.Locale#LanguageRange

The following examples show how to use java.util.Locale#LanguageRange . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   File: HttpHeadersTests.java    License: MIT License 6 votes vote down vote up
@Test
public void acceptLanguage() {
	String headerValue = "fr-ch, fr;q=0.9, en-*;q=0.8, de;q=0.7, *;q=0.5";
	headers.setAcceptLanguage(Locale.LanguageRange.parse(headerValue));
	assertEquals(headerValue, headers.getFirst(HttpHeaders.ACCEPT_LANGUAGE));

	List<Locale.LanguageRange> expectedRanges = Arrays.asList(
			new Locale.LanguageRange("fr-ch"),
			new Locale.LanguageRange("fr", 0.9),
			new Locale.LanguageRange("en-*", 0.8),
			new Locale.LanguageRange("de", 0.7),
			new Locale.LanguageRange("*", 0.5)
	);
	assertEquals(expectedRanges, headers.getAcceptLanguage());
	assertEquals(Locale.forLanguageTag("fr-ch"), headers.getAcceptLanguageAsLocales().get(0));

	headers.setAcceptLanguageAsLocales(Collections.singletonList(Locale.FRANCE));
	assertEquals(Locale.FRANCE, headers.getAcceptLanguageAsLocales().get(0));
}
 
Example 2
private static void checkLookup(String ranges, String tags,
        String expectedLocale) {

    List<Locale.LanguageRange> priorityList = Locale.LanguageRange
            .parse(ranges);
    List<Locale> localeList = generateLocales(tags);
    Locale loc = Locale.lookup(priorityList, localeList);
    String actualLocale
            = loc.toLanguageTag();

    if (!actualLocale.equals(expectedLocale)) {
        System.err.println("Locale.lookup failed with ranges: " + ranges
                + " Expected: " + expectedLocale
                + " Actual: " + actualLocale);
        err = true;
    }

}
 
Example 3
/**
 * Finds the most appropriate model to use for the given target locale list.
 *
 * The basic logic is: we ignore all models that don't support any of the target locales. For
 * the remaining candidates, we take the update model unless its version number is lower than
 * the factory version. It's assumed that factory models do not have overlapping locale ranges
 * and conflict resolution between these models hence doesn't matter.
 */
@GuardedBy("mLock") // Do not call outside this lock.
@Nullable
private ModelFile findBestModelLocked(LocaleList localeList) {
    // Specified localeList takes priority over the system default, so it is listed first.
    final String languages = localeList.isEmpty()
            ? LocaleList.getDefault().toLanguageTags()
            : localeList.toLanguageTags() + "," + LocaleList.getDefault().toLanguageTags();
    final List<Locale.LanguageRange> languageRangeList = Locale.LanguageRange.parse(languages);

    ModelFile bestModel = null;
    for (ModelFile model : listAllModelsLocked()) {
        if (model.isAnyLanguageSupported(languageRangeList)) {
            if (model.isPreferredTo(bestModel)) {
                bestModel = model;
            }
        }
    }
    return bestModel;
}
 
Example 4
Source Project: tutorials   File: SimpleController.java    License: MIT License 5 votes vote down vote up
@GetMapping("/slf4j-guide-locale-request")
public String clientLocaleRequest(@RequestHeader("Accept-Language") String localeHeader) {
    List<Locale.LanguageRange> list = Locale.LanguageRange.parse(localeHeader);
    Locale locale = Locale.lookup(list, Arrays.asList(Locale.getAvailableLocales()));
    IMessageConveyor messageConveyor = new MessageConveyor(locale);
    LocLoggerFactory llFactory = new LocLoggerFactory(messageConveyor);
    LocLogger locLogger = llFactory.getLocLogger(this.getClass());
    locLogger.info(Messages.CLIENT_REQUEST, "parametrizedClientId", localeHeader);
    locLogger.debug(Messages.REQUEST_STARTED);
    locLogger.info(Messages.REQUEST_FINISHED);
    return "finished";
}
 
Example 5
Source Project: spring-analysis-note   File: HttpHeaders.java    License: MIT License 5 votes vote down vote up
/**
 * Set the acceptable language ranges, as specified by the
 * {@literal Accept-Language} header.
 * @since 5.0
 */
public void setAcceptLanguage(List<Locale.LanguageRange> languages) {
	Assert.notNull(languages, "LanguageRange List must not be null");
	DecimalFormat decimal = new DecimalFormat("0.0", DECIMAL_FORMAT_SYMBOLS);
	List<String> values = languages.stream()
			.map(range ->
					range.getWeight() == Locale.LanguageRange.MAX_WEIGHT ?
							range.getRange() :
							range.getRange() + ";q=" + decimal.format(range.getWeight()))
			.collect(Collectors.toList());
	set(ACCEPT_LANGUAGE, toCommaDelimitedString(values));
}
 
Example 6
Source Project: java-technology-stack   File: HttpHeaders.java    License: MIT License 5 votes vote down vote up
/**
 * Set the acceptable language ranges, as specified by the
 * {@literal Accept-Language} header.
 * @since 5.0
 */
public void setAcceptLanguage(List<Locale.LanguageRange> languages) {
	Assert.notNull(languages, "LanguageRange List must not be null");
	DecimalFormat decimal = new DecimalFormat("0.0", DECIMAL_FORMAT_SYMBOLS);
	List<String> values = languages.stream()
			.map(range ->
					range.getWeight() == Locale.LanguageRange.MAX_WEIGHT ?
							range.getRange() :
							range.getRange() + ";q=" + decimal.format(range.getWeight()))
			.collect(Collectors.toList());
	set(ACCEPT_LANGUAGE, toCommaDelimitedString(values));
}
 
Example 7
Source Project: java-technology-stack   File: HttpHeaders.java    License: MIT License 5 votes vote down vote up
/**
 * A variant of {@link #getAcceptLanguage()} that converts each
 * {@link java.util.Locale.LanguageRange} to a {@link Locale}.
 * @return the locales or an empty list
 * @throws IllegalArgumentException if the value cannot be converted to a locale
 * @since 5.0
 */
public List<Locale> getAcceptLanguageAsLocales() {
	List<Locale.LanguageRange> ranges = getAcceptLanguage();
	if (ranges.isEmpty()) {
		return Collections.emptyList();
	}
	return ranges.stream()
			.map(range -> Locale.forLanguageTag(range.getRange()))
			.filter(locale -> StringUtils.hasText(locale.getDisplayName()))
			.collect(Collectors.toList());
}
 
Example 8
@Override
public List<Locale.LanguageRange> acceptLanguage() {
	return this.headers.acceptLanguage();
}
 
Example 9
Source Project: spring-analysis-note   File: MockServerRequest.java    License: MIT License 4 votes vote down vote up
@Override
public List<Locale.LanguageRange> acceptLanguage() {
	return delegate().getAcceptLanguage();
}
 
Example 10
Source Project: archiva   File: TestConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public List<Locale.LanguageRange> getLanguagePriorities( )
{
    return Locale.LanguageRange.parse("en,fr,de");
}
 
Example 11
/** Returns whether the language supports any language in the given ranges. */
boolean isAnyLanguageSupported(List<Locale.LanguageRange> languageRanges) {
    return mLanguageIndependent || Locale.lookup(languageRanges, mSupportedLocales) != null;
}
 
Example 12
@Override
public List<Locale.LanguageRange> acceptLanguage() {
	return delegate().getAcceptLanguage();
}
 
Example 13
@Override
public List<Locale.LanguageRange> acceptLanguage() {
	return this.headers.acceptLanguage();
}
 
Example 14
@Override
public List<Locale.LanguageRange> acceptLanguage() {
	return delegate().getAcceptLanguage();
}
 
Example 15
Source Project: spring-analysis-note   File: ServerRequest.java    License: MIT License 2 votes vote down vote up
/**
 * Get the list of acceptable languages, as specified by the
 * {@code Accept-Language} header.
 */
List<Locale.LanguageRange> acceptLanguage();
 
Example 16
Source Project: spring-analysis-note   File: ServerRequest.java    License: MIT License 2 votes vote down vote up
/**
 * Get the list of acceptable languages, as specified by the
 * {@code Accept-Language} header.
 */
List<Locale.LanguageRange> acceptLanguage();
 
Example 17
Source Project: spring-analysis-note   File: HttpHeaders.java    License: MIT License 2 votes vote down vote up
/**
 * Return the language ranges from the {@literal "Accept-Language"} header.
 * <p>If you only need sorted, preferred locales only use
 * {@link #getAcceptLanguageAsLocales()} or if you need to filter based on
 * a list of supported locales you can pass the returned list to
 * {@link Locale#filter(List, Collection)}.
 * @throws IllegalArgumentException if the value cannot be converted to a language range
 * @since 5.0
 */
public List<Locale.LanguageRange> getAcceptLanguage() {
	String value = getFirst(ACCEPT_LANGUAGE);
	return (StringUtils.hasText(value) ? Locale.LanguageRange.parse(value) : Collections.emptyList());
}
 
Example 18
Source Project: java-technology-stack   File: ServerRequest.java    License: MIT License 2 votes vote down vote up
/**
 * Get the list of acceptable languages, as specified by the
 * {@code Accept-Language} header.
 */
List<Locale.LanguageRange> acceptLanguage();
 
Example 19
Source Project: java-technology-stack   File: HttpHeaders.java    License: MIT License 2 votes vote down vote up
/**
 * Return the language ranges from the {@literal "Accept-Language"} header.
 * <p>If you only need sorted, preferred locales only use
 * {@link #getAcceptLanguageAsLocales()} or if you need to filter based on
 * a list of supported locales you can pass the returned list to
 * {@link Locale#filter(List, Collection)}.
 * @throws IllegalArgumentException if the value cannot be converted to a language range
 * @since 5.0
 */
public List<Locale.LanguageRange> getAcceptLanguage() {
	String value = getFirst(ACCEPT_LANGUAGE);
	return (StringUtils.hasText(value) ? Locale.LanguageRange.parse(value) : Collections.emptyList());
}
 
Example 20
Source Project: archiva   File: ArchivaConfiguration.java    License: Apache License 2.0 votes vote down vote up
public List<Locale.LanguageRange> getLanguagePriorities();