CookieLocaleResolver is RFC6265 and language tag compliant by default

Like CookieLocaleResolver, LocaleChangeInterceptor parses both locale formats by default now. Since it does not need to render the locale, its languageTagCompliant property is not relevant anymore at all.

The parseLocale method in StringUtils validates the locale value now and turns an empty locale into null, compatible with parseLocaleString behavior and in particular aligned with web locale parsing needs.

Issue: SPR-16700
Issue: SPR-16651
This commit is contained in:
Juergen Hoeller
2018-07-17 17:57:59 +02:00
parent 955665b419
commit 88e4006790
5 changed files with 68 additions and 51 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.core.convert.support;
import java.util.Locale;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/**
@@ -35,6 +36,7 @@ import org.springframework.util.StringUtils;
final class StringToLocaleConverter implements Converter<String, Locale> {
@Override
@Nullable
public Locale convert(String source) {
return StringUtils.parseLocale(source);
}

View File

@@ -772,7 +772,9 @@ public abstract class StringUtils {
public static Locale parseLocale(String localeValue) {
String[] tokens = tokenizeLocaleSource(localeValue);
if (tokens.length == 1) {
return Locale.forLanguageTag(localeValue);
validateLocalePart(localeValue);
Locale resolved = Locale.forLanguageTag(localeValue);
return (resolved.getLanguage().length() > 0 ? resolved : null);
}
return parseLocaleTokens(localeValue, tokens);
}
@@ -821,7 +823,7 @@ public abstract class StringUtils {
private static void validateLocalePart(String localePart) {
for (int i = 0; i < localePart.length(); i++) {
char ch = localePart.charAt(i);
if (ch != ' ' && ch != '_' && ch != '#' && !Character.isLetterOrDigit(ch)) {
if (ch != ' ' && ch != '_' && ch != '-' && ch != '#' && !Character.isLetterOrDigit(ch)) {
throw new IllegalArgumentException(
"Locale part \"" + localePart + "\" contains invalid characters");
}