Announcement Announcement Module
Collapse
No announcement yet.
messageSource locale selection and basename Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • messageSource locale selection and basename

    I spent days to troubleshoot 2 problems related the locale specific message resolution. It seems to me that Spring only use the message resource with basename + locale.properties. It never use the basename only file. I.e. if the base name is 'mymsg', only the files mymsg_en_US.properties, mymsg_zh_HK.properties, mymsg_fr_CA will be used. The file basename only property file 'mymsg.properties' is never used. It looks true to me because the getMessage() method always use the locale specified resource file or if not found, it will use the default message in the method or throw an exception of not found. The same seems to be true when using resourcebundle view resolver. Want to know if it is correct to say that there is no need to create the base only properties file. Or there is situation this file will be used. If the latter is true, please advise the situation.

    Thanks a lot.

  • #2
    i think it looks for just the base name first. if you have this mapping:

    Code:
      <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages" />
      </bean>
    then spring will look in messages_es.properties and messages.properties. if your local is set to es, and a requested property can't be found in messages_es.properties, then spring will look in messages.properties. my local resolver is as follows:

    Code:
      <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"></bean>
    of course if i changed my local resolver, spring would look in messages_es.properties first:

    Code:
      <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="es" />
      </bean>

    Comment


    • #3
      slimchrisp,

      I have the same concept too. Even so, it will also mean that the file contents in the base file basename.properties will never be used. This was what happened in my resourcebundle view resolver and similar <fmt:message> tag. If I missed out the basename_locale.properties file, the resultant value will not be fetched from the basename.properties file. That is what I wondering in what situation will the basename.properties file be used.

      Comment


      • #4
        The basename.properties will be used when no locale specific basename_locale.properties can be found. It will not be used as a fallback when a property is not present in the basename_locale.properties property file.

        However you could ofcourse write a MessageSource which only looks at the basename.properties and completly ignores the locale and set that as a parent messageSource. This would enable the fallback mechanism when you miss a property in your locale specific file.
        Last edited by Marten Deinum; Oct 19th, 2006, 05:34 AM.

        Comment


        • #5
          Originally posted by mdeinum View Post
          The basename.properties will be used when no locale specific basename_locale.properties can be found. It will not be used as a fallback when a property is not present in the basename_locale.properties property file.
          Hmmm. Actually mdeinum, yeah, it will. Here's my setup in applicationContext.xml:

          Code:
            <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
              <property name="defaultLocale" value="es" />
            </bean>
            
            <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
              <property name="basename" value="messages" />
            </bean>
          I put some of the properties in messages_es.properties, but left some out. The ones I left out are in messages.properties.

          When I hit a page, all of the property values available in messages_es.properties are used, but when a property is not in there, the value is coming from messages.properties.

          This is in the book Expert Spring MVC and Web Flow. There are a bajillion setups you can configure in Spring, so maybe using a different setup (different localeResolver maybe?) this won't work, but at least as it is configured here, it does.
          Last edited by slimchrisp; Oct 19th, 2006, 02:51 PM.

          Comment


          • #6
            Locale &quot;Getter&quot;

            Newb here,

            How do i read the locale ?
            In my form controller ?
            I've created a messages.properties and a messages_pt.properties.
            My local settings are "PT", so, the messages appear in Portuguese.
            (OK so far), i change my local settings in windos (to test if messages displayed are the messages.properties), but i get my Portuguese messages instead.
            Any hint?

            Thknx

            Comment


            • #7
              Originally posted by VRodas View Post
              Newb here,

              How do i read the locale ?
              In my form controller ?
              I've created a messages.properties and a messages_pt.properties.
              My local settings are "PT", so, the messages appear in Portuguese.
              (OK so far), i change my local settings in windos (to test if messages displayed are the messages.properties), but i get my Portuguese messages instead.
              Any hint?

              Thknx
              I'd recommend getting Expert Spring MVC and Web Flow. I've been using spring dependency injection and transaction management for a few years, but I only recently started using Spring MVC. The book has been a good review of the basic ideas of Spring MVC. To be honest though, the book is pretty horrible as far as mistakes go. For example, in a small sample app, a few classes are given one name at the beginning of the example, but at the end a few pages later the names have been changed. No mention of the changes, you just have to figure out what is what. And on top of that half of the files aren't even mentioned. The source code is available though. You can get it at http://www.apress.com/ApressCorporat...9584X-3351.zip.

              Sorry this doesn't exactly answer your question, but I would take a look at locale resolvers (FixedLocaleResolver, AcceptHeaderLocaleResolver, SessionLocaleResolver, CookieLocaleResolver). You can have a form submit the locale value, and then in a controller call localeResolver.setLocale(req, res, locale). In my above example a few posts up, I am using the CookieLocaleResolver with a default locale set. Again, this is from the book (page 102). In all fairness, since I slammed it for the editing mistakes, I should reiterate that it is a great source for a lot of what spring mvc has to offer, what does what, etc.

              Comment


              • #8
                Originally posted by wlchung View Post
                slimchrisp,

                I have the same concept too. Even so, it will also mean that the file contents in the base file basename.properties will never be used. This was what happened in my resourcebundle view resolver and similar <fmt:message> tag. If I missed out the basename_locale.properties file, the resultant value will not be fetched from the basename.properties file. That is what I wondering in what situation will the basename.properties file be used.
                I do not know if the resource bundle viewresolver will resolve using the basemessage. but the <spring:message/> tag will. I never used the <fmt:message/> tag, I always use the one with spring. That one indeed does the fallback, as stated earlier (again a learning experience ).

                Comment


                • #9
                  slimchrisp,

                  Actually, I found out the case in similar situation to yours but different results. I did not have my locale specific file created, basename_zh_HK.properties. Everything is in the basename.properties in English. The result was it answered no value. Then I created the zh_HK file. The correct output came out. That resulted in my conclusion. By the way, I used the above in the Spring Portlet module.

                  VRodas,

                  Besides checking those locale resolver, if you have them, suggested by slimchrisp, I found it useful to check the actual locale send from the request from browser. You can try to check the HTTP request header for the locale. I check it using debug of the request in my development environment. You can check using some http request intercept program or network monitoring program.

                  Comment


                  • #10
                    slimchrisp,

                    Thkx for your reply, after changing my local settings and doing a logoff / logon, my localeResolver works.

                    I have my messages.properties written in french, my messages_pt.propertines in Portuguese and messages_en.properties in english and after the logoff / loggon, the messages displayed are the "correct" ones.

                    wlchung,

                    My guess is thet you're right, but i'm satisfied (for now), with the logoff / logon trick. Users won't be changing their local settings...

                    But... i've tried on 2 diferent machines, each with its local settings (one pt and one en), and the messages_(?).properties read is the server's local settings ... not the users... is this supposed?

                    Thks for your replies!

                    Comment

                    Working...
                    X