5 Replies Latest reply on Oct 3, 2008 12:31 PM by nimo22

    Possible bug in LocaleSelector.getSupportedLocales()

    joblini

      Hello,


      When calling LocaleSelector.getSupportedLocales(), the name of each locale is displayed in its native language, for example:



      • English

      • espagnol

      • francais



      Wouldn't the desired behavior be to display the list in the language of the current locale?


      When the current language is English:



      • English

      • French

      • Spanish



      When the current language is French:



      • anglais

      • espagnol

      • francais




      I am able to produce this behavior by changing the following line:



      selectItems.add( new SelectItem( locale.toString(), locale.getDisplayName(getLocale()) ) );
      



      to this:



      selectItems.add( new SelectItem( locale.toString(), locale.getDisplayName(locale) ) );



        • 1. Re: Possible bug in LocaleSelector.getSupportedLocales()
          joblini

          Also, it is nice to have the list sorted by display name.  Here is my modification, in case anyone finds it useful.



             public List<SelectItem> getSupportedLocales()
             {
                List<SelectItem> selectItems = new ArrayList<SelectItem>();
          
                Iterator<Locale> locales = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
                TreeMap<String,Locale> sortedLocales = new TreeMap<String,Locale>();
                
                // Sort by display name
                while ( locales.hasNext() ) {
                     Locale locale = locales.next();
                    String displayName = locale.getDisplayName(getLocale());
                     sortedLocales.put(displayName, locale);
                }
                
                // Build the list of select items
                for (Iterator<Map.Entry<String,Locale>> it=sortedLocales.entrySet().iterator(); it.hasNext(); ) {
                     
                     Map.Entry<String,Locale> entry = (Map.Entry<String,Locale>)it.next();
                    String displayName = entry.getKey();
                    Locale locale = entry.getValue();
                    
                    selectItems.add( new SelectItem( locale.toString(), displayName ) );
                }
                
                return selectItems;
             }
          




          • 2. Re: Possible bug in LocaleSelector.getSupportedLocales()
            joblini

            Here is an example of the modification to components.xml to persist the choice of locale using cookies:


            <components xmlns:international="http://jboss.com/products/seam/international"
            ...
            xsi:schemaLocation=http://jboss.com/products/seam/international http://jboss.com/products/seam/international-2.0.xsd>
            
            <international:locale-selector cookie-enabled="true"/>
             
            



            • 3. Re: Possible bug in LocaleSelector.getSupportedLocales()
              pmuir

              Ingo Jobling wrote on Oct 02, 2008 01:30:


              Wouldn't the desired behavior be to display the list in the language of the current locale?



              Agreed, create an issue in JIRA and attach a patch :-)



              Also, it is nice to have the list sorted by display name. Here is my modification, in case anyone finds it useful.

              Again, file a JIRA with a patch, but you should use a comparator to sort the list.

              • 4. Re: Possible bug in LocaleSelector.getSupportedLocales()
                nimo22

                Hey Ingo,


                I do not like your idea.


                Imagine a person speaking (only) english, another speaking only french.


                So these two people visit your page. The french person wants to toggle to his/her language and sees these options:


                    English
                   
                French
                    Spanish

                But he/she can not read english and therefore cannot switch to her/his language. It s better to show these options in the way showing the language in the speech depending on the language:

                   
                English
                    espagnol
                   
                francais



                He/she can read francais but not neccesseraly french!


                So it makes absolutely sense (!), to show each locale its native language. It makes No (!) sense to list all selectable languages in the speech of the current locale.


                • 5. Re: Possible bug in LocaleSelector.getSupportedLocales()
                  nimo22

                  But this makes absolutely sense:



                  Also, it is nice to have the list sorted by display name.