1 """If you want your program to be translated into multiple languages you need
4 - Pass all strings that should be translated through the '_' function, eg:
5 print _('Hello World!')
7 - Create a Messages subdirectory in your application.
9 - Run 'pygettext *.py' to extract all the marked strings.
11 - Copy messages.pot as Messages/<lang>.po and edit (see ROX-Lib2's README).
13 - Use msgfmt to convert the .po files to .gmo files.
15 - In your application, use the rox.i18n.translation() function to set the _ function:
16 __builtins__._ = rox.i18n.translation(os.path.join(rox.app_dir, 'Messages'))
17 (for libraries, just do '_ ='; don't mess up the builtins)
19 Note that the marked strings must be fixed. If you're using formats, mark up the
22 print _('You have %d lives remaining') % lives
24 You might like to look at the scripts in ROX-Lib2's Messages directory for
30 def _expand_lang(locale
):
31 from locale
import normalize
32 locale
= normalize(locale
)
33 COMPONENT_CODESET
= 1 << 0
34 COMPONENT_TERRITORY
= 1 << 1
35 COMPONENT_MODIFIER
= 1 << 2
36 # split up the locale into its base components
38 pos
= locale
.find('@')
40 modifier
= locale
[pos
:]
42 mask |
= COMPONENT_MODIFIER
45 pos
= locale
.find('.')
47 codeset
= locale
[pos
:]
49 mask |
= COMPONENT_CODESET
52 pos
= locale
.find('_')
54 territory
= locale
[pos
:]
56 mask |
= COMPONENT_TERRITORY
61 for i
in range(mask
+1):
62 if not (i
& ~mask
): # if all components for this combo exist ...
64 if i
& COMPONENT_TERRITORY
: val
+= territory
65 if i
& COMPONENT_CODESET
: val
+= codeset
66 if i
& COMPONENT_MODIFIER
: val
+= modifier
71 def expand_languages(languages
= None):
72 # Get some reasonable defaults for arguments that were not supplied
75 for envar
in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
76 val
= os
.environ
.get(envar
)
78 languages
= val
.split(':')
80 if 'C' not in languages
:
83 # now normalize and expand the languages
85 for lang
in languages
:
86 for nelang
in _expand_lang(lang
):
87 if nelang
not in nelangs
:
88 nelangs
.append(nelang
)
91 # Locate a .mo file using the ROX strategy
92 def find(messages_dir
, languages
= None):
93 """Look in messages_dir for a .gmo file for the user's preferred language
94 (or override this with the 'languages' argument). Returns the filename, or
95 None if there was no translation."""
97 for lang
in expand_languages(languages
):
100 mofile
= os
.path
.join(messages_dir
, '%s.gmo' % lang
)
101 if os
.path
.exists(mofile
):
105 def translation(messages_dir
, languages
= None):
106 """Load the translation for the user's language and return a function
107 which translates a string into its unicode equivalent."""
108 mofile
= find(messages_dir
, languages
)
112 return gettext
.GNUTranslations(file(mofile
)).ugettext
114 langs
= expand_languages()