Permalink
Browse files

a little refactoring

  • Loading branch information...
1 parent 6ffcafb commit 877c4bccd61caa0d3d6ff2827085a9ece105a4c7 @freizl freizl committed Oct 5, 2012
Showing with 56 additions and 43 deletions.
  1. +4 −0 Makefile
  2. +1 −1 snaplet-i18n.cabal
  3. +51 −42 src/Snap/Snaplet/I18N.hs
View
4 Makefile
@@ -6,6 +6,10 @@ default: clean build rebuild
clean:
rm -rf $(DIST)
+ rm -rf ./cabal-dev/lib/snaplet-i18n*
+ rm -rf ./cabal-dev/packages/snaplet-i18n*
+ rm -f ./cabal-dev/packages-7.4.1.conf/snaplet-i18n-*
+
conf:
$(CBD) configure
View
2 snaplet-i18n.cabal
@@ -3,7 +3,7 @@
-- http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/authors.html#pkg-descr.
-- The name of the package.
Name: snaplet-i18n
-Version: 0.0.3
+Version: 0.0.3.1
Description: A light weight i18n snaplet.
Synopsis: snaplet-i18n
Homepage: https://github.com/HaskellCNOrg/snaplet-i18n
View
93 src/Snap/Snaplet/I18N.hs
@@ -1,8 +1,7 @@
-{-# LANGUAGE OverloadedStrings, FlexibleInstances, MultiParamTypeClasses #-}
-{-# LANGUAGE DeriveDataTypeable #-}
+{-# LANGUAGE OverloadedStrings #-}
-module Snap.Snaplet.I18N
+module Snap.Snaplet.I18N
( I18NSnaplet
, HasI18N (..)
, I18NMessage (..)
@@ -12,22 +11,24 @@ module Snap.Snaplet.I18N
) where
import Control.Monad
+import qualified Data.Configurator as Config
+import qualified Data.Configurator.Types as Config
import Data.Lens.Common
import Data.Maybe
+import qualified Data.Text as T
import System.Directory
import System.FilePath.Posix
import Text.Templating.Heist
-import Text.XmlHtml hiding (render)
-import qualified Data.Configurator as Config
-import qualified Data.Configurator.Types as Config
-import qualified Data.Text as T
-import qualified Text.XmlHtml as X
+import Text.XmlHtml hiding (render)
+import qualified Text.XmlHtml as X
-import Snap
-import Snap.Snaplet.Heist
+import Snap
+import Snap.Snaplet.Heist
--------------------------------------------------------
+----------------------------------------------------------------------
+-- Types
+----------------------------------------------------------------------
type Locale = String
type MessageFile = String
@@ -36,87 +37,93 @@ defaultLocale :: Locale
defaultLocale = "en_US"
-- | ?? could be multiple message files
---
+--
defaultMessageFilePrefix :: MessageFile
defaultMessageFilePrefix = "data/message"
-data I18NConfig = I18NConfig { _getLocale :: Locale -- ^ locale, default "en"
- , _getMessageFile :: MessageFile -- ^ message file name, default to "message"
+data I18NConfig = I18NConfig { _getLocale :: Locale
+ -- ^ locale, default "en"
+ , _getMessageFile :: MessageFile
+ -- ^ message file name, default to "message"
} deriving (Show)
--- | A simple mapping to hold i18n messages
---
-data I18NMessage = I18NMessage Config.Config
+-- | Message content.
+--
+newtype I18NMessage = I18NMessage Config.Config
-- | data type
---
+--
data I18NSnaplet = I18NSnaplet
{ _getI18NConfig :: I18NConfig
, _getI18NMessage :: I18NMessage
- }
+ }
-- | Compose App with a I18N Snaplet.
---
+--
class HasI18N b where
i18nLens :: Lens b (Snaplet I18NSnaplet)
-
+
-- | Util functions
---
+--
getI18NSnaplet :: HasI18N b => Handler b b I18NSnaplet
getI18NSnaplet = with i18nLens Snap.get
-- | Get the @I18NMessage@
---
+--
getI18NMessages :: HasI18N b => Handler b b I18NMessage
getI18NMessages = liftM _getI18NMessage getI18NSnaplet
-- | Look up a value in, usuallly Handler Monad
---
+--
lookupI18NValue :: HasI18N b => T.Text -> Handler b b T.Text
lookupI18NValue key = do
(I18NMessage msg) <- getI18NMessages
liftIO $ Config.lookupDefault "Error: no value found." msg key
--------------------------------------------------------
+----------------------------------------------------------------------
+-- Init Snaplet
+----------------------------------------------------------------------
-- | Init this I18NSnaplet snaplet.
---
+--
initI18NSnaplet :: (HasHeist b, HasI18N b)
=> Maybe Locale -- ^ Locale, default to @defaultLocale@
-> SnapletInit b I18NSnaplet
initI18NSnaplet l = makeSnaplet "I18NSnaplet" "" Nothing $ do
let i18nConfig = I18NConfig (fromMaybe defaultLocale l) defaultMessageFilePrefix
- config <- liftIO $ readMessageFile i18nConfig
+ msg <- liftIO $ readMessageFile i18nConfig
addDefaultSplices
- return $ I18NSnaplet i18nConfig $ I18NMessage config
+ return $ I18NSnaplet i18nConfig msg
where addDefaultSplices = addSplices [ ("i18n", liftHeist i18nSplice)
, ("i18nSpan", liftHeist i18nSpanSplice)]
-------------------------------------------------------
---
+--
-- | Load file
-- server will not be able to start up if dir doesnt exists.
-- Thus, no additional validation check so far.
---
-readMessageFile :: I18NConfig -> IO Config.Config
+--
+readMessageFile :: I18NConfig -> IO I18NMessage
+ --IO Config.Config
readMessageFile config = do
- base <- getCurrentDirectory
+ base <- getCurrentDirectory
let fullname = base </> file config
- Config.load [Config.Required fullname]
- where
+ fmap I18NMessage (Config.load [Config.Required fullname])
+ where
-- file fullname will be like message-en_US.cfg
- -- FIXME: Maybe replace "-" with "_" in locale for typo
+ -- FIXME: Maybe replace "-" with "_" in locale in case typo
file c = _getMessageFile c ++ "-" ++ _getLocale c ++ ".cfg"
--------------------------------------------------------
-
+----------------------------------------------------------------------
+-- Splices
+----------------------------------------------------------------------
-- | element attribute used for looking up i18n value.
-- e.g. <i18n name="hello" />
---
+--
i18nSpliceAttr :: T.Text
i18nSpliceAttr = "name"
@@ -128,7 +135,7 @@ i18nSpliceAttr = "name"
-- <i18n name="hello" />
-- <i18n name="hello"><p><i18nValue/></p></i18n>
--
--- FIXME: Turns out that it is not possible to fail at compilation if value is Nothing but runtime.
+-- FIXME: Turns out that it is not possible to fail at compilation if value is Nothing but runtime.
i18nSplice :: HasI18N b => Splice (Handler b b)
i18nSplice = do
input <- getParamNode
@@ -138,16 +145,18 @@ i18nSplice = do
_ -> runChildrenWithText [("i18nValue", value)]
-- | Splices. use 'span' html element wrap result.
---
+--
i18nSpanSplice :: HasI18N b => Splice (Handler b b)
i18nSpanSplice = do
input <- getParamNode
value <- lift . lookupI18NValue $ getNameAttr input
return [X.Element "span" (elementAttrs input) [X.TextNode value]]
-- | Look up 'name' attribute value.
---
+--
getNameAttr :: Node -> T.Text
getNameAttr n = case getAttribute i18nSpliceAttr n of
Just x -> x
- _ -> ""
+ _ -> ""
+
+----------------------------------------------------------------------

0 comments on commit 877c4bc

Please sign in to comment.