We are developing a java web application using Spring MVC with JSON at my presentation layer.
For displaying the UI labels in JSP instead of hard-cording we are getting it from the properties file and in the JSP represent we are using spring:message tag. We are using ResourceBundleMessageSource to achieve this.This is working fine.
But our application will be accessed from various region and based on the region we have different properties file for UI labels which needs to be used. Keys in all the properties file will be same only the value will be diffrent.
For eg: Say there are 2 reqions ‘Region1’ & ‘Region2’ from where the application will be accessed by the user, i have 2 properties file such as message_region1.properties & message_region2.properties. Now when the user log in from Region1 i need to pick message_Region1.properties and if the user log-in from ‘Region2’ i need to pick message_Region2.properties and display the UI labels. Since all the keys in both the properties are same there wont be any changes in my jsp files.
Any advice how to achieve it?
Simple:)
Create your
message_region1.propertiesandmessage_region2.propertiesfiles. And Spring picks up the appropriate property file depending on the client’s locale (using DefaultLocaleResolver). The property files generally are in the formatfilename_<language>_<country>.propertiesAnd if you want to tell the Spring, how the locale should be resolved, use one of the 4 approaches given in spring documentation
EDIT:
To dynamically change the property file, use one of the below ways; I don’t know if they are better than the 4 approaches (mentioned in above spec).
Just before returning the view, change the locale to the “unique code” of the logged in user, as below.
Change your message source in spring config to ReloadableResourceBundleMessageSource
And reset properties file name in controller, before returning the view, as below: