1

Once again I'm flabbergasted and disappointed in SF and Apex functionality. Either that or I missed something. It appears that Datetime.format() doesn't return the result in the running user's language but only English.

Datetime dt = Datetime.newInstance(2023,1,1,0,0,0);
System.debug(UserInfo.getLanguage());
System.debug(UserInfo.getLocale());
System.debug(dt.format('EEE EEEE MMMM'));

Yields --

10:18:57.2 (4133564)|USER_DEBUG|[2]|DEBUG|es
10:18:57.2 (4311009)|USER_DEBUG|[3]|DEBUG|es_ES
10:18:57.2 (4418061)|USER_DEBUG|[4]|DEBUG|Sun Sunday January

Anyone know of a work around for this one? I recall can't believe what I'm seeing. I expected "Dom Domingo Enero".

3
  • 1
    I believe this is still a limitation within the DateTime class in Salesforce. Even after adopting ICU standards for Date and Time locale formatting in 2020, it only applies to formatting of numeric month, day, and year. You'd need to build your own method for handling translations of string values. Feb 20 at 15:44
  • 2
    I believe This may be a limitation based on how Apex is compiled into Java bytecode, the version of Java that Apex is based off of, or possibly both. It appears that Java's SimpleDateFormat didn't support locales until Java 9. Also keep in mind that a lot of customers have written a lot of code that likely depends on it always being English names. Changing that (or rather changing the default behavior) would break a lot of code and ruffle a lot of feathers. I suppose the quick workaround would be to maintain a map from English names/abbreviations to that of your desired language(s).
    – Derek F
    Feb 20 at 16:07
  • 1
    As DerekF and @MorganMarchese said, this is a limitation of the SimpleDateFormat. We had to write a shed load of code and provide custom settings to allow this to be localized. But that only supports localization of text generated through our code. I don't think the ICU changes address these problems either.
    – Phil W
    Feb 20 at 18:16

0

You must log in to answer this question.

Browse other questions tagged .