andreasewering / intl-proxy / Intl

CodeGen for Intl functions. The Intl API will be given access to by a Proxy Object injected into the Elm Runtime via Flags. This mechanism makes it possible to have synchronous communication with JS. In order to avoid a lot of methods on the JS side, we are using a eval-like mechanism: We pass the information which Sub API to call and with which arguments as a JSON string.


type alias Intl =
Json.Decode.Value

Use this type for the JS Proxy Object you received via a flag or a port from the accompanying JS package.

decode : Json.Decode.Decoder Intl

Convienience decoder for Intl Type. This makes it possible for me to change the internal representation without a breaking change.


type PluralRule
    = Zero
    | One
    | Two
    | Few
    | Many
    | Other

The different plural rules. Depending on language, you might only have two of these like English (one, other) or all 6.

pluralRuleToString : PluralRule -> String

Convert a PluralRule to its String representation. This is written so it is compatible with the return values of the Intl API.

pluralRuleFromString : String -> Maybe PluralRule

Parse a PluralRule from a String. This is written so it is compatible with the return values of the Intl API.


type PluralType
    = Cardinal
    | Ordinal

Cardinal: Semantics of amount, Ordinal: Semantics of ranking (think 1st, 2nd ...)


type alias PluralOptions number =
{ language : String
, type_ : PluralType
, number : number 
}

Options for the plural function.

determinePluralRuleInt : Intl -> PluralOptions Basics.Int -> PluralRule

Determine the CLDR plural category (see https://www.unicode.org/cldr/cldr-aux/charts/30/supplemental/language_plural_rules.html) for a given Int and language.

The possible categories are: zero, one, two, few, many, and other. When the category cannot be determined for whatever reason, this function will default to "other".

determinePluralRuleFloat : Intl -> PluralOptions Basics.Float -> PluralRule

Determine the CLDR plural category (see https://www.unicode.org/cldr/cldr-aux/charts/30/supplemental/language_plural_rules.html) for a given Float and language.

The possible categories are: zero, one, two, few, many, and other. When the category cannot be determined for whatever reason, this function will default to "other".


type alias FormatNumberOptions number =
{ language : String
, args : List ( String
, Json.Encode.Value )
, number : number 
}

Options for the formatInt and formatFloat functions.

args can consist of any object entries you want to pass to the NumberFormat constructor. The following serves as a hint to what is actually valid and will not result in an error:

- currency: String, needs to be set if style is "currency". For example "EUR" is a valid currency.
- currencyDisplay: "name", "symbol" or "code", defaults to "symbol"
- maximumFractionDigits: Int
- maximumSignificantDigits: Int
- minimumFractionDigits: Int
- minimumIntegerDigits: Int
- minimumSignificantDigits: Int
- style : one of "decimal", "currency", "percent"
- useGrouping : Bool, True results in something like 123.456.789 while False will result in 123456789

formatFloat : Intl -> FormatNumberOptions Basics.Float -> String

Format a Float with the given Options

formatInt : Intl -> FormatNumberOptions Basics.Int -> String

Format an Int with the given Options


type alias FormatDateTimeOptions =
{ time : Time.Posix
, args : List ( String
, Json.Encode.Value )
, language : String 
}

Options for the formatDate function.

args can consist of any object entries you want to pass to the DateTimeFormat constructor. The following serves as a hint to what is actually valid and will not result in an error:

- timeZone: String, implementation specific. UTC is the default and works for all implementations. Stuff like Asia/Shanghai could work depending on browser.
- hour12: Bool, determines whether AM/PM or 24h format should be used.
- hourCycle: "h11", "h12", "h23" or "h24". Overrides the hour12 argument.
- weekday: "narrow", "short or "long"
- era: "narrow", "short or "long"
- year: "numeric" or "2-digit"
- month: "numeric", "2-digit", "narrow", "short or "long"
- day: "numeric" or "2-digit"
- hour: "numeric" or "2-digit"
- minute: "numeric" or "2-digit"
- second: "numeric" or "2-digit"
- timeZoneName: "short" or "long"

formatDateTime : Intl -> FormatDateTimeOptions -> String

Format a Posix Time with the given Options

unsafeAccess : Intl -> String -> Maybe String

Use only if you know what you are doing! Accesses the Intl object directly which can be more efficient if you only want to relay json strings instead of decoding and encoding dicts in Elm. Also provides access to APIs that are not wrapped in a more typesafe way by this package yet.