reflex-localize (empty) → 1.0.0.0
raw patch · 9 files changed
+362/−0 lines, 9 filesdep +basedep +jsaddledep +mtl
Dependencies added: base, jsaddle, mtl, reflex, reflex-external-ref, text
Files
- CHANGELOG.md +3/−0
- LICENSE +7/−0
- README.md +64/−0
- reflex-localize.cabal +62/−0
- src/Reflex/Localize.hs +10/−0
- src/Reflex/Localize/Class.hs +60/−0
- src/Reflex/Localize/Language.hs +20/−0
- src/Reflex/Localize/Monad.hs +29/−0
- src/Reflex/Localize/Trans.hs +107/−0
+ CHANGELOG.md view
@@ -0,0 +1,3 @@+# 1.0.0.0++* Initial release
+ LICENSE view
@@ -0,0 +1,7 @@+Copyright 2019 ATUM SOLUTIONS AG++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,64 @@+# reflex-localize++Library provides helpers for dynamic strings that depends on current selected+language.++# How to use++First, you should define which languages your app supports:+``` haskell+module App.Language(+ Language(..)+ , module Reflex.Localize+ ) where++import Reflex.Localize+import Reflex.Localize.Language++data instance Language+ = English+ | Russian+```++Second, define enumeration for strings ids.++``` haskell+module App.Localization(+ module App.Localization+ , module App.Language+ ) where++import App.Language++data AboutPageStrings =+ AboutTitle+ | AboutVersion+ | AboutLicence+ | AboutHomepage+ | AboutDevelopers++instance LocalizedPrint AboutPageStrings where+ localizedShow l v = case l of+ English -> case v of+ AboutTitle -> "About"+ AboutVersion -> "Version"+ AboutLicence -> "Licence"+ AboutHomepage -> "Homepage"+ AboutDevelopers -> "Developers"+ Russian -> case v of+ AboutTitle -> "О продукте"+ AboutVersion -> "Версия"+ AboutLicence -> "Лицензия"+ AboutHomepage -> "Сайт"+ AboutDevelopers -> "Разработчики"+```++You can either collect all strings to one data sum or split strings for each+widget.++Finally, you should implement `MonadLocalized` type class in you application monad.+We suggest using monad transformer `LocalizeT` via `runLocalize` function:++``` haskell+runLocalize :: (Reflex t, TriggerEvent t m, MonadIO m) => Language -> LocalizeT t m a -> m a+```
+ reflex-localize.cabal view
@@ -0,0 +1,62 @@+name: reflex-localize+version: 1.0.0.0+synopsis: Localization library for reflex+description: Library provides helpers for dynamic strings that depends on current selected language.+ See also `reflex-localize-dom` for examples and DOM related helpers.+stability: Experimental+category: Reflex, FRP, Web, GUI, HTML, Javascript, Reactive, Reactivity, User Interfaces, User-interface+build-type: Simple+cabal-version: >=1.10+license: MIT+license-file: LICENSE+copyright: 2019 ATUM SOLUTIONS AG+author:+ - Anton Gushcha+ - Aminion+ - Vladimir Krutkin+ - Levon Oganyan+maintainer: - Anton Gushcha <ncrashed@gmail.com>+ - Aminion <>+ - Vladimir Krutkin <krutkinvs@gmail.com>+ - Levon Oganyan+bug-reports: https://github.com/hexresearch/ergvein/issues+homepage: https://github.com/hexresearch/ergvein+extra-source-files:+ README.md+ CHANGELOG.md++library+ hs-source-dirs: src+ exposed-modules:+ Reflex.Localize+ Reflex.Localize.Class+ Reflex.Localize.Language+ Reflex.Localize.Monad+ Reflex.Localize.Trans+ build-depends:+ base >= 4.5 && < 4.15+ , jsaddle+ , mtl+ , reflex >= 0.4 && < 0.8+ , reflex-external-ref+ , text+ default-language: Haskell2010+ default-extensions:+ DataKinds+ DeriveDataTypeable+ DeriveGeneric+ FlexibleInstances+ FunctionalDependencies+ GeneralizedNewtypeDeriving+ LambdaCase+ MultiParamTypeClasses+ RankNTypes+ ScopedTypeVariables+ StandaloneDeriving+ TypeFamilies+ UndecidableInstances++source-repository head+ type: git+ location: https://github.com/hexresearch/ergvein+ subdir: reflex-localize
+ src/Reflex/Localize.hs view
@@ -0,0 +1,10 @@+module Reflex.Localize+ (+ module Reflex.Localize.Class+ , module Reflex.Localize.Monad+ , module Reflex.Localize.Trans+ ) where++import Reflex.Localize.Class+import Reflex.Localize.Monad+import Reflex.Localize.Trans
+ src/Reflex/Localize/Class.hs view
@@ -0,0 +1,60 @@+-- | Pretty printing of values with localization+module Reflex.Localize.Class(+ GrammarCase(..)+ , LocalizedPrint(..)+ , defaultLocPrintDyn+) where++import Data.Data+import Data.Monoid+import Data.Text (Text)+import GHC.Generics+import qualified Data.Text as T+import Reflex+import Reflex.Localize.Language+import Reflex.Localize.Monad++-- | Grammar case to change form of word+data GrammarCase =+ Nominative -- ^ the “subject” case+ | Accusative -- ^ the “direct object” case+ | Genitive -- ^ corresponding to the possessive case or “of + (noun)”+ | Dative -- ^ corresponding to “to + (noun)" or the indirect object+ | Instrumental -- ^ denoting an instrument used in an action+ | Prepositional -- ^ used with many common prepositions, such as “in”, “on” etc.+ deriving (Eq, Ord, Enum, Show, Read, Bounded, Generic, Data)++-- | Printing human readable localized values.+--+-- Minimal implementation is either 'localizedShow' or 'localizedShowCased'+class LocalizedPrint a where+ -- | Convert value to localized string+ localizedShow :: Language -> a -> Text+ localizedShow = localizedShowCased Nominative+ {-# INLINE localizedShow #-}++ -- | Convert value to localized string using grammar case+ localizedShowCased :: GrammarCase -> Language -> a -> Text+ localizedShowCased _ = localizedShow+ {-# INLINE localizedShowCased #-}++ -- | Convert value to a dynamically changed string+ localized :: MonadLocalized t m => a -> m (Dynamic t Text)+ localized = defaultLocPrintDyn localizedShow+ {-# INLINE localized #-}++instance LocalizedPrint Text where+ localizedShow _ = id+ {-# INLINE localizedShow #-}+ localized = pure . pure+ {-# INLINE localized #-}++instance (LocalizedPrint a, LocalizedPrint b) => LocalizedPrint (Either a b) where+ localizedShow l = either (localizedShow l) (localizedShow l)+ {-# INLINE localizedShow #-}+ localized = either localized localized+ {-# INLINE localized #-}++-- | Default implementation+defaultLocPrintDyn :: MonadLocalized t m => (Language -> a -> Text) -> a -> m (Dynamic t Text)+defaultLocPrintDyn f v = fmap (fmap (flip f v)) getLanguage
+ src/Reflex/Localize/Language.hs view
@@ -0,0 +1,20 @@+-- |+-- Module : Reflex.Localize.Language+-- Copyright : (c) 2019 ATUM SOLUTIONS AG+-- License : MIT+-- Maintainer : ncrashed@protonmail.com+-- Stability : unstable+-- Portability : non-portable+--+-- A signature for languages enumeration that is used in your application,+-- alows to not pass around actual language implementation in type classes.+--+module Reflex.Localize.Language(+ Language+ ) where++import GHC.Generics (Generic)+import Prelude (Eq, Ord, Show, Read)++-- | Possible languages enumeration that is defined in your application+data family Language
+ src/Reflex/Localize/Monad.hs view
@@ -0,0 +1,29 @@+module Reflex.Localize.Monad+ (+ MonadLocalized(..)+ ) where++import Control.Monad.Reader+import Reflex+import Reflex.Localize.Language++-- | ===========================================================================+-- | Monad Localized+-- | ===========================================================================++-- | API for language localization support+class (Reflex t, Monad m) => MonadLocalized t m | m -> t where+ -- | Switch frontend language+ setLanguage :: Language -> m ()+ -- | Switch frontend language by event+ setLanguageE :: Event t (Language) -> m ()+ -- | Get language of the frontend+ getLanguage :: m (Dynamic t Language)++instance {-# OVERLAPPABLE #-} MonadLocalized t m => MonadLocalized t (ReaderT e m) where+ setLanguage = lift . setLanguage+ setLanguageE = lift . setLanguageE+ getLanguage = lift getLanguage+ {-# INLINE setLanguage #-}+ {-# INLINE setLanguageE #-}+ {-# INLINE getLanguage #-}
+ src/Reflex/Localize/Trans.hs view
@@ -0,0 +1,107 @@+-- |+-- Module : Reflex.Localize.Trans+-- Copyright : (c) 2019 ATUM SOLUTIONS AG+-- License : MIT+-- Maintainer : lemarwin42@gmail.com+-- Stability : unstable+-- Portability : non-portable+--+-- Plug-in implementation for `MonadLocalized` using wrapper around `ReaderT`.+-- Internal module, implementation details can be changed at any moment.+module Reflex.Localize.Trans where++import Control.Monad.Reader+import Control.Monad.State.Strict+import GHC.Generics+import Language.Javascript.JSaddle.Types+import Reflex+import Reflex.ExternalRef+import Reflex.Localize.Language+import Reflex.Localize.Monad++data LocalizeEnv t = LocalizeEnv {+ locEnvLangRef :: !(ExternalRef t Language)+} deriving (Generic)++-- | Allocate new environment for `LocalizeT`.+newLangEnv :: (Reflex t, TriggerEvent t m, MonadIO m) => Language -> m (LocalizeEnv t)+newLangEnv initLang = fmap LocalizeEnv $ newExternalRef initLang++-- | Plug-in implementation of `MonadLocalized`.+newtype LocalizeT t m a = LocalizeT { unLocalizeT :: ReaderT (LocalizeEnv t) m a }+ deriving (Functor, Applicative, Monad, Generic, MonadFix)++deriving instance PostBuild t m => PostBuild t (LocalizeT t m)+deriving instance NotReady t m => NotReady t (LocalizeT t m)+deriving instance PerformEvent t m => PerformEvent t (LocalizeT t m)+deriving instance TriggerEvent t m => TriggerEvent t (LocalizeT t m)+deriving instance MonadHold t m => MonadHold t (LocalizeT t m)+deriving instance MonadSample t m => MonadSample t (LocalizeT t m)+deriving instance MonadIO m => MonadIO (LocalizeT t m)+deriving instance MonadJSM m => MonadJSM (LocalizeT t m)+deriving instance (Group q, Additive q, Query q, Eq q, MonadQuery t q m, Monad m) => MonadQuery t q (LocalizeT t m)+deriving instance (Monoid w, DynamicWriter t w m) => DynamicWriter t w (LocalizeT t m)+deriving instance (Monoid w, MonadBehaviorWriter t w m) => MonadBehaviorWriter t w (LocalizeT t m)+deriving instance (Semigroup w, EventWriter t w m) => EventWriter t w (LocalizeT t m)+deriving instance (Requester t m) => Requester t (LocalizeT t m)++instance MonadTrans (LocalizeT t) where+ lift = LocalizeT . lift+ {-# INLINABLE lift #-}++instance MonadReader e m => MonadReader e (LocalizeT t m) where+ ask = lift ask+ {-# INLINABLE ask #-}+ local f (LocalizeT ma) = LocalizeT $ do+ r <- ask+ lift $ local f $ runReaderT ma r+ {-# INLINABLE local #-}++instance MonadState s m => MonadState s (LocalizeT t m) where+ get = lift get+ {-# INLINABLE get #-}+ put = lift . put+ {-# INLINABLE put #-}++instance Adjustable t m => Adjustable t (LocalizeT t m) where+ runWithReplace a0 a' = do+ r <- LocalizeT ask+ lift $ runWithReplace (runLocalizeT a0 r) $ fmap (`runLocalizeT` r) a'+ {-# INLINABLE runWithReplace #-}+ traverseIntMapWithKeyWithAdjust f dm0 dm' = do+ r <- LocalizeT ask+ lift $ traverseIntMapWithKeyWithAdjust (\k v -> runLocalizeT (f k v) r) dm0 dm'+ {-# INLINABLE traverseIntMapWithKeyWithAdjust #-}+ traverseDMapWithKeyWithAdjust f dm0 dm' = do+ r <- LocalizeT ask+ lift $ traverseDMapWithKeyWithAdjust (\k v -> runLocalizeT (f k v) r) dm0 dm'+ {-# INLINABLE traverseDMapWithKeyWithAdjust #-}+ traverseDMapWithKeyWithAdjustWithMove f dm0 dm' = do+ r <- LocalizeT ask+ lift $ traverseDMapWithKeyWithAdjustWithMove (\k v -> runLocalizeT (f k v) r) dm0 dm'+ {-# INLINABLE traverseDMapWithKeyWithAdjustWithMove #-}++-- | Execute localization widget with given environment.+runLocalizeT :: LocalizeT t m a -> LocalizeEnv t -> m a+runLocalizeT (LocalizeT ma) e = runReaderT ma e+{-# INLINEABLE runLocalizeT #-}++-- | Simplified version of `runLocalizeT`+runLocalize :: (Reflex t, TriggerEvent t m, MonadIO m) => Language -> LocalizeT t m a -> m a+runLocalize initLang ma = do+ re <- newLangEnv initLang+ runLocalizeT ma re+{-# INLINABLE runLocalize #-}++instance (PerformEvent t m, MonadHold t m, Adjustable t m, MonadFix m, MonadIO (Performable m), PostBuild t m, MonadIO m)+ => MonadLocalized t (LocalizeT t m) where+ setLanguage lang = do+ langRef <- LocalizeT $ asks locEnvLangRef+ writeExternalRef langRef lang+ setLanguageE langE = do+ langRef <- LocalizeT $ asks locEnvLangRef+ performEvent_ $ fmap (writeExternalRef langRef) langE+ getLanguage = LocalizeT $ (externalRefDynamic =<< asks locEnvLangRef)+ {-# INLINE setLanguage #-}+ {-# INLINE setLanguageE #-}+ {-# INLINE getLanguage #-}