Permalink
Browse files

simplify snaplet name

  • Loading branch information...
1 parent f575b9a commit 792d21dc1c515e6208eb4e9083d0b113bcff57e8 @freizl freizl committed Jan 2, 2013
Showing with 50 additions and 62 deletions.
  1. +1 −1 Makefile
  2. +4 −7 example/snap.hs
  3. +22 −27 example/snaplets/heist/templates/index.tpl
  4. +23 −27 src/Snap/Snaplet/I18N.hs
View
@@ -17,6 +17,7 @@ conf:
build: conf
$(CBD) build
hlint src/
+ stylish-haskell -i src/Snap/Snaplet/*.hs
rebuild: clean build
@@ -29,4 +30,3 @@ reinstall: clean install
demo:
cd example/ && runghc -package-conf=../cabal-dev/packages-7.4.1.conf snap.hs -b 127.0.0.1 -p 8888
-
View
@@ -32,19 +32,16 @@ import qualified Heist.Interpreted as I
import Control.Lens
import Text.XmlHtml hiding (render)
-#ifdef DEVELOPMENT
-import Snap.Loader.Dynamic
-#else
+import Snap.Snaplet.I18N
+
import Snap.Loader.Static
-#endif
-import Snap.Snaplet.I18N
------------------------------------------------------------------------------
data App = App
{ _heist :: Snaplet (Heist App)
- , _i18n :: Snaplet I18NSnaplet
+ , _i18n :: Snaplet I18N
}
makeLenses ''App
@@ -91,7 +88,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")
+ i <- nestSnaplet "i18n" i18n $ initI18N (Just "zh_CN")
addRoutes routes
return $ App h i
@@ -1,30 +1,25 @@
<!DOCTYPE html>
<html>
-<head>
-
-<style>
- p > span { display: block; }
-</style>
-
-</head>
-
-<body>
-
-<h2>Test I18N</h2>
-
-<testSplice />
-
-<p>
-<i18n name="shanghai"></i18n>
-<i18nSpan name="hello" class="test"></i18nSpan>
-<i18n name="invalidekey"></i18n>
-</p>
-
- <p>
- <i18n name="shanghai">
- <input type="submit" value="${i18nValue}" />
- </i18n>
-</p>
-
-</body>
+ <head>
+ </head>
+
+ <body>
+
+ <h2>Test I18N</h2>
+
+ <label><testSplice /></label>
+
+ <label><i18n name="shanghai"></i18n></label>
+ <p>
+ <i18nSpan name="hello" class="test"></i18nSpan>
+ </p>
+ <p><i18n name="invalidekey"></i18n></p>
+
+ <p>
+ <i18n name="shanghai">
+ <input type="submit" value="${i18nValue}" />
+ </i18n>
+ </p>
+
+ </body>
</html>
@@ -2,28 +2,26 @@
module Snap.Snaplet.I18N
- ( I18NSnaplet
+ ( I18N
, HasI18N (..)
, I18NMessage (..)
- , initI18NSnaplet
+ , initI18N
, getI18NMessages
, lookupI18NValue
) where
import Control.Monad
import qualified Data.Configurator as Config
import qualified Data.Configurator.Types as Config
---import Control.Lens
import Data.Maybe
import qualified Data.Text as T
-import System.FilePath.Posix
import Heist
-import qualified Heist.Interpreted as I
---import qualified Heist.Compiled as C
+import qualified Heist.Interpreted as I
+import System.FilePath.Posix
import Text.XmlHtml hiding (render)
import qualified Text.XmlHtml as X
-import Paths_snaplet_i18n
+import Paths_snaplet_i18n
import Snap
import Snap.Snaplet.Heist
@@ -55,57 +53,55 @@ newtype I18NMessage = I18NMessage Config.Config
-- | data type
--
-data I18NSnaplet = I18NSnaplet
- { _getI18NConfig :: I18NConfig
- , _getI18NMessage :: I18NMessage
- }
-
+data I18N = I18N
+ { _getI18NConfig :: I18NConfig
+ , _getI18NMessage :: I18NMessage
+ }
-- | Compose App with a I18N Snaplet.
--
class HasI18N b where
- i18nLens :: SnapletLens b I18NSnaplet
+ i18nLens :: SnapletLens b I18N
-- | Util functions
--
-getI18NSnaplet :: HasI18N b => Handler b b I18NSnaplet
-getI18NSnaplet = with i18nLens Snap.get
+getI18N :: HasI18N b => Handler b b I18N
+getI18N = with i18nLens Snap.get
-- | Get the @I18NMessage@
--
getI18NMessages :: HasI18N b => Handler b b I18NMessage
-getI18NMessages = liftM _getI18NMessage getI18NSnaplet
+getI18NMessages = liftM _getI18NMessage getI18N
-- | 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
+ (I18NMessage msg) <- getI18NMessages
+ liftIO $ Config.lookupDefault "Error: no value found." msg key
----------------------------------------------------------------------
-- Init Snaplet
----------------------------------------------------------------------
--- | Init this I18NSnaplet snaplet.
+-- | Init this I18N snaplet.
--
-initI18NSnaplet :: (HasHeist b, HasI18N b)
+initI18N :: (HasHeist b, HasI18N b)
=> Maybe Locale -- ^ Locale, default to @defaultLocale@
- -> SnapletInit b I18NSnaplet
-initI18NSnaplet l = makeSnaplet "i18n" description datadir $ do
+ -> SnapletInit b I18N
+initI18N l = makeSnaplet "i18n" description datadir $ do
let i18nConfig = I18NConfig (fromMaybe defaultLocale l) defaultMessageFilePrefix
fp <- getSnapletFilePath
msg <- liftIO $ readMessageFile fp i18nConfig
addDefaultSplices
- return $ I18NSnaplet i18nConfig msg
+ return $ I18N i18nConfig msg
where addDefaultSplices = addSplices [ ("i18n", i18nSplice)
- , ("i18nSpan", i18nSpanSplice)]
+ , ("i18nSpan", i18nSpanSplice)
+ ]
-- config dir for current snaplet
datadir = Just $ liftM (++ "/resources") getDataDir
description = "light weight i18n snaplet"
--------------------------------------------------------
---
-- | Load file
-- server will not be able to start up if dir doesnt exists.
@@ -150,7 +146,7 @@ i18nSplice = do
-- | Splices. use 'span' html element wrap result.
--
-i18nSpanSplice :: HasI18N b => SnapletISplice b -- (Handler b b)
+i18nSpanSplice :: HasI18N b => I.Splice (Handler b b)
i18nSpanSplice = do
input <- getParamNode
v <- lift . lookupI18NValue $ getNameAttr input

0 comments on commit 792d21d

Please sign in to comment.