Closed
Description
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:
- 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);
}
- 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:
- Support for HTTP Vary configuration (e.g. in reaction to locale-based rendering) [SPR-14070] #18642 Support for HTTP Vary configuration (e.g. in reaction to locale-based rendering)
- AcceptHeaderLocaleResolver should allow to define a default Locale [SPR-14312] #18884 AcceptHeaderLocaleResolver should allow to define a default Locale
- Revise AcceptHeaderLocaleResolver default locale handling [SPR-15426] #19987 Revise AcceptHeaderLocaleResolver default locale handling
Referenced from: commits b3d67b9