1 2 Previous Next 26 Replies Latest reply on Mar 13, 2006 6:05 PM by gavin.king Go to original post
      • 15. Re: RessourceBundle for all Locals in APP-Scope
        perwik

        It never ends.... ;-)

        in LocaleSelector.setLocale() you do this

        ...
        set locale based on language and so on...
        ...
        else
         {
         locale = java.util.Locale.getDefault();
         FacesContext facesContext = FacesContext.getCurrentInstance();
         if (facesContext!=null)
         {
         java.util.Locale defaultLocale = facesContext.getApplication().getDefaultLocale();
         if (defaultLocale!=null) locale = defaultLocale;
         java.util.Locale requestLocale = facesContext.getExternalContext().getRequestLocale();
         if (requestLocale!=null) locale = requestLocale;
         }
         }
        


        this has the effect that the browser locale overrides the default application locale from faces-config.xml even though it is not listed in "supported locales".

        with these bundles
        messages.properties (happens to be in en_US)
        messages_sv_SE.properties
        messages_de_DE.properties
        


        and this in faces-config.xml
         <locale-config>
         <default-locale>de_DE</default-locale>
         <supported-locale>de_DE</supported-locale>
         <supported-locale>en_US</supported-locale>
         <supported-locale>sv_SE</supported-locale>
         </locale-config>
        


        If someone now comes along with a browser with the locale set to es_MX (mexican spanish) setLocale() (as of now) would be perfectly happy with the requestLocale but when java tries to find a resource bundle matching it, it would fall back on messages.properties (english) although the person who deployed the app said it should be messages_de_DE.properties (german). So setLocale() should only be happy with the requestLocale if it is in the list of supported locales.

        • 16. Re: RessourceBundle for all Locals in APP-Scope
          andy.2003

          if "de" is your default language, your messages.properties have to be in german ;-)

          • 17. Re: RessourceBundle for all Locals in APP-Scope
            perwik

            Sure, that would solve the problem, once. But you wouldn't want to have the deployer changing the filenames when he could change a setting in faces-config.xml. In my imaginary company the developers have a standard that says they should always have messages.properties in english :-)

            • 18. Re: RessourceBundle for all Locals in APP-Scope
              andy.2003

              so the default language is english as well...

              that's the way it works!

              • 19. Re: RessourceBundle for all Locals in APP-Scope
                gavin.king

                I gotta admit that it seems nuts to say "the default locale is german", but messages.properties is in english....

                • 20. Re: RessourceBundle for all Locals in APP-Scope
                  perwik

                  Well, you could just get rid of messages.properties and only have the other files, IF the localeSelector cared about the "supported locales" settings in faces-config.xml.

                  • 21. Re: RessourceBundle for all Locals in APP-Scope
                    perwik

                    To quote myself:

                    "perwik" wrote:
                    ...IF the localeSelector cared about the "supported locales" settings in faces-config.xml.


                    And it looks like it actually does, in the latest CVS. Thanks Gavin!

                    I guess I should learn to
                    1. Read forum
                    2. Update from CVS
                    3. Post if still needed
                    

                    :-)

                    • 22. Re: RessourceBundle for all Locals in APP-Scope
                      perwik

                      To be able to do this

                      <h:selectOneMenu value="#{localeSelector.localeString}">
                       <f:selectItems value="#{localeSelector.supportedLocales}" />
                      </h:selectOneMenu>
                      <h:commandButton action="#{localeSelector.select}" value="#{messages['button.changeLocale']}"/>
                      


                      I added this to LocaleSelector, and it looks like it's working as it should. The only flaw is that the list is not sorted and perhaps all language names should begin with an uppercase letter to be consistent within the list. My list (based on supported locales "sv" and "en") now says "svenska, English" since Swedish spelling rules says it should be written with a lowercase "s").

                       private ArrayList<SelectItem> supportedLocales;
                      
                       public String getLocaleString()
                       {
                       return getLocale().toString();
                       }
                      
                       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];
                       }
                       }
                      
                       @Create
                       public void setSupportedLocales()
                       {
                       this.supportedLocales = new ArrayList<SelectItem>();
                       Locale supportedLocale;
                       SelectItem selectItem;
                       FacesContext facesContext = FacesContext.getCurrentInstance();
                       for (Iterator it = facesContext.getApplication().getSupportedLocales(); it.hasNext();)
                       {
                       supportedLocale = (Locale) it.next();
                       selectItem = new SelectItem(supportedLocale.toString(), supportedLocale.getDisplayLanguage(supportedLocale));
                       this.supportedLocales.add(selectItem);
                       }
                       }
                      
                       public ArrayList<SelectItem> getSupportedLocales()
                       {
                       return this.supportedLocales;
                       }
                      


                      • 23. Re: RessourceBundle for all Locals in APP-Scope
                        gavin.king

                        Ah, that is a nice idea. I will do something like that.

                        By the way, if you are ever not happy with the algorithm for calculating the locale (which now basically just delegates to JSF), you can simply subclass LocaleSelector, override calculateLocale(), and name your subclass @Name("localeSelector").

                        Final note: in the latest CVS, changing the locale via localeSelector.select now also changes the locale in use by JSF.

                        • 24. Re: RessourceBundle for all Locals in APP-Scope
                          perwik

                          *Oh*, so components in my jar overrides the core ones if they have the same name? I guess I should've known that :-) By the way, what about anotations in the superclass? Are they inherited now? I remember reading something about it not being implemented yet.

                          • 25. Re: RessourceBundle for all Locals in APP-Scope
                            gavin.king

                            No not yet implemented.

                            • 26. Re: RessourceBundle for all Locals in APP-Scope
                              gavin.king

                              I committed that to CVS.

                              1 2 Previous Next