Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: internationalization - messageSource

  1. #1
    Join Date
    Apr 2005
    Location
    Austria
    Posts
    17

    Default internationalization - messageSource

    hi!

    I have a webapplication where I would like to offer a dropdown menue for the user - where he can select his language -> and then all information on the website is given in the selected language. but I don't know how I can exactly realize this. I thought I can provide for each language a seperate messages_langueage.properites file. But how can I put this on my applicationContext.xml or how can I change the messages_language.properties file in the applicationContext.xml file by selecting a value from a drop down menue?

    my current messageSource in my applicationContext.xml file looks like this:
    Code:
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    		<property name="basename"><value>messages</value></property>										
    	</bean>
    tia, lana

  2. #2
    Join Date
    Apr 2005
    Location
    Porto Alegre, RS, Brazil
    Posts
    14

    Default

    Hi Lana.

    Some weeks ago I had the same questions you're having now. Then, I just took a look at the "countries" application, which is in the "samples" folder of "springframewor with dependencies". That application helped me so much.

    But, in case you don't have time to study that application, I'll try to give you some tips, that, I hope, may be helpful.

    When thinking about spring multi-language applications, besides the "ResourceBundleMessageSource", you also must have a "ResourceBundleViewResolver". You must declare this bean in the applicationContext.xml, setting its "basenames" property. This property refers to a simple properties file, which defines some properties of the view that is being resolved.

    Code:
    <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
    		<property name="basename"><value>your_properties_file</value></property>
    Properties file example:
    Code:
    modelView.class=org.springframework.web.servlet.view.JstlView
    modelView.url=/WEB-INF/views/jsp/countries/model.jsp
    Well, besides that, you must have a localeResolver bean declared in your appcontext, just like that:

    Code:
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
    And a locale change interceptor:

    Code:
    	<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>

    After that, you have to declar a "interceptors" property in your urlMapping bean, like this:

    Code:
    	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="interceptors">
    			<list>
    				<ref local="localeChangeInterceptor"/>
    			</list>
    		</property>
    		<property name="mappings">
    			<props>
    ...
    Basically, these are the steps to be followed.

    One more thing only: For changing your application's language during its runtime, all you have to do is set a "locale" attribute in some request. Something like that:

    Code:
    <jsp&#58;forward page="/urlToBeResoved.html?locale=en_US"/>
    I hope I could help you.

    Mauricio

  3. #3
    Join Date
    Apr 2005
    Location
    Austria
    Posts
    17

    Default store locale variable

    hi mauricio!

    thx for your detailed description!
    It works fine - but is it really necessary to add the locale param to all my requests? How (or where) can I store the locale the user has selected, so that I needn't add it to all my requests?

    tia lana

  4. #4
    Join Date
    Apr 2005
    Location
    Porto Alegre, RS, Brazil
    Posts
    14

    Default

    Hi Lana,

    It's not necessary to add the "locale" param in every request. You just need to add it once, at the first time, or when you want your application language to be changed.

    Once you have added a locale param in the url, it becomes automatically "saved" in your session by the SessionLocaleResolver.

    Well,

    I hope it works!

    Mauricio

  5. #5
    Join Date
    Apr 2005
    Location
    Austria
    Posts
    17

    Default

    Mauricio thx for your quick reply, but I still hava a problem.

    When I start my webapp the locale variable is GERMAN.
    Then I change it to ENGLISH -> so the english messages.properties file is taken for displaying (but only 1x)...and when I make any other request -> the locale changes back to german - and the german messages.properties file is displayed. The same problem occures during testing springs Countries application.

    might there be an other way how I can solve my probelm so that the user can select a prefered language in a drop down menue -> and then the messages.properties file would be exchanged in the application context file?

    tia lana

  6. #6
    Join Date
    Apr 2005
    Location
    Austria
    Posts
    17

    Default internationalization problem solved

    strange - but today I tried it again and now it successfully works ; )

    thx, lana

  7. #7

    Default

    Hi!

    Interesting thread!

    I have implemented the locale change as a dropdown on my application.
    This dropdown resids in a menu.jsp.

    I want to be able to change locale wherever I am in the application.

    I have implemented a localeController that extends SimpleFormController and have the method hendleRequest() that do the:

    RequestContextUtils.getLocaleResolver(request).set Locale(request, response, new Locale(reqeust.getParameter('lang')));

    After this the method have to return a new ModelAndView("hardcodeview.htm");

    I would like the user to return to the page where he resided on when he changed the language through the dropdown and not to a hardcoded ModelAndView.

    Anyone have suggestion?
    Thanks
    /M

  8. #8
    Join Date
    Jun 2006
    Posts
    109

    Default

    This way forces to rewrite each jsp page
    Is it really the best way to internationalize a web site? messageBundle isn't more convenient?
    I don't get the interest of this way of internationalize
    Last edited by Chaps; Jun 23rd, 2006 at 08:36 AM.

  9. #9

    Default

    Hi Mauricio.

    I have placed the bean definitions in applicationContext.xml as described by you. ...but it doesn't work.

    I just have changed the localeChangeInterceptor this way:

    Code:
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        	<property name="paramName">
        		<value>siteLanguage</value>
        	</property>
    </bean>
    ...so that I have to put a param named 'siteLanguage' to the query string. Am I right? However, even if I copy your suggested bean definition it doesn't work in my app.

    Besides the app starts with Locale de_DE automatically but I want it to start with Locale en_US. How can I do this?
    For the time being I just have a button which shall switch from en_US to de_DE but in the future (after I got this stuff working) I want to place a dropdown list.

    Furthermore I want to display the same page after changing the Locale.

    Could you assist anyway?

    Thanks a lot in advance.

    Regards
    Dirk

  10. #10
    Join Date
    Jun 2006
    Posts
    109

    Default

    I used this javascript function to to it :

    Code:
    <script>
    		function changeLang(lang)
    		{
    			if(document.URL.match(new RegExp("\\?","g")))
    			{
    				if(document.URL.match(new RegExp("lang=","g")))
    				{
    					var string = document.URL;
    					var reg = new RegExp("lang=.*","g");
    					string = string.replace(reg,"lang="+lang);
    					window.location.replace(string);
    				}
    				else
    				{
    					window.location.replace(document.URL+"&lang="+lang);
    				}
    			}
    			else
    			{
    				window.location.replace(document.URL+"?lang="+lang);
    			}
    		}
    	</script>
    my param name was lang.

    But it's may be not the better way
    Last edited by Chaps; Jul 28th, 2006 at 12:35 PM.

Similar Threads

  1. MessageSource with external property file
    By grimholtz in forum Container
    Replies: 3
    Last Post: Aug 3rd, 2005, 02:38 PM
  2. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  3. Replies: 1
    Last Post: Apr 13th, 2005, 04:46 PM
  4. Unable to locate MessageSource
    By fracca in forum Web
    Replies: 2
    Last Post: Jan 24th, 2005, 12:37 AM
  5. Replies: 3
    Last Post: Sep 1st, 2004, 12:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •