1

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

4

1 回答 1

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
于 2012-11-26T15:29:17.890 回答