让message/en.msg
文件如:
Category1: some text 1
Category2: some text 2
...
CategoryN: some text N
让下一个有效代码:
getHomeR :: Handler RepHtml
getHomeR = do
(msg :: AppMessage -> Text) <- getMessageRender
let list = T.concat $ map msg [MsgCategory1, MsgCategory7]
defaultLayout $ do
$(widgetFile "homepage") -- <p>List: #{list}
然后,list
包含MsgCategory1
和MsgCategory7
翻译。
我想做一些像:
let list = T.concat $ map msg [MsgCategory1 .. MsgCategory7]
但AppMessage
不是Enum
派生的。
我的另一个替代方案也无效(也不是Read
派生的)
let list = T.concat $ map (\n -> msg $ read "MsgCategory" ++ show n) [1 .. 7]
一般来说,如何投射“实时”AppMessage
元素?
非常感谢!
(我喜欢Yesod!:))
答案 0 :(得分:0)
我发现的一种方法是here
deriving instance Enum AppMessage
deriving instance Eq AppMessage
deriving instance Read AppMessage
deriving instance Show AppMessage
...
(需要-XStandaloneDeriving)
说明:
更改Foundation.hs
:
-- Set up i18n messages. See the message folder.
mkMessage "App" "messages" "en"
-- Require extension: StandaloneDeriving
deriving instance Enum AppMessage
在site.cabal
:
extensions: TemplateHaskell
QuasiQuotes
OverloadedStrings
NoImplicitPrelude
CPP
MultiParamTypeClasses
TypeFamilies
GADTs
GeneralizedNewtypeDeriving
FlexibleContexts
EmptyDataDecls
NoMonomorphismRestriction
StandaloneDeriving