枚举AppMessage构造函数

时间:2012-11-26 15:12:15

标签: haskell internationalization yesod

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包含MsgCategory1MsgCategory7翻译。

我想做一些像:

    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!:))

1 个答案:

答案 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