Skip to content
brendonh edited this page Sep 13, 2010 · 7 revisions

The site tree now has a messages/ directory, with JSON files inside it for each language (en.json is the default).

The JSON files are arbitrarily nested dicts, and the leaf values are strings. Everything should be in UTF-8. You can put array interpolation markers (%s) or dict interpolation markers (%(field)s) in the strings. Example:

{
  "navigation": {
    "Home": "Acceuil",
    "Admin": "Admin"
  },

  "Welcome to our site":
    "Bienvenue sur notre site",

  "You added %(quantity)s of %(name)s to your cart.": 
    "Vous avez ajouté %(quantity)s de %(name)s à votre commande."
}

Warp has some translation files of its own stored elsewhere, starting with the “warp” key. Message loading merges Warp’s files with yours, giving yours priority. So you can put "_warp": {"login": {"Email or password incorrect": "Hah hah you fail"}}} in your own translation file, and it will override Warp’s setting for that exact key ("Email or password incorrect" in _warp:login), but keep everything else in the same dicts.

By default, messages are loaded at startup and then cached in memory. You can add "reloadMessages": True to the config dict in warpconfig.py to reload them at the start of every request, for development.

Okay, using it to translate:

At the start of each request, the session’s language is checked and an appropriate translator function is set as request.translateTerm. It’s also made available in templates as just t. So in a template you can write:

  ${t("You added %(quantity)s of %(name)s to your cart.", name=u"Skin Cream", quantity=3)}

(Note that strings to be interpolated should be unicode)

Or, with a namespace:

  ${t("Home Page", _domain="navigation")}

Thanks to Mako’s filter magic, in simple cases you can also write:

  ${"Welcome to our site" | t}

If a term is missing from the messages, the original term is used instead. However, if you give a domain and it is not found, or you give extra arguments and their interpolation fails, an error message is returned.

Clone this wiki locally