5 Replies Latest reply on Apr 6, 2011 8:02 AM by kenfinni

    Switch locale

    tdtappe

      In Seam 2 I used the localeSelector to switch to a different language/locale. What is the (best) way to do so in Seam 3?


      --Heiko

        • 1. Re: Switch locale
          shane.bryzak

          You set the locale by firing an event, like so:





          @Inject @Alter private Event<java.util.Locale> localeEvent;
          
          public void setUserLocale() {
              Locale canada = Locale.CANADA;
              localeEvent.fire(canada);
          }




          You can find more info in the reference docs.

          • 2. Re: Switch locale
            tdtappe

            Thanks Shane. But for what so ever reason it doesn't seem to work.


            I would have expected for example that firing the event gets me to UserLocaleProducer.changeLocale. Right? But I never get there.


            Also


            @Inject
            private List<Locale> locales


            gets me an empty list!? I tried to define some locales via beans.xml:


            <lc:AvailableLocales>
                 <s:specializes />
                 <lc:supportedLocaleKeys>
                      <s:value>de</s:value>
                      <s:value>en</s:value>
                      <s:value>fr</s:value>
                 </lc:supportedLocaleKeys>
            </lc:AvailableLocales>


            But without success.


            Is it Ok to see the following info when my app is initialized?


            Preventing class org.jboss.seam.international.locale.LocaleConfiguration from being installed as bean due to @Veto annotation


            --Heiko

            • 3. Re: Switch locale
              kenfinni

              Heiko,


              Sorry for these problems with i18n!


              For modifying the Locale, unfortunately there is a bug in the documentation.  You also need to add @Client to the injection of the Event, so it would be:


              @Inject
              @Alter
              @Client
              private Event<java.util.Locale> localeEvent;




              For the list of available locales, for some reason the latest documentation that Shane provided the link to is not actually the latest.  I'm looking into why that is the case, but in the meantime you can go to snapshot docs and see that to configure the available locales you do the following:




              public class CustomLocaleConfiguration extends LocaleConfiguration {
                  @PostConstruct
                  public void setup() {
                      addSupportedLocaleKey("en");
                      addSupportedLocaleKey("fr");
                  }
              }




              Ken

              • 4. Re: Switch locale
                tdtappe

                Yeah! Works like a charm now :-)
                Thanks a lot, Ken!


                As for extending LocaleConfiguration to configure available locales: is this the way to go? What about using AvailableLocales in beans.xml - is it for something else or just not working right now?


                --Heiko

                • 5. Re: Switch locale
                  kenfinni

                  Glad it solved the problem.


                  As for XML config, it should be possible to do it with beans.xml but I haven't tested that ability recently so don't know the correct syntax.


                  There should be two ways you can achieve it, creating an XML Producer to create an object that becomes injectable into AvailableLocales, or replace the content of the field within AvailableLocales.


                  Ken