Skip to content

Flexible locale resolution based on supported languages [SPR-13330] #17915

Closed
@spring-projects-issues

Description

@spring-projects-issues

Yi EungJun opened SPR-13330 and commented

When an HTTP client prefers a specific language, the server can return a message in the language as follows:

@Autowired
private MessageSource messageSource;

public String say (Locale locale) {
    return messageSource.getMessage("my.message", null, locale);
}

But Here is a difficult case. If a client requests with many acceptable languages by Accept-Language header:

Accept-Language: ko, ja;q=0.9, en;q=0.8; *;q=0.1

and the server supports many languages, the server should find the best match. In this case, if the server supports 'ja' and 'en', the best match is 'ja'.

But it seems that Spring Framework does not provide a way to do that. It would be nice if it supports that as the following examples:

  1. getMessage() accepts a list of locales and choose the best one
@Autowired
private MessageSource messageSource;

public String say1 (Collection<Locale> locales) {
    return messageSource.getMessage("my.message", null, locales);
}
  1. MessageSource has a method to lookup the best locale as java.util.Locale does.
public String say2(Collection<Locale> locales) {
    Locale locale = messageSource.lookupBestLocale(locales);
    return messageSource.getMessage("my.message", null, locale);
}

Issue Links:

Referenced from: commits b3d67b9

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions