snaplet-i18n 0.0.3.1 → 0.0.4
raw patch · 4 files changed
+80/−87 lines, 4 filesdep +lensdep −bytestringdep −data-lensdep −data-lens-templatedep ~heistdep ~snapdep ~snap-loader-static
Dependencies added: lens
Dependencies removed: bytestring, data-lens, data-lens-template, directory, snap-core, snap-loader-dynamic
Dependency ranges changed: heist, snap, snap-loader-static
Files
- example/snap.hs +12/−14
- example/snaplets/heist/templates/index.tpl +22/−27
- snaplet-i18n.cabal +17/−15
- src/Snap/Snaplet/I18N.hs +29/−31
example/snap.hs view
@@ -27,25 +27,24 @@ import qualified Data.Text as T import qualified Data.Text.Encoding as T import qualified Text.XmlHtml as X-import Text.Templating.Heist+import Heist+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 } -makeLens ''App+makeLenses ''App instance HasHeist App where heistLens = subSnaplet heist@@ -63,18 +62,18 @@ ------------------------------------------------------------------------------ -testSplice :: Splice AppHandler+testSplice :: I.Splice AppHandler testSplice = do locale <- liftIO $ getDefaultLocale --liftIO $ print locale- textSplice $ T.pack "test"+ I.textSplice $ T.pack "test" index :: AppHandler () index = do- heistLocal (bindSplice "testSplice" testSplice) $ render "index"+ heistLocal (I.bindSplice "testSplice" testSplice) $ render "index" -- | wrap to element span--- +-- ------------------------------------------------------------------------------ @@ -89,7 +88,7 @@ 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 @@ -117,4 +116,3 @@ (appEnvironment =<< getOther conf) app hPutStrLn stderr $ T.unpack msgs return (site, cleanup)-
example/snaplets/heist/templates/index.tpl view
@@ -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>
snaplet-i18n.cabal view
@@ -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.1+Version: 0.0.4 Description: A light weight i18n snaplet. Synopsis: snaplet-i18n Homepage: https://github.com/HaskellCNOrg/snaplet-i18n@@ -36,20 +36,15 @@ Other-modules: Paths_snaplet_i18n Build-Depends: - base >= 4 && < 5,- snap == 0.9.*,- snap-core == 0.9.*,- snap-loader-dynamic == 0.9.*,- snap-loader-static == 0.9.*,- xmlhtml >= 0.2,- heist >= 0.8.1 && < 0.9,- bytestring >= 0.9 && < 1.0,- data-lens-template >= 2.1 && < 2.2,- data-lens >= 2.0 && < 2.1,- filepath >= 1.2 && < 1.4,- directory >= 1.1 && < 1.2,- configurator >= 0.2 && < 0.3,- text >= 0.11 && < 0.12+ base >= 4 && < 5,+ configurator >= 0.2 && < 0.3,+ filepath >= 1.2 && < 1.4,+ heist >= 0.10 && < 0.11,+ lens >= 3.7.0.1 && < 3.8,+ snap >= 0.10 && < 0.11,+ snap-loader-static >= 0.9 && < 0.11,+ text >= 0.11 && < 0.12,+ xmlhtml >= 0.2 if impl(ghc >= 6.12.0) ghc-options: -Wall -fwarn-tabs -funbox-strict-fields@@ -64,3 +59,10 @@ -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source. -- Build-tools: +++++++
src/Snap/Snaplet/I18N.hs view
@@ -2,10 +2,10 @@ module Snap.Snaplet.I18N- ( I18NSnaplet+ ( I18N , HasI18N (..) , I18NMessage (..)- , initI18NSnaplet+ , initI18N , getI18NMessages , lookupI18NValue ) where@@ -13,15 +13,15 @@ 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 Heist+import qualified Heist.Interpreted as I import System.FilePath.Posix-import Text.Templating.Heist 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 @@ -53,57 +53,55 @@ -- | 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 :: Lens b (Snaplet 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- where addDefaultSplices = addSplices [ ("i18n", liftHeist i18nSplice)- , ("i18nSpan", liftHeist i18nSpanSplice)]- -- config dir for snaplet+ return $ I18N i18nConfig msg+ where addDefaultSplices = addSplices [ ("i18n", i18nSplice)+ , ("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.@@ -138,21 +136,21 @@ -- <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.-i18nSplice :: HasI18N b => Splice (Handler b b)+i18nSplice :: HasI18N b => I.Splice (Handler b b) i18nSplice = do input <- getParamNode value <- lift . lookupI18NValue $ getNameAttr input case childElements input of [] -> return [X.TextNode value]- _ -> runChildrenWithText [("i18nValue", value)]+ _ -> I.runChildrenWithText [("i18nValue", value)] -- | Splices. use 'span' html element wrap result. ---i18nSpanSplice :: HasI18N b => Splice (Handler b b)+i18nSpanSplice :: HasI18N b => I.Splice (Handler b b) i18nSpanSplice = do input <- getParamNode- value <- lift . lookupI18NValue $ getNameAttr input- return [X.Element "span" (elementAttrs input) [X.TextNode value]]+ v <- lift . lookupI18NValue $ getNameAttr input+ return [X.Element "span" (elementAttrs input) [X.TextNode v]] -- | Look up 'name' attribute value. --