Announcement Announcement Module
No announcement yet.
internationalization - messageSource Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • internationalization - messageSource


    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 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:
    <bean id="messageSource" class="">
    		<property name="basename"><value>messages</value></property>										
    tia, lana

  • #2
    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.

    <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
    		<property name="basename"><value>your_properties_file</value></property>
    Properties file example:
    Well, besides that, you must have a localeResolver bean declared in your appcontext, just like that:

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

    	<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:

    	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="interceptors">
    				<ref local="localeChangeInterceptor"/>
    		<property name="mappings">
    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:

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



    • #3
      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
        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.


        I hope it works!



        • #5
          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 file is taken for displaying (but only 1x)...and when I make any other request -> the locale changes back to german - and the german 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 file would be exchanged in the application context file?

          tia lana


          • #6
            internationalization problem solved

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

            thx, lana


            • #7

              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?


              • #8
                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, 09:36 AM.


                • #9
                  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:

                  <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
                      	<property name="paramName">
         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.



                  • #10
                    I used this javascript function to to it :

                    		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);
                    my param name was lang.

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


                    • #11
                      I've done everything that is written here.
                      It is working.

                      But when I'm reaching url like this:
                      <a /urlToBeResoved.html?locale=some_lang"/>

                      in urlToBeResolved request.getLocale() is null! (but anyway, i18n messages are working fine). How can I get locale?