Permalink
Browse files

Fixes, use ByteString interface of Si18n by default

  • Loading branch information...
1 parent a243b14 commit 410920c4d5aface93135779fbc837fc12674ff84 @ozataman committed Dec 22, 2010
Showing with 72 additions and 6 deletions.
  1. +30 −0 LICENSE
  2. +10 −0 README.md
  3. +2 −0 Setup.hs
  4. +2 −1 snap-extension-si18n.cabal
  5. +16 −2 src/Snap/Extension/Si18n.hs
  6. +12 −3 src/Snap/Extension/Si18n/Impl.hs
View
30 LICENSE
@@ -0,0 +1,30 @@
+Copyright (c)2010, Ozgun Ataman
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of Ozgun Ataman nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
@@ -0,0 +1,10 @@
+# Simple I18n For Snap Applications
+
+
+## Introduction
+
+
+## Status
+
+
+## TODO
View
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
@@ -1,5 +1,5 @@
Name: snap-extension-si18n
-Version: 0.1
+Version: 0.0.1
Synopsis: Simple I18n framework for Snap
Homepage: https://github.com/ozataman/snap-extension-si18n
License: BSD3
@@ -25,6 +25,7 @@ Library
, bytestring >= 0.9
, containers >= 0.3
, directory
+ , filepath
, mtl
, si18n
, snap >= 0.3 && < 0.4
@@ -9,14 +9,28 @@ module Snap.Extension.Si18n
import Snap.Types
import Text.Si18n
+import Data.ByteString (ByteString)
import Data.Text (Text)
------------------------------------------------------------------------------
--- | The 'MonadSi18n' class.
+-- | The 'MonadSi18n' class. Minimum complete definition: 'translate'
class MonadSnap m => MonadSi18n m where
- translate :: Text -> Text -> TokenMap -> m Text
+ ----------------------------------------------------------------------------
+ -- | Translate given key using the i18n data buried in app's state
+ translate :: Text -- ^ Locale
+ -> Text -- ^ Key
+ -> TokenMap -- ^ Substitution tokens
+ -> m Text
+
+
+ ----------------------------------------------------------------------------
+ -- | Same as 'translate' but instead uses ByteString (UTF8 encoded) I/O.
+ translate' :: ByteString -- ^ Locale
+ -> ByteString -- ^ Key
+ -> [(ByteString, ByteString)] -- ^ Substitution tokens
+ -> m ByteString
@@ -11,9 +11,12 @@ import Control.Monad.IO.Class
import Control.Monad.Reader
import Control.Monad.Trans
import qualified Data.Map as Map
+import Data.ByteString (ByteString)
import Data.Text (Text)
import qualified Data.Text as T
+import Data.Text.Encoding
import System.Directory
+import System.FilePath.Posix
import Snap.Extension
import Snap.Types
@@ -28,7 +31,7 @@ import Text.Si18n (I18nData)
-- |
data Si18nState = Si18nState
{ si18nData :: I18nData
- , si18nDefLocale :: Text
+ , si18nDefLocale :: ByteString
}
@@ -41,7 +44,7 @@ class HasSi18nState s where
------------------------------------------------------------------------------
si18nStateInitializer :: FilePath
-- ^ Path to directory containing locale files.
- -> Text
+ -> ByteString
-- ^ Default locale
-> Initializer Si18nState
si18nStateInitializer fp l = do
@@ -52,7 +55,7 @@ si18nStateInitializer fp l = do
mkInitializer st
where
step a x = do
- d <- liftIO $ S.loadI18nFile x
+ d <- liftIO $ S.loadI18nFile (fp </> x)
return $ a `Map.union` d
@@ -72,3 +75,9 @@ instance HasSi18nState s => MonadSi18n (SnapExtend s) where
i18n <- fmap si18nData $ asks getSi18nState
return $ S.t i18n l s m
+ translate' l s m = liftM encodeUtf8 $ translate l' s' m'
+ where
+ (l':s':_) = map decodeUtf8 [l, s]
+ m' = map (\(k,v) -> (decodeUtf8 k, decodeUtf8 v)) m
+
+

0 comments on commit 410920c

Please sign in to comment.