[web-devel] [Yesod] rendering different templates for different languages

Max Cantor mxcantor at gmail.com
Mon Feb 21 05:16:23 CET 2011


This isn't pretty, cut you could do something like:

getRootR = do
  currentLanguage <- getCurrentLanguage
  defaultLayout $ do
    let widget "EN" = $(widgetFile $ "en/homepage")
        widget "ES" = $(widgetFile $ "es/homepage")
        widget "JP" = $(widgetFile $ "jp/homepage")
    ...
  addWidget (widget currentLanguage)

might be able to do some TH to clean it up.  it requires hard coding your languages, but might be a quick little hack to get it working..

max


On Feb 21, 2011, at 5:19 AM, Dmitry Kurochkin wrote:

> Hi all.
> 
> I want a handler to render different templates for different languages.
> I have getCurrentLanguage function and now I try to do something like:
> 
>    getRootR = do
>        currentLanguage <- getCurrentLanguage
>        defaultLayout $ do
>            addWidget $(widgetFile $ currentLanguage ++ "/homepage")
> 
> This results in:
> 
>    GHC stage restriction: `currentLanguage'
>      is used in a top-level splice or annotation,
>      and must be imported, not defined locally
> 
> This makes sense to me, because TH is calculated at compile time. I
> would like to hear ideas how to work around this restriction. Perhaps
> there is an existing solution in Yesod?
> 
> At the moment, the best I could think of is smth like this:
> 
>    getRootR = do
>        currentLanguage <- getCurrentLanguage
>        defaultLayout $ do
>            case currentLanguage of
>                "en" -> addWidget $(widgetFile  "en/homepage")
>                ... and so on for each language ...
> 
> Obviously, this is not a solution taking in account that there are many
> languages and many handlers.
> 
> I was considering creating a global (template file name -> rendered
> template) map. But I am not sure this is really feasible.
> 
> Regards,
>  Dmitry
> 
> _______________________________________________
> web-devel mailing list
> web-devel at haskell.org
> http://www.haskell.org/mailman/listinfo/web-devel




More information about the web-devel mailing list