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 1: | Line 1: | ||
- | + | [[Category:Internationalization]] | |
+ | [[Category:Developers]] | ||
+ | [[Category:N900]] | ||
- | There are already some | + | = Internationalization of a python application (in Maemo) = |
+ | |||
+ | This page explains step by step add support for translations in a python application for maemo. | ||
+ | |||
+ | There are already some tutorials online about how to internationalize (i18n) python applications, but i find them confusing and they lack some needed code and final tricks to make everything work fine. This is the steps i followed to add i18n in my project Mussorgsky. | ||
To support i18n we need to accomplish 5 tasks: | To support i18n we need to accomplish 5 tasks: | ||
- | # Define correctly a function ' | + | # Define correctly a function '_()' that translates strings |
# Mark the strings in the code with that function | # Mark the strings in the code with that function | ||
# Generate a template for the translators | # Generate a template for the translators | ||
# Add translations | # Add translations | ||
- | # Include the translations in | + | # Include the translations in your installation |
== The overall process == | == The overall process == | ||
- | From the source code, using a command line tool, we will generate a " | + | From the source code, using a command line tool, we will generate a ".pot" file. It is plain text file containing all string that need translation in the project. |
+ | This .pot file only needs to be generated/recreated when the strings change in the code (and _not_ in every build!). There is only one .pot file per project. | ||
- | Then, from the | + | Then, from the .pot files and using another command line tool, we will generate the .po files for each language. There is one .po file per language, and it is the file that the translators need to complete. |
- | But once your program is running, it doesn't use | + | But once your program is running, it doesn't use directly .po files, but a binary (compiled) version of them: the .mo files. These .mo files must be re-created on build time (when creating the package) and installed in the right location in the system. |
- | From the code point of view, you just need to call few functions to tell gettext where are the translation files, what language do you want to use (usually the locale of the environment) | + | From the code point of view, you just need to call few functions to tell gettext where are the translation files, and what language do you want to use (usually the locale of the environment). |
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 == | ||
+ | TODO | ||
== Mark strings for i18n == | == Mark strings for i18n == | ||
- | This is one of the easiest parts. Browse the source code files that show something on the | + | This is one of the easiest parts. Browse the source code files that show something on the string, and wrap the visible strings (window titles, dialog titles, notifications, labels, button labels and so on) with the '_()' function. |
For example: | For example: | ||
- | |||
- | |||
- | |||
- | |||
- | class Example (hildon.StackableWindow): | + | # import the _() function! |
+ | import i18n | ||
+ | _ = i18n.language.gettext | ||
+ | |||
+ | class Example (hildon.StackableWindow): | ||
+ | |||
+ | def __init__ (self): | ||
+ | hildon.StackableWindow.__init__ (self) | ||
+ | self.set_title (_("All music")) | ||
- | + | == Generate template for the translators == | |
- | + | TODO | |
- | + | == Include translations in your installation == | |
- | + | TODO | |
== How to add translations == | == How to add translations == | ||
- | + | TODO | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + |
Learn more about Contributing to the wiki.