module Duckling.Locale
( Lang(..)
, Locale(..)
, Region(..)
, allLocales
, makeLocale
) where
import Data.Hashable
import Data.HashMap.Strict (HashMap)
import Data.HashSet (HashSet)
import GHC.Generics
import Prelude
import TextShow (TextShow)
import qualified Data.HashMap.Strict as HashMap
import qualified Data.HashSet as HashSet
import qualified TextShow as TS
data Lang
= AR
| BG
| CS
| DA
| DE
| EN
| ES
| ET
| FR
| GA
| HE
| HR
| HU
| ID
| IT
| JA
| KA
| KO
| MY
| NB
| NL
| PL
| PT
| RO
| RU
| SV
| TR
| UK
| VI
| ZH
deriving (Bounded, Enum, Eq, Generic, Hashable, Ord, Read, Show)
instance TextShow Lang where
showb = TS.fromString . show
data Region
= CA
| CN
| GB
| HK
| MO
| TW
| US
deriving (Bounded, Enum, Eq, Generic, Hashable, Ord, Read, Show)
instance TextShow Region where
showb = TS.fromString . show
data Locale = Locale Lang (Maybe Region)
deriving (Eq, Generic, Hashable, Ord)
instance Show Locale where
show (Locale lang Nothing) = show lang ++ "_XX"
show (Locale lang (Just region)) = show lang ++ "_" ++ show region
instance TextShow Locale where
showb = TS.fromString . show
makeLocale :: Lang -> Maybe Region -> Locale
makeLocale lang Nothing = Locale lang Nothing
makeLocale lang (Just region)
| HashSet.member region locales = Locale lang $ Just region
| otherwise = Locale lang Nothing
where
locales = HashMap.lookupDefault HashSet.empty lang allLocales
allLocales :: HashMap Lang (HashSet Region)
allLocales = HashMap.fromList
[ (EN, HashSet.fromList [CA, GB, US])
, (ZH, HashSet.fromList [CN, HK, MO, TW])
]