EN or" /> EN or" /> EN or"/>

How to make a language toggle in Yesod?

44 views Asked by At

I want to add a button to my page that says "EN/PL" and when pressed it reloads the page with the other language (one button that performs PL -> EN or EN -> PL depending on current language selection). The problem is that I cannot find any way to query the current language settings so I cannot tell the program which language to switch to. I tried looking on hackage, hoogle and in the Yesod book and I could only find setLanguage, there doesn't seem to be a corresponding getLanguage function.

Is it possible to query the current language setting in Yesod or is there another way to implement a button like this?

1

There are 1 answers

0
Reverent Lapwing On

I managed to solve the issue.

in pl.msg:

OtherLang: en

in en.msg:

OtherLang: pl

And route that sets the language like in the book.

mkYesod "App" [parseRoute|
    /lang/#Text LangR GET

    ...
|]

getLangR :: Text -> Handler ()
getLangR lang = do
    setLanguage lang
    redirect HomeR

Then language preferences can be queries with

do
langs <- languages 
lang <- renderMessage "App" langs OtherLang

...

<a href=@{LangR lang}>