|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (C) 2007 Edgewall Software |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# This software is licensed as described in the file COPYING, which |
| 7 | +# you should have received as part of this distribution. The terms |
| 8 | +# are also available at http://babel.edgewall.org/wiki/License. |
| 9 | +# |
| 10 | +# This software consists of voluntary contributions made by many |
| 11 | +# individuals. For the exact contribution history, see the revision |
| 12 | +# history and logs, available at http://babel.edgewall.org/log/. |
| 13 | + |
| 14 | +"""Support for ``gettext`` message catalogs.""" |
| 15 | + |
| 16 | +import gettext |
| 17 | + |
| 18 | +__all__ = ['Translations'] |
| 19 | + |
| 20 | +DEFAULT_DOMAIN = 'messages' |
| 21 | + |
| 22 | + |
| 23 | +class Translations(gettext.GNUTranslations): |
| 24 | + """An extended translation catalog class.""" |
| 25 | + |
| 26 | + def __init__(self, fileobj=None): |
| 27 | + """Initialize the translations catalog. |
| 28 | + |
| 29 | + :param fileobj: the file-like object the translation should be read |
| 30 | + from |
| 31 | + """ |
| 32 | + GNUTranslations.__init__(self, fp=fileobj) |
| 33 | + self.files = [getattr(fileobj, 'name')] |
| 34 | + |
| 35 | + def load(cls, dirname=None, locales=None, domain=DEFAULT_DOMAIN): |
| 36 | + """Load translations from the given directory. |
| 37 | + |
| 38 | + :param dirname: the directory containing the ``MO`` files |
| 39 | + :param locales: the list of locales in order of preference (items in |
| 40 | + this list can be either `Locale` objects or locale |
| 41 | + strings) |
| 42 | + :param domain: the message domain |
| 43 | + :return: the loaded catalog, or a ``NullTranslations`` instance if no |
| 44 | + matching translations were found |
| 45 | + :rtype: `Translations` |
| 46 | + """ |
| 47 | + locales = [str(locale) for locale in locales] |
| 48 | + filename = gettext.find(domain, dirname, locales) |
| 49 | + if not filename: |
| 50 | + return NullTranslations() |
| 51 | + return cls(open(filename, 'rb')) |
| 52 | + load = classmethod(load) |
| 53 | + |
| 54 | + def merge(self, translations): |
| 55 | + """Merge the given translations into the catalog. |
| 56 | + |
| 57 | + Message translations in the specfied catalog override any messages with |
| 58 | + the same identifier in the existing catalog. |
| 59 | + |
| 60 | + :param translations: the `Translations` instance with the messages to |
| 61 | + merge |
| 62 | + :return: the `Translations` instance (``self``) so that `merge` calls |
| 63 | + can be easily chained |
| 64 | + :rtype: `Translations` |
| 65 | + """ |
| 66 | + if isinstance(translations, Translations): |
| 67 | + self._catalog.update(translations._catalog) |
| 68 | + self.files.extend(translations.files) |
| 69 | + return self |
| 70 | + |
| 71 | + def __repr__(self): |
| 72 | + return "<%s %r>" % (type(self).__name__) |
0 commit comments