3 Replies Latest reply on Apr 15, 2014 9:42 AM by mbarkley

    Formatting dates using ERRAI localization

    jasper.oosterman

      Hi guys,

       

      I really like the ERRAI way of handling localization. No lengthy compile times and locale changes without reloading the file. But what I would like to do is the following:

       

      I have list of items separated with dates. The dates are now in English but when changing the locales, to for example Italian, the date should also change to Italian. Using GWT DateTimeFormat (http://www.gwtproject.org/javadoc/latest/com/google/gwt/i18n/client/DateTimeFormat.html) does not help, since according to GWT, we are still on the English locale.

       

      Are there any solutions or workarounds known?

       

      My ideal solution would be to have two data- fields in the template. One that contains the format (SHORT_DATE, from the PredefinedFormat in DateTimeFormat or  some custom string) and the other the timestamp. When the locale is then changed and all widgets receive the right translation such fields can then be automatically formatted and the content replaced..

      <span data-date-timestamp = "1396959676" data-date-format="DATE_SHORT"></span>
      
        • 1. Re: Formatting dates using ERRAI localization
          mbarkley

          Hi Jasper,

           

          I took a quick look at DateTimeFormat. It looks like you can pass in your own format String and furthermore all of the GWT locale implementations can be found in com.google.gwt.i18n.shared.impl.cldr. So here is how you could format the date for the "aa" locale:

          DateTimeFormat.getFormat(new DateTimeFormatInfoImpl_aa().dateFormatFull()).format(new Date());

           

          If you want an easy way to update lots of elements to new dates all at once, you could check out the StyleBindings. (Note: the example method in the docs takes a Style object, but it is also possible to take an Element as a parameter.) You could make a StyleBinding for localized date data-fields that uses GWT code like the example above. Then whenever you switch locales, you could simply call

          StyleBindingsRegistry.get().updateStyles();
          

           

          Hope that helps.

          • 2. Re: Formatting dates using ERRAI localization
            jasper.oosterman

            Ah, I was under the impression the GWT would compile out all localization info which were not specified in the .gwt.xml but that does not seem to be the case.

             

            Thank for the pointer to the stylebindings, a sort of onload function for each element, works like a charm. If I would like to have the manual changed such that it becomes clear that both Style and Element objects are allowed as parameter, how do I go about that?

            • 3. Re: Formatting dates using ERRAI localization
              mbarkley

              Glad to hear that's working for you.

              If I would like to have the manual changed such that it becomes clear that both Style and Element objects are allowed as parameter, how do I go about that?

              The Errai docs are all written in asciidoc, and you can make a pull request to them here. We would very much appreciate any contribution you have time to make

               

              Cheers.