Skip to content

Spring MVC in runtime fails when the locale is changed according with BCP 47 [SPR-16775] #21315

Closed
@spring-projects-issues

Description

@spring-projects-issues

Manuel Jordan opened SPR-16775 and commented

I have the following infrastructure:

@Bean
public LocaleChangeInterceptor localeChangeInterceptor(){
	LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
	localeChangeInterceptor.setParamName("language");
	return localeChangeInterceptor;
}

@Bean(name="localeResolver")
public LocaleResolver cookieLocaleResolver(){
	CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
	cookieLocaleResolver.setCookieName("language");
	cookieLocaleResolver.setDefaultLocale(Locale.forLanguageTag("en-US"));
	return cookieLocaleResolver;
}

I have confirmed that in runtime the following works:

(A): [http://localhost:8080/thymeleaf-02/locale/changelocale?language=es_ES]
Observe that the URL uses _, and the app works.

(B) [http://localhost:8080/thymeleaf-02/locale/changelocale?language=es-ES]
Observe that the URL uses -, and the app fails with:

HTTP Status 500 – Internal Server Error
Type Exception Report

Message Request processing failed; nested exception is java.lang.IllegalArgumentException: Locale part "es-ES" contains invalid characters

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Locale part "es-ES" contains invalid characters
	
...

Root Cause

java.lang.IllegalArgumentException: Locale part "es-ES" contains invalid characters
	org.springframework.util.StringUtils.validateLocalePart(StringUtils.java:817)

According with my experience in: #20999

Is valid use - and not _. Currently my @Test methods pass with - but fails in runtime.
Is it a bug? Or missing configuration? Because I think runtime and testing should have the same behavior.

I know that BCP 47 for this case is about the Accept-Language header and the URL is working through a parameter. But I hope you see my point.

Not sure if it is the expected behavior, but could be confuse for the developer.

Thank You.


Affects: 5.0.4

Issue Links:

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: invalidAn issue that we don't feel is valid

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions