Skip to content

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

Closed

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:

Activity

  1. added
    type: bug
    status: waiting-for-triage
    in: web
    and removed
    type: bug
    on Jan 11, 2019
  2. virmundi commented on Jan 26, 2019

    @virmundi

    I have the same issue with Spring Boot release 2.1.2.RELEASE. If I revert to 2.0.0.RELEASE it works fine.

  3. charly077 commented on Jul 8, 2019

    @charly077

    We have a similar issue with release 2.0.6.RELEASE. Is it fixed on the newest version?

  4. sbrannen commented on Jul 9, 2019

    @sbrannen
    Member

    Since Spring Framework 5.1, this should no longer be an issue (see commit 88e4006).

    From Spring Framework 4.3 to 5.0, you would need to set the languageTagCompliant property of LocaleChangeInterceptor to true for BCP 47 compliance (see commit 0dd320f).

    I am therefore closing this issue.

    However, if you find that the above is not the case, please add a comment here with details.

  5. added
    status: invalid
    and removed
    status: waiting-for-triage
    on Jul 9, 2019
  6. self-assigned this
    on Jul 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webstatus: invalid

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions