9

As you know ICU supports the following calendars:

Japanese
Buddhist
Chinese
Persian
Indian
Islamic
Hebrew
Indian
Coptic
Ethiopic

However IntlDateFormatter only convert the Gregorian format of date to a locale and does not convert the calendar automatically, yet it is possible to send to it the calendar and it will do the trick. The problem is I have to figure out the calendar system manually for each locale.

How to get the calendar type from locale string. for example sending fa_IR I need to get 'persian', so send it to IntleDateFormatter to generate a data string for that locale.

Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173

1 Answers1

3

In short; you can't. Why?

Let's take a look at what both objects are: IntlDateFormatter is part of the Internationalization extension built for performing various locale-aware operations. Amongst these operations are Date formatters and a locale-aware calendar.

Locale class is an identifier used to get language, culture or regionally-specific behaviour.

Even though they are part of the same extension, they are orthogonal as described here. That means that you cannot determine one from the other. This statement is further proven in the user guide provided by the ICU project.

... At present, all locales default to a Gregorian calendar, except for the compatibility locales th_TH_TRADITIONAL and ja_JP_TRADITIONAL. If the "calendar" keyword is supplied, this value will override the default for that locale.

Source: User guide ICU project

As the quote states any locale defaults to the Gregorian calendar unless the "calendar" keyword is provided. They cannot derive the calendar from the locale provided automatically.

The solution to your problem is most likely found in building a wrapper yourself. There are some packages on the internet that help you with this but it's nowhere near what you exactly want. If you need to save the date, save it as a timestamp and show it in the format desired by the user, the latter being if you have the freedom to implement some account settings or anything alike.

One module where I found inspiration in my quest for the answer is GlobalizeJS. There's an open open issue that paints an interesting picture of how difficult it is to get right. Take for example the United Arab Emirates (AE), they have five calendar preferences; Gregorian, Islamic Um al-Qura, Islamic, Islamic Civil and Islamic Tabular. I would let the user determine which calendar they prefer so it's most usable for them.

Good luck!

Edwin
  • 1,135
  • 2
  • 16
  • 24