Java Code Examples for java.util.Locale#LanguageRange

The following examples show how to use java.util.Locale#LanguageRange . 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 File: TextClassifierImpl.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * 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 2
Source File: Bug8035133.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: HttpHeadersTests.java    From spring-analysis-note with 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 4
Source File: HttpHeaders.java    From java-technology-stack with 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 5
Source File: SimpleController.java    From tutorials with 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 6
Source File: HttpHeaders.java    From java-technology-stack with 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 File: HttpHeaders.java    From spring-analysis-note with 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 8
Source File: TestConfiguration.java    From archiva with Apache License 2.0 4 votes vote down vote up
@Override
public List<Locale.LanguageRange> getLanguagePriorities( )
{
    return Locale.LanguageRange.parse("en,fr,de");
}
 
Example 9
Source File: MockServerRequest.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public List<Locale.LanguageRange> acceptLanguage() {
	return delegate().getAcceptLanguage();
}
 
Example 10
Source File: TextClassifierImpl.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/** 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 11
Source File: DefaultServerRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public List<Locale.LanguageRange> acceptLanguage() {
	return delegate().getAcceptLanguage();
}
 
Example 12
Source File: ServerRequestWrapper.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public List<Locale.LanguageRange> acceptLanguage() {
	return this.headers.acceptLanguage();
}
 
Example 13
Source File: MockServerRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public List<Locale.LanguageRange> acceptLanguage() {
	return delegate().getAcceptLanguage();
}
 
Example 14
Source File: ServerRequestWrapper.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public List<Locale.LanguageRange> acceptLanguage() {
	return this.headers.acceptLanguage();
}
 
Example 15
Source File: HttpHeaders.java    From spring-analysis-note with 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 16
Source File: ServerRequest.java    From spring-analysis-note with 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 File: ServerRequest.java    From java-technology-stack with 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 18
Source File: HttpHeaders.java    From java-technology-stack with 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 19
Source File: ServerRequest.java    From spring-analysis-note with 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 20
Source File: ArchivaConfiguration.java    From archiva with Apache License 2.0 votes vote down vote up
public List<Locale.LanguageRange> getLanguagePriorities();