packages feed

fluent-1.0.0: src/Language/Fluent/Locale.hs

module Language.Fluent.Locale where

import Data.List.NonEmpty (NonEmpty)
import Data.Scientific (Scientific)
import Data.Text (Text)
import Data.Time (UTCTime)
import Language.Fluent.Number (NumberOptions)
import Language.Fluent.Plural qualified as Plural
import Language.Fluent.Time (TimeOptions)
import Prelude

-- | A locale, identified by an implementation-defined code (e.g. BCP 47, ISO 639).
class Locale l where
    -- | Parse a locale from its textual code, if recognised.
    fromCode :: Text -> Maybe l

    -- | Render a locale back to its textual code.
    toCode :: l -> Text

    -- | Translate the name of one language into another.
    displayLanguage
        :: l
        -- ^ The language being named
        -> l
        -- ^ The language to render the name in
        -> Maybe Text

    -- | A language's own name for itself, e.g. "Deutsch" for German.
    localDisplayLanguage :: l -> Maybe Text
    localDisplayLanguage l = displayLanguage l l

    -- | Capitalise the first character of some text following the locale's casing rules.
    capitalise :: l -> Text -> Text

    -- | Classify a number into a plural category (e.g. "one", "few", "other"), used to
    -- pick the matching translation variant.
    pluralCategory :: l -> Scientific -> NumberOptions -> Plural.Category

    -- | Format a number according to locale conventions.
    formatNumber :: NonEmpty l -> Scientific -> NumberOptions -> Either String Text

    -- | Format a point in time according to locale conventions.
    formatTime :: NonEmpty l -> UTCTime -> TimeOptions -> Either String Text