The first step is the replacement of all strings of the form:
"This needs to be translated"
by the following call (interpreted to be a C macro in Gnu's gettext)
_("This needs to be translated")
which is very simple.
The standard way then requires the use of gettextand results in the creation of ".pot" files (Portable Object Templates) to be copied and translated in ".po" (Portable Object) files by a human translator; these are then converted into ".mo" (Machine Object) files by a compiler. Yet, a few more steps, mostly dealing with directory structures and setting up locales, are needed before one can conclude the process.
I present here a simpler way to proceed which, at a later time, can easily be converted to the standard gettext method as it basically uses the same "_()" notation required by gettext. This was inspired by a comp.lang.python post by Martin v. Löwis to a question I asked almost a year ago.
Consider the following simple (English) program [Note: "." are used to indicate indentation as Blogger appears to eat up leading spaces]
def name():The French translation of this program would be
....print "My name is Andre"
if __name__ == '__main__':
....name()
# -*- coding: latin-1 -*-Without further ado, here's the internationalized version using a poor man's i18n method and demonstrating how one can easily switch between languages:
def name():
....print u"Je m'appelle André"
if __name__ == '__main__':
....name()
from translate import _, selectThe key here is the creation of the simple translate.py module:
def name():
....print _("My name is Andre")
if __name__ == '__main__':
....name()
....select('fr')
....name()
....select('en')
....name()
__language = 'en' # leading double underscore to avoid namespace collisionstogether with the language-specific fr.py module containing a single dictionary whose keys are the original English strings.:
def select(lang):
....global __language
....__language = lang
....if lang == 'fr':
........global fr
........import fr
def _(message):
....if __language == 'en':
........return message
....elif __language == 'fr':
........return fr.translation[message]
# -*- coding: latin-1 -*-That's it! Try it out!
translation = {
"My name is Andre" : u"Je m'appelle André"
}
In conclusion, if you want to make your programs translation friendly, all you have to do is:
- replace all "strings" by _("strings")
- include the statement
"from translate import _, select"
at the beginning of your program. - create a file named "translate.py" containing the following:
def select(lang):and leave the rest to the international users/programmers; you will have done them a huge favour!
....pass
def _(message):
....return message