-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Flexible locale resolution based on supported languages [SPR-13330] #17915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Rossen Stoyanchev commented In the top example above, the Locale is resolved based on the configured In short this should already be working. Have you tried and is there an issue? |
Yi EungJun commented I already used AcceptHeaderLocaleResolver but it does not work as I want. If a client requests with "Accept-Language: ko, ja;q=0.9" and my server has messages.properties (English) and messages_ko.properties, it works as I want. But if a client requests with the same header and my server has messages.properties (English) and messages_ja.properties, but not messages_ko.properties, it does not work as I want. It returns an English message instead of a "ja" message which I want. AcceptHeaderLocalResolver.resolveLocale() returns "ko", which the user mostly prefers, but the server returns an English message, not a "ko" message, because it does not have messages_ko.properties. In such a case, the better way is response with "ja", the secondary preferred language. So we need a way to resolve a best-match language. Both of AcceptHeaderLocaleResolver.resolveLocale() and ServletRequest.getLocale() considers only the client's Accept-Language header. But it is not enough. The better language-negotiation should consider also the server's language capability. I guess this issue can be resolved by adding new implementation of LocaleResolver, which considers both of client's requirement and server's capability. |
Rossen Stoyanchev commented Okay I see. It looks like ServletRequest also has getLocales which returns an enumeration with all locales in order of preference. We could extend the existing AcceptHeaderLocaleResolver to be configured with a list of locales that are available and then apply that against the list of requested locales. I don't see any reliable way to automatically determine what locales are supported. ReloadableResourceBundleMessageSource is configured with basenames of resource bundles. It could try to find on start up what's available but each basename could have different locales. Also what's available may change at any time by additional files being added. The most viable path I see is a configuration property on AcceptHeaderLocaleResolver. |
Yi EungJun commented Thanks for your effort! Feel free to let me know if there is anything I help you. |
Rossen Stoyanchev commented There is a proposed fix for this now in master (4.3 snapshot). Please give that a try if you can. |
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:
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:
Issue Links:
Referenced from: commits b3d67b9
The text was updated successfully, but these errors were encountered: