开发者

Different i18n in spring according to url

开发者 https://www.devze.com 2022-12-31 13:13 出处:网络
I have a spring web application that is required to work as following the application will be accessed from two different URLs www.domain1.com and www.domain2.com

I have a spring web application that is required to work as following

the application will be accessed from two different URLs www.domain1.com and www.domain2.com

and it is required that the two URLs looks like two different applications with different CSS and I18n.

for the css part is done but I am stuck with the i18n part

How to make spring load different i18n properties file according to the domain name?

The solution that I thought in is to implement a filter that check the request URL and according to the URL it clears the message source bean and load the required i18n file but 开发者_运维技巧it does not looks good for the performance

by the way I am using ReloadableResourceBundleMessageSource message source

Another solution is to implement two different message sources. The problem with this solution is that from the source code I can manage the bean that I use but how can I tell the fmt:message tag which data source to use ?

Thanks in advance and best regards


I suggest using a LocaleResolver. This is a standard Spring interface for doing exactly this sort of thing.

Interface for web-based locale resolution strategies that allows for both locale resolution via the request and locale modification via request and response.

This interface allows for implementations based on request, session, cookies, etc.

The pre-defined implementations of LocaleResolver don't do what you need, but it's trivial to write your own. Your implementation would be asked by to determine the locale for each request, and this information is then used by Spring's i18n code, including ReloadableResourceBundleMessageSource. You just declare your LocaleResolver bean in the context, and it's picked up automatically.

Your resource bundles would then use standard java locale mechanism to resolve the correct message for the current locale.

0

精彩评论

暂无评论...
验证码 换一张
取 消