1 2 Previous Next 26 Replies Latest reply on Mar 13, 2006 6:05 PM by Gavin King

    RessourceBundle for all Locals in APP-Scope

    Andreas Berger Newbie

      Is it possible to load all recource-bundles in Apllication scope?
      So the translated bundles don't have to be reloaded every request and can be receive from Memory.

      The concrete RessourceString have to be determined by the Local of the Clients Browser or optional by a Local stored in another scope (e.g. Session).

      Especially the Skeleton Code from SeamApp (JbossIDE) doesn't offer the correct RessourceBundle in a Component:

      ...
      
      @In
      private transient ResourceBundle resourceBundle;
      
      ...
      
      


      If I set my Browser to "de" and the Seam default to "en", in the <f:view> scope I get german but in the component scope I get english...

        • 1. Re: RessourceBundle for all Locals in APP-Scope
          Gavin King Master

          Doesn't the JVM cache loaded resource bundles?!

          You have a great point about the Seam resourceBundle component. I should certainly make this a session-scoped component and use the browser locale, not a global locale.

          I suck at i18n stuff :-)

          Thanks!

          • 2. Re: RessourceBundle for all Locals in APP-Scope
            Gavin King Master

            Yes, Java caches ResourceBundles, so you don't need to worry about caching them yourself.

            I have reworked the resourceBundle component as a SESSION scoped component, that uses the client locale, instead of letting you to set the locale in seam.properties.

            This is of course the correct way to do it. duh.

            • 3. Re: RessourceBundle for all Locals in APP-Scope
              Andreas Berger Newbie

               

              "gavin.king@jboss.com" wrote:
              ... I have reworked the resourceBundle component as a SESSION scoped component ...


              but if you store the resourceBundle in session scope, does seam load the resourceBundle for each Session, isn't it? So this shouldn't be very good for the memory usage (if each session stores the bundle)

              -Andreas

              • 4. Re: RessourceBundle for all Locals in APP-Scope
                Gavin King Master

                Well, I'm not absolutely certain, but I suppose that since the JVM is caching the resource bundle, all sessions are sharing the same instance.

                Check if you like, but I would be very surprised if this was not what happens....

                • 5. Re: RessourceBundle for all Locals in APP-Scope
                  Andreas Berger Newbie

                  You're right the JVM cache works very well! (Didn't know that Java caches the resources)

                  • 6. Re: RessourceBundle for all Locals in APP-Scope
                    Per Wiklander Newbie

                    Gavin,

                    Using the browser locale is of course better than just using a default value, but what if the user wants to change the locale? I don't speak a word of french but when I'm on holiday and sit in front of a computer with a french version of Firefox I would at least want to be able to click a button on the site and change the language to English. This would be a lot easier to do if the Seam ResourceBundle could handle that. I posted a patch in http://jira.jboss.com/jira/browse/JBSEAM-149. This does not include your new additions but I think some kind of merge of the current version and mine should give a result.

                    Just adding a

                    public void setBundle(Locale locale) {
                     bundle = java.util.ResourceBundle.getBundle( bundleName, locale, Thread.currentThread().getContextClassLoader() );
                    }
                    

                    would do the trick I guess, but it would perhaps be nice to be able to just input a language or country code as well.


                    • 7. Re: RessourceBundle for all Locals in APP-Scope
                      Gavin King Master

                      But this would still be a per-client setting, right?

                      If so, I am +1 on adding this capability.

                      • 8. Re: RessourceBundle for all Locals in APP-Scope
                        Andreas Berger Newbie

                        yes the client should be able to set his language and it should be stored in the session scope (maybe you can create a nice component for this...)

                        - Andreas

                        • 9. Re: RessourceBundle for all Locals in APP-Scope
                          Gavin King Master

                          Sure, that is easy to do.

                          • 10. Re: RessourceBundle for all Locals in APP-Scope
                            Gavin King Master

                            OK, this is now in CVS:

                            <h:selectOneMenu value="#{localeSelector.language}">
                             <f:selectItem itemLabel="English" itemValue="en"/>
                             <f:selectItem itemLabel="Deutsch" itemValue="de"/>
                             <f:selectItem itemLabel="French" itemValue="fr"/>
                             </h:selectOneMenu>
                             <h:commandButton action="#{localeSelector.select}" value="#{messages.Switch}"/>


                            • 11. Re: RessourceBundle for all Locals in APP-Scope
                              Per Wiklander Newbie

                              Thanks, this works great. Just as my own version, that I wrote before I saw that there was a reply to the thread :-) well, I guess it's always good to use the thinking cap, or something.

                              One problem though:
                              When setting the language to "sv" using this component (no country or variant selected) java will choose "messages.properties" before "messages_sv_SE.properties". Changing the filename to "messages_sv.properties" gets the job done, but that's not the way I want it.

                              An other thing:
                              It would be nice if setLocale() updated the country, language, and variant properties when it sets the default locale, so the correct language is selected in the list.

                              private void setLocale()
                              {
                              ...
                               this.language = locale.getLanguage;
                               this.country = locale.getCountry();
                               this.variant = locale.getVariant
                              }
                              


                              Thing number three:
                              I think localeSelector could have a
                              public void setLocale(java.util.Locale locale)
                              {
                               this.locale = locale;
                              }
                              


                              This would make it easier to change to a locale when you have an instance of Locale.

                              Thing number four (I hope someone's still reading :-)
                              The ultimate localeSelector would be if the list automagically displayed the supported locales listed in faces-config.xml and used Locale.getDisplayLanguage() for the itemLabel. When doing this I would vote for using locale.getDisplayLanguage(locale) so that the language is displayed in the language of the locale. Someone wanting to change the language of a site should have an easier time locating his preferred language if it is displayed in, you guessed it, his preferred language.

                              • 12. Re: RessourceBundle for all Locals in APP-Scope
                                Gavin King Master

                                 

                                "perwik" wrote:

                                One problem though:
                                When setting the language to "sv" using this component (no country or variant selected) java will choose "messages.properties" before "messages_sv_SE.properties". Changing the filename to "messages_sv.properties" gets the job done, but that's not the way I want it.


                                THat's the way Java's lookup algorithm works, not ours.

                                It would be nice if setLocale() updated the country, language, and variant properties when it sets the default locale, so the correct language is selected in the list.


                                OK, makes sense.

                                • 14. Re: RessourceBundle for all Locals in APP-Scope
                                  Per Wiklander Newbie

                                   

                                  "gavin.king@jboss.com" wrote:

                                  THat's the way Java's lookup algorithm works, not ours.


                                  Ok, I think I can live with that until I actually develop something that needs two different locales depending on country. I don't think it's that often. Although it should be easy enough to have a setLocaleString() like this
                                  public void setLocaleString(String localeString)
                                  {
                                   String[] localeParts = localeString.split("_");
                                   if (localeParts.length() > 0)
                                   {
                                   this.language = localeParts[0];
                                   }
                                   if (localeParts.length() > 1)
                                   {
                                   this.country = localeParts[1];
                                   }
                                   if (localeParts.length() > 2)
                                   {
                                   this.variant = localeParts[2];
                                   }
                                  }
                                  


                                  and a
                                  public String getLocaleString()
                                  {
                                   return this.locale.toString()
                                  }
                                  

                                  and then
                                  <h:selectOneMenu value="#{localeSelector.localeString}">
                                   <f:selectItem itemLabel="English" itemValue="en_US"/>
                                   <f:selectItem itemLabel="Deutsch" itemValue="de"/>
                                   <f:selectItem itemLabel="French" itemValue="fr_CH"/>
                                   <f:selectItem itemLabel="French" itemValue="fr_FR"/>
                                  </h:selectOneMenu>
                                  <h:commandButton action="#{localeSelector.select}" value="#{messages.Switch}"/>
                                  


                                  1 2 Previous Next