Library: localize

From Yombo
Jump to: navigation, search
Commonly Used No
Status Active

Internationalization and localization (i18n and l10n) for formatting various items for display.


The Localize library is primarily responsible supporting localization and Internationalization. It's easy to add support for this in your module.


No hooks registered.



There are two steps needed to support internationalization:

  1. To output text, simply use the _() method instead of print().
  2. Create a folder inside the module called 'locale'. Inside here, create any local files your want to support.

Using _()

To translate text, simply use the _(). Some examples: print _("Hello {name}.").format(name=users_name) # Note location of paresis! Don't use something like: print (_("Hello") + " %s" % users_name) This won't translate correctly. Also, depending on the language, the location of the user's name may be in a different location. In your local PO file (see next section), you will create two lines

1 #. TRANSLATORS: Used within XYX module to display a welcome home message.
2 msgctxt "mymodulename"
3 msgid "Hello {name}."
4 msgstr "Hola {name}."

Creating Locale Files

Create a folder called locale inside the module directory. Yo will place PO files here. The name of the PO file should be ocale.po. For example, to add translations for English, the file would be "en.po". For US English, it should be "en_US.po". Please follow standard python gettext naming convention: <ISO 639 lang code>_<ISO 3166 REGION CODE (optional)>.po The contents of the PO file are just the msgid's and msgstr's. Any PO file editor will work. Besure to not include the header of the file inside the PO file. If you are supporting a language that Yombo Gateway does not have support for yet, you can add support to get your module working. Note: Only add translations relevant to your module, no Yombo Gateway translations. To add headers, just simple create a file matching the name of the PO file, but with '.head' attached to the end. For example: es.po.head and es.po.head The below example demonstrates how to leave a comment for translators for other languages, a context (msgctxt) and the actual translation:

1 #. TRANSLATORS: Used within XYX module to display a welcome home message.
2 msgctxt "mymodulename"
3 msgid "There is {count} device turned on."
4 msgstr "Hola {name}, bienvenido a casa."
  • "#. TRANSLATORS: " section is used by various PO tools to leave notes for a translator.
  • msgtxt: This allows for context selection (not support yet on output). If for example, you want to use a msgid of "S" for the first letter of South or "S" for the first letter is Soda. A future version of _() will support context selection.
  • msgid: The string you entered into the _() function.
  • msgstr: The translation.


When the gateway starts up, it searches all active modules for language files and combines them together and can be accessible through the _() method.

This page was last edited on 9 December 2017.