Translating text is a central part of localisation/internationalisation. If you want your app to be available to speakers of multiple languages, you need a way to present written text in their language.

This library is a framework for translating text in Anvil apps. You define a dictionary of translations, tell the library which properties contain text to be translated, then call a function to translate the page.

The Translations module provides the functionality.

To set the translation engine up, you define a dictionary and pass it to set_dictionary:

FRENCH_LOCALE = {
  "Hello, World!": "Bonjour, le monde!",
  "Click Here": "Cliquez Ici",
  "Cancel": "Annuler",
  "Close": "Fermer",
  "Apply": "Appliquer",
  "OK": "OK",
  "Some text to translate": u"Du texte à traduire",
  "Language:": "Langue:",
  "Title": "Titre",
}
Translations.set_dictionary('FR', FRENCH_LOCALE)

For each component property you want to translate, you can register it with the translation library:

Translations.register_translation(self.label_language, 'text')

The component is passed in as an object, and the property name is a string, just like the signature of getattr.

To perform a translation, you use Translations.set_locale, for example:

Translations.set_locale('FR')

would translate all registered components to the FR locale.

There are also functions for translating properties manually - Translations.translate, and Translations._ (the latter is just a shorthand, so you can do _(self.label_1.text).

If you want to add advanced functionality such as punctuation insensitivity or translation of substrings, you probably want to modify the logic in the Translations.translate function.

The Translations library is completely free to use, so click on the clone link and use it in your app!

You can use it from another app by using the App Dependencies dialog.

When you clone the library, you will also get an example app that shows you how to include it and make use of it.