packages feed

fluent-1.0.0: test/Prelude.hs

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-name-shadowing #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Prelude
    ( module Prelude
    , module Data.Scientific
    , module Data.Text
    , module Language.Fluent
    , module Test.Hspec
    )
where

import Data.Char (toUpper)
import Data.Scientific (Scientific)
import Data.String (IsString (..))
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Time qualified as Time
import Language.Fluent
import Language.Fluent.AST (NumberLiteral (..))
import Language.Fluent.Bundle qualified as Bundle
import Language.Fluent.Number qualified as Number
import Language.Fluent.Parser qualified as Parser
import Language.Fluent.Plural qualified as Plural
import Test.Hspec
import "base" Prelude

data Plain = Plain

instance Locale Plain where
    fromCode "plain" = Just Plain
    fromCode _ = Nothing

    toCode _ = "plain"

    displayLanguage Plain Plain = Just "plain"

    capitalise Plain (Text.uncons -> Just (x, xs)) = Text.cons (toUpper x) xs
    capitalise Plain t = t

    pluralCategory _ value options
        | options.form == Plural.Ordinal = Plural.Other
        | abs value == 1 = Plural.One
        | otherwise = Plural.Other

    formatNumber _ amount options = case options.style of
        Decimal -> Right decimal
        Percent -> Right $ decimal <> "%"
        Currency c -> Right $ decimal <> c
        Unit u -> Right $ decimal <> u
      where
        NumberLiteral decimal = Number.toLiteral amount options

    formatTime _ when _ =
        Right . Text.pack $ Time.formatTime Time.defaultTimeLocale "%Y-%m-%d" when

instance IsString Identifier where
    fromString = either error id . Parser.parseIdentifier . Text.pack

instance (r ~ Either String Text) => Translate (Resource -> r) where
    translate reference =
        translate reference
            . Bundle.override (Bundle.UseIsolating False)
            . bundle (pure Plain)
            . pure