Announcement Announcement Module
No announcement yet.
SessionLocaleResolver - question to defaultLocale Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • SessionLocaleResolver - question to defaultLocale


    I have a webapp with property-files for german and english. The user can choose the language and all is working fine. If the accept-language is english the english messages are used (at the first visit), if it is german, the german ones are used and if the user selects one, his desired are used. But now I would like to have some kind of 'fallback-locale'. If I define a defaultLocale, for example 'en', this is used even if the accept-language is german (that means a user on a german system needs change the language). And when I don't define it, I have no idea which language is used when the accept-language is something we don't support (maybe spanish or so). I tested a little bit and most of the time the webapp was displayed in german but sometimes in english. Is there a possibillity to define such a 'fallback-locale'? Since I havn't found a property for this I tried to implement a workaround but that resulted in really strange behavior...

    try {
    	ApplicationContext ac = getApplicationContext();"test: " + ac.getMessage("label.locale", new Object[] {}, request.getLocale()));			
    } catch (NoSuchMessageException nsm_e) {
    	WebUtils.setSessionAttribute(req, SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.ENGLISH);
    Sometimes the exception was thrown, but sometimes simply the german property-file was used... Am I missing something?

    applicationContext.xml contains:
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    	<property name="paramName" value="lang" />
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    	<!-- <property name="defaultLocale" value="en" /> -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    	<property name="interceptors">
                <ref local="localeChangeInterceptor"/>
    Would be great if someone can help me...

  • #2
    First off all you are mixing things up and you are solving it at the wrong end of the technology... Also you confuse me as you state you are using the SessionLocaleResolver but are mentioning the accept-language (I assume here the header?!).

    You are making it way to complex and are thinking to complex, you shouldn't fix it with/on the resolver (simply configure the defaultLocale and leave it there)... Also your 'workaround' code is flawed as you shouldn't use request.getLocale but use the LocalContextHolder to receive the current locale, this integrates with the LocaleResolver methods.

    Also the fallback/loading is handled by the MessageSource implementation of your choice and basically your default language should be in a resource bundle called (for example) the german one Now if for a specific language no .properties is found it will use the default ( (at least this is what the ReloadableResourceBundleMessageSource does).


    • #3
      Thank you for your reply! I think I miss more than just 'something'...

      This is what I want to do: When a user enters our site for the first time, I want the site beeing displayed either in the 'browser-language' or if this language is not supportet in the default language. Because of this I mentioned the accept-language. The problem is that I havn't developed the webapp but got the project from an external coworker, so I'm not very familiar with the source code...

      Do you know a tutorial, where I can check my configuration of this locale-stuff? I thought I am on the right way, but now I'm even more confused...

      By the way, now I defined a defaultLocale ('en') and named the english messages-file But now when I choose an unsupported locale I get no labels at all (only the 'not-found-that-label'-messages like '???label.login.username???')...


      • #4
        Remove the default-locale from the SessionLocaleResolver then (I suggest the reference guide and javadoc for this class), what it does is

        1. Check session for locale if found use it
        2. When default-locale is set and no local in session use it
        3. When no default locale is set and no local in session use request locale (the header).

        So when you specify a default one that has precedence over the request header.

        By the way, now I defined a defaultLocale ('en') and named the english messages-file But now when I choose an unsupported locale I get no labels at all (only the 'not-found-that-label'-messages like '???label.login.username???')...
        As mentioned the behavior I described is the behavior of the ReloadableResouceBundleMessageSource, ResourceBundleMessageSource relies on the default ResourceBundle mechanism...


        • #5
          Sorry, forgot to mention that I canged the messageSource to ReloadableResouceBundleMessageSource...

          Maybe this is a stupid question... But do I need a special controller for changing the locale? I found some tutorials and examples and none of them mention a special controller. It always seemed like the xml-configuration is enough. I have controller for the sites of course, but I thought the localeChangeInterceptor does the trick for me... I thought if this interceptor finds a param named like the "paramName"-porperty it changes the locale by itself... Okay, this never happened and so I checked this param manually with ServletRequestUtils.getStringParameter but I always searched for a wrong or missing configuration parameter or so...

          I know, trying to help me must be some kind of pita... Thanks a lot for your patience!

          Edit: Maybe our spring version is too old? We use 2.0.3...
          Another edit: Seems not...
          You can build in changing of locales using the LocaleChangeInterceptor. This interceptor needs to be added to one of the handler mappings (see Section 13.4, “Handler mappings”). It will detect a parameter in the request and change the locale (it calls setLocale() on the LocaleResolver that also exists in the context).
          Last edited by molly_grue; Sep 20th, 2012, 07:09 AM.


          • #6
            Okay, just found this property:
            <property name="fallbackToSystemLocale" value="false"/>
            Now, the english default is used when the browser speaks an unsupported language. Yesss, my minor success for today!