Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit a243b142b3e44f0ab5f770365500e80c9bbdee69 @ozataman committed Dec 21, 2010
Showing with 133 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +36 −0 snap-extension-si18n.cabal
  3. +22 −0 src/Snap/Extension/Si18n.hs
  4. +74 −0 src/Snap/Extension/Si18n/Impl.hs
@@ -0,0 +1 @@
+dist
@@ -0,0 +1,36 @@
+Name: snap-extension-si18n
+Version: 0.1
+Synopsis: Simple I18n framework for Snap
+Homepage: https://github.com/ozataman/snap-extension-si18n
+License: BSD3
+License-file: LICENSE
+Author: Ozgun Ataman
+Maintainer: ozataman@gmail.com
+Category: Web
+Build-type: Simple
+
+Cabal-version: >=1.2
+
+Library
+ hs-source-dirs: src
+
+ Exposed-modules:
+ Snap.Extension.Si18n.Impl
+
+ Other-modules:
+ Snap.Extension.Si18n
+
+ Build-depends:
+ base >= 4 && < 5
+ , bytestring >= 0.9
+ , containers >= 0.3
+ , directory
+ , mtl
+ , si18n
+ , snap >= 0.3 && < 0.4
+ , snap-core >= 0.3 && < 0.4
+ , text >= 0.11
+ , transformers
+
+ extensions:
+ OverloadedStrings
@@ -0,0 +1,22 @@
+{-|
+
+-}
+
+module Snap.Extension.Si18n
+ ( MonadSi18n(..)
+ ) where
+
+import Snap.Types
+
+import Text.Si18n
+import Data.Text (Text)
+
+
+------------------------------------------------------------------------------
+-- | The 'MonadSi18n' class.
+class MonadSnap m => MonadSi18n m where
+
+ translate :: Text -> Text -> TokenMap -> m Text
+
+
+
@@ -0,0 +1,74 @@
+module Snap.Extension.Si18n.Impl
+ ( MonadSi18n(..)
+ , Si18nState(..)
+ , HasSi18nState(..)
+ , si18nStateInitializer
+ ) where
+
+
+import Control.Monad
+import Control.Monad.IO.Class
+import Control.Monad.Reader
+import Control.Monad.Trans
+import qualified Data.Map as Map
+import Data.Text (Text)
+import qualified Data.Text as T
+import System.Directory
+
+import Snap.Extension
+import Snap.Types
+
+import Snap.Extension.Si18n
+
+import qualified Text.Si18n as S
+import Text.Si18n (I18nData)
+
+
+------------------------------------------------------------------------------
+-- |
+data Si18nState = Si18nState
+ { si18nData :: I18nData
+ , si18nDefLocale :: Text
+ }
+
+
+------------------------------------------------------------------------------
+-- |
+class HasSi18nState s where
+ getSi18nState :: s -> Si18nState
+
+
+------------------------------------------------------------------------------
+si18nStateInitializer :: FilePath
+ -- ^ Path to directory containing locale files.
+ -> Text
+ -- ^ Default locale
+ -> Initializer Si18nState
+si18nStateInitializer fp l = do
+ fs <- liftIO $ getDirectoryContents fp
+ let fs' = filter (`notElem` [".", ".."]) fs
+ i18n <- foldM step Map.empty fs'
+ let st = Si18nState i18n l
+ mkInitializer st
+ where
+ step a x = do
+ d <- liftIO $ S.loadI18nFile x
+ return $ a `Map.union` d
+
+
+------------------------------------------------------------------------------
+-- | Register Si18nState as an Extension.
+instance InitializerState Si18nState where
+ extensionId = const "Si18n/Impl"
+ mkCleanup = const $ return ()
+ mkReload = const $ return ()
+
+
+------------------------------------------------------------------------------
+-- |
+instance HasSi18nState s => MonadSi18n (SnapExtend s) where
+
+ translate l s m = do
+ i18n <- fmap si18nData $ asks getSi18nState
+ return $ S.t i18n l s m
+

0 comments on commit a243b14

Please sign in to comment.