Mercurial > p > roundup > code
view roundup/i18n.py @ 2755:e3cd28cec23d
Clean out sessions / otks tables when migrating
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 14 Oct 2004 22:27:59 +0000 |
| parents | 7c1fbb2b2fc8 |
| children | 845e87d5e3ba |
line wrap: on
line source
# # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/) # This module is free software, and you may redistribute it and/or modify # under the same terms as Python, so long as this copyright message and # disclaimer are retained in their original form. # # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # # $Id: i18n.py,v 1.10 2004-07-14 07:27:21 a1s Exp $ """ RoundUp Internationalization (I18N) To use this module, the following code should be used:: from roundup.i18n import _ ... print _("Some text that can be translated") Note that to enable re-ordering of inserted texts in formatting strings (which can easily happen if a sentence has to be re-ordered due to grammatical changes), translatable formats should use named format specs:: ... _('Index of %(classname)s') % {'classname': cn} ... Also, this eases the job of translators since they have some context what the dynamic portion of a message really means. """ __docformat__ = 'restructuredtext' import errno # Roundup text domain DOMAIN = "roundup" # first, we try to import gettext; this probably never fails, but we make # sure we survive this anyway try: import gettext as gettext_module except ImportError: gettext_module = None # use or emulate features of gettext_module if not gettext_module: # no gettext engine available. # implement emulation for Translations class # and find_translation() function class RoundupNullTranslations: """Dummy Translations class Only methods used by Roundup are implemented. """ def add_fallback(self, fallback): pass def gettext(self, text): return text def ugettext(self, text): return unicode(text) def ngettext(self, singular, plural, count): if count == 1: return singular return plural def ungettext(self, singular, plural, count): return unicode(self.ngettext(singular, plural, count)) RoundupTranslations = RoundupNullTranslations def find_translation(domain, localedir=None, languages=None, class_=None): """Always raise IOError (no message catalogs available)""" raise IOError(errno.ENOENT, "No translation file found for domain", domain) elif not hasattr(gettext_module.GNUTranslations, "ngettext"): # prior to 2.3, there was no plural forms. mix simple emulation in class PluralFormsMixIn: def ngettext(self, singular, plural, count): if count == 1: _msg = singular else: _msg = plural return self.gettext(_msg) def ungettext(self, singular, plural, count): if count == 1: _msg = singular else: _msg = plural return self.ugettext(_msg) class RoundupNullTranslations( gettext_module.NullTranslations, PluralFormsMixIn ): pass class RoundupTranslations( gettext_module.GNUTranslations, PluralFormsMixIn ): pass # lookup function is available find_translation = gettext_module.translation else: # gettext_module has everything needed RoundupNullTranslations = gettext_module.NullTranslations RoundupTranslations = gettext_module.GNUTranslations find_translation = gettext_module.translation def get_translation(language=None, domain=DOMAIN, translation_class=RoundupTranslations, null_translation_class=RoundupNullTranslations ): """Return Translation object for given language and domain Arguments 'translation_class' and 'null_translation_class' specify the classes that are instantiated for existing and non-existing translations, respectively. """ if language: _languages = [language] else: # use OS environment _languages = None # except for english ("en") language, add english fallback if available if language == "en": _fallback = None else: try: _fallback = find_translation(domain=domain, languages=["en"], class_=translation_class) # gettext.translation returns a cached translation # even if it is not of the desired class. # This is a quick-and-dirty solution for this problem. # It works with current codebase, because all translators # inherit from respective base translation classes # defined in the gettext module, i.e. have same internal data. # The cached instance is not affected by this hack, # 'cause gettext made a copy for us. # XXX Consider making a copy of gettext.translation function # with class bug fixed... if _fallback.__class__ != translation_class: _fallback.__class__ = translation_class except IOError: # no .mo files found _fallback = None # get the translation try: _translation = find_translation(domain=domain, languages=_languages, class_=translation_class) # XXX See the comment after first find_translation() call if _translation.__class__ != translation_class: _translation.__class__ = translation_class except IOError: _translation = None # see what's found if _translation and _fallback: _translation.add_fallback(_fallback) elif _fallback: _translation = _fallback elif not _translation: _translation = null_translation_class() return _translation # static translations object translation = get_translation() # static translation functions _ = gettext = translation.gettext ugettext = translation.ugettext ngettext = translation.ngettext ungettext = translation.ungettext # vim: set filetype=python sts=4 sw=4 et si :
