Skip to content

Need to be able to call setLocale from within resolveLocale in custom LocaleResolver [SPR-10618] #15246

Closed
@spring-projects-issues

Description

@spring-projects-issues

Nick Williams opened SPR-10618 and commented

I want to be able to do this in a custom LocaleResolver:

  • Use the logged-in Principal's locale, if he has one.
  • If not, use the locale specified in a cookie, if it exists.
  • If not, use the Accept-Language header.

That in itself isn't that difficult. However, I also want to detect if the Principal's locale has been changed (for example, on a user profile settings page) and, if it has, update the cookie. The only way to do this is by calling the setLocale method from the resolveLocale method. For example:

public class UserCookieHeaderLocaleResolver extends CookieLocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        Locale locale = super.resolveLocale(request);

        Principal user = request.getUserPrincipal();
        if(user != null && user instanceof FooPrincipal) {
            Locale userLocale = ((FooPrincipal)user).getLocale();
            if(userLocale != null && !userLocale.equals(locale)) {
                locale = userLocale;
                this.setLocale(request, ???, userLocale);
            }
        }

        return locale;
    }
}

However, there is the obvious problem that the resolveLocale method has no access to the HttpServletResponse. It would be great if this paramater could be added to the resolveLocale signature. The change itself is trivial, but the impact is major, hence why I marked this a major improvement.

I know interfaces can change between major versions, but I'm not sure how willing the community is going to be to change this particular method.

Thoughts?


Affects: 4.0 M1

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: bulk-closedAn outdated, unresolved issue that's closed in bulk as part of a cleaning process

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions