Editing Internationalize a Python application

Warning: You are not logged in. Your IP address will be recorded in this page's edit history.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 21: Line 21:
Ok, this is what we need to do. Let's code.
Ok, this is what we need to do. Let's code.
 +
 +
== Configure gettext Define the '_()' function ==
 +
 +
If you don't care too much about the details, just copy this [https://garage.maemo.org/plugins/ggit/browse.php/?p=mussorgsky;a=blob;f=src/i18n.py;hb=HEAD i18n.py] file in your src/ directory ('''change the APP_NAME''' variable there!), or copy the code block below into a file i18n.py:
 +
<source lang="python">
 +
# -*- coding: utf-8 -*-
 +
 +
import os, sys
 +
import locale
 +
import gettext
 +
 +
# Change this variable to your app name!
 +
#  The translation files will be under
 +
#  @LOCALE_DIR@/@LANGUAGE@/LC_MESSAGES/@APP_NAME@.mo
 +
APP_NAME = "SleepAnalyser"
 +
 +
# This is ok for maemo. Not sure in a regular desktop:
 +
#APP_DIR = os.path.join (sys.prefix, 'share')
 +
LOCALE_DIR = os.path.join(APP_DIR, 'i18n') # .mo files will then be located in APP_Dir/i18n/LANGUAGECODE/LC_MESSAGES/
 +
 +
# Now we need to choose the language. We will provide a list, and gettext
 +
# will use the first translation available in the list
 +
#
 +
#  In maemo it is in the LANG environment variable
 +
#  (on desktop is usually LANGUAGES)
 +
DEFAULT_LANGUAGES = os.environ.get('LANG', '').split(':')
 +
DEFAULT_LANGUAGES += ['en_US']
 +
 +
lc, encoding = locale.getdefaultlocale()
 +
if lc:
 +
    languages = [lc]
 +
 +
# Concat all languages (env + default locale),
 +
#  and here we have the languages and location of the translations
 +
languages += DEFAULT_LANGUAGES
 +
mo_location = LOCALE_DIR
 +
 +
# Lets tell those details to gettext
 +
#  (nothing to change here for you)
 +
gettext.install(True, localedir=None, unicode=1)
 +
 +
gettext.find(APP_NAME, mo_location)
 +
 +
gettext.textdomain (APP_NAME)
 +
 +
gettext.bind_textdomain_codeset(APP_NAME, "UTF-8")
 +
 +
language = gettext.translation(APP_NAME, mo_location, languages=languages, fallback=True)
 +
</source>
 +
 +
 +
 +
And in '''every .py''' file that needs to translate any string, add these lines at the top:
 +
<source lang="python">
 +
import i18n
 +
_ = i18n.language.ugettext #use ugettext instead of getttext to avoid unicode errors
 +
</source>
 +
Done. If you are curious about what is going on there, the file has plenty of comments explaining every line.
== Mark strings for i18n ==
== Mark strings for i18n ==

Learn more about Contributing to the wiki.


Please note that all contributions to maemo.org wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see maemo.org wiki:Copyrights for details). Do not submit copyrighted work without permission!


Cancel | Editing help (opens in new window)