Search code examples
d3.jsvega-lite

How to get country specific formatting rules?


I am currently building a workflow for vega lite API and want to parametrize the choice of a locale.

So I want the user to specify her country code by providing a IETF language tag like "us-En" or "fr-FR" and from that I want to obtain the country specific formatting rules (see below for the format I need).

E.g., I am looking for a function, which produces the following output for different country codes:

   function getLocaleFormats(countryCode) {
     if (countryCode === "de-DE") {
      return {
            "decimal": ",",
            "thousands": ".",
            "grouping": [3],
            "currency": ["€", ""],
            "dateTime": "%a %b %e %X %Y",
            "date": "%d.%m.%Y",
            "time": "%H:%M:%S",
            "periods": ["AM", "PM"],
            "days": ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"],
            "shortDays": ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"],
            "months": ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
            "shortMonths": ["Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
        };
     }
   }

Any help would be greatly appreciated.


Solution

  • I found the references I need, they are in:

    • https://unpkg.com/d3-format@1/locale/${countryCode}.json
    • https://unpkg.com/d3-time-format@1/locale/${countryCode}.json

    with country code being the IETF language tag like "us-En".

    p.s. I noticed these formatting rules are specified in d3.js as vega lite is build upon d3.js, so I added the d3.js and vega lite tags to the question.