1''' 2Provides support for internationalization using the Python gettext 3module. Configures gettext to use the translation associated with the locale 4set by the user's environment at runtime. If no appropriate translation is 5found, the default language is used: the one in which the strings in the 6code are written (i.e. English). 7 8The gettext function in the gettext module is alised as _ to match the name of 9the equivalent C function provided by GNU gettext tools. 10 11See the Python documentation on the gettext module at 12U{http://docs.python.org/lib/module-gettext.html}. A brief tutorial on 13i18n and Python is available at 14U{http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq22.002.htp} 15 16@author: Peter Parente 17@organization: IBM Corporation 18@copyright: Copyright (c) 2005, 2007 IBM Corporation 19@license: The BSD License 20 21All rights reserved. This program and the accompanying materials are made 22available under the terms of the BSD license which accompanies 23this distribution, and is available at 24U{http://www.opensource.org/licenses/bsd-license.php} 25''' 26import sys, locale, gettext, os.path 27from gettext import gettext as translate_func 28 29def bind(domain, locale_dir): 30 ''' 31 Convenience function for creating a new instance of a Python translation 32 class bound to a domain and locale directory other than the default one for 33 LSR. This function is useful in L{UIElement}s which have translatable strings 34 and ship separately from LSR with their own translation files. For instance, 35 a L{Perk} writer may call this function globally in his L{Perk} and assign 36 the result to a global variable _ to use the GNU translation system without 37 affecting how other components of LSR are translated. 38 39 @param domain: Filename of the translation file, typically the name of the 40 project with which it is associated 41 @type domain: string 42 @param locale_dir: Directory to search for translation files for the domain 43 @type locale_dir: string 44 @return: Bound method gettext on the translation object 45 @rtype: callable 46 ''' 47 try: 48 t = gettext.translation(domain, locale_dir) 49 return t.gettext 50 except IOError: 51 # no translation file, so just return the unicode version of the given 52 # string 53 return lambda x: str(x) 54 55# use the current system locale 56locale.setlocale(locale.LC_ALL, '') 57# build the path to where the main translation file lives 58LOCALE_DIR = "@localedir@".replace('${prefix}', '@prefix@') 59DOMAIN = '@GETTEXT_PACKAGE@' 60# build a default instance to the LSR domain and locale directory 61_ = bind(DOMAIN, LOCALE_DIR) 62gettext.bindtextdomain (DOMAIN, LOCALE_DIR) 63gettext.textdomain(DOMAIN) 64 65def C_(ctx, s): 66 '''Provide qualified translatable strings. Some strings translate to 67 more than one string in another locale. We provide a convention to 68 provide contextual information for the string so that translators can 69 do the right thing.''' 70 translated = translate_func('%s\x04%s' % (ctx, s)) 71 if '\x04' in translated: 72 # no translation found, return input string 73 return s 74 return translated 75 76# allow gettext to extract the string, but it should not be translated inline. 77N_ = lambda string: string 78