Skip to content

Commit

Permalink
bump 0.0.2; simplified public api.
Browse files Browse the repository at this point in the history
  • Loading branch information
freizl committed May 17, 2012
1 parent bfabe80 commit 7cd26aa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
5 changes: 2 additions & 3 deletions snaplet-i18n.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
-- 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.1
Version: 0.0.2
Description: A light weight i18n snaplet.
Synopsis: snaplet-i18n
Homepage: freizl.github.com
Homepage: https://github.com/HaskellCNOrg/snaplet-i18n
License: BSD3
License-file: LICENSE
Author: Haisheng,Wu
Expand All @@ -17,7 +17,6 @@ Build-type: Simple
Stability: Experience
Cabal-version: >=1.6


Extra-source-files:
README.md
test/snap.hs
Expand Down
24 changes: 15 additions & 9 deletions src/Snap/Snaplet/I18N.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
module Snap.Snaplet.I18N
( I18NSnaplet
, HasI18N (..)
, I18NMessage (..)
, initI18NSnaplet
, getI18NMessages
, I18NMessage (..)
, lookupI18NValue
) where

Expand Down Expand Up @@ -69,24 +69,32 @@ class HasI18N b where
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 this I18NSnaplet snaplet.
--
initI18NSnaplet :: (HasHeist b, HasI18N b)
=> Maybe Locale -- ^ Locale, default to @defaultLocale@
-> Maybe MessageFile -- ^ Message file prefix, default to @defaultMessageFilePrefix@
-> SnapletInit b I18NSnaplet
initI18NSnaplet l m = makeSnaplet "I18NSnaplet" "" Nothing $ do
i18nConfig <- return $ I18NConfig (fromMaybe defaultLocale l) (fromMaybe defaultMessageFilePrefix m)
initI18NSnaplet l = makeSnaplet "I18NSnaplet" "" Nothing $ do
i18nConfig <- return $ I18NConfig (fromMaybe defaultLocale l) defaultMessageFilePrefix
config <- liftIO $ readMessageFile i18nConfig
defaultSplices
addDefaultSplices
return $ I18NSnaplet i18nConfig $ I18NMessage config
where defaultSplices = addSplices [ ("i18n", liftHeist i18nSplice)
, ("i18nSpan", liftHeist i18nSpanSplice)]
where addDefaultSplices = addSplices [ ("i18n", liftHeist i18nSplice)
, ("i18nSpan", liftHeist i18nSpanSplice)]

-------------------------------------------------------
--
Expand Down Expand Up @@ -140,5 +148,3 @@ lookupI18NValue' m input = Config.lookupDefault "Error: no value found." m (getA
where getAttr' i = case getAttribute i18nSpliceAttr i of
Just x -> x
_ -> ""
lookupI18NValue :: Config.Config -> T.Text -> IO T.Text
lookupI18NValue = Config.lookupDefault "Error: no value found."
2 changes: 1 addition & 1 deletion test/snap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ app :: SnapletInit App App
app = makeSnaplet "app" "An snaplet example application." Nothing $ do
h <- nestSnaplet "heist" heist $ heistInit "templates"
locale <- liftIO $ getDefaultLocale
i <- nestSnaplet "i18n" i18n $ initI18NSnaplet (Just "zh_CN") Nothing
i <- nestSnaplet "i18n" i18n $ initI18NSnaplet (Just "zh_CN")
addRoutes routes
return $ App h i

Expand Down

0 comments on commit 7cd26aa

Please sign in to comment.