Search code examples
webhttp-headerstranslationnim-lang

Translation in Happyx


Can I translate strings in happyx web framework?

For example

...
get "/":
  if lang == "en":
    return "Hello, world!"
  else:
    return "Bye"

Now I'm trying to use request headers to detect language, but can I made it otherwise?


Solution

  • You can use translatable strings that available since v1.5.0

    Here is example

    # Declare your translatable strings
    translatable:
      "Hello, world!":  # this string will be used by default
        "ru" -> "Привет"
        "jp" -> "こんにちは"
    
    ...
    
      get "/":
        # In SSR/SSG, language takes from HTTP headers
        # In SPA, language takes from browser
        return translate("Hello, world!")