Mercurial > babel > mirror
annotate babel/messages/__init__.py @ 56:f40fc143439c trunk
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
author | cmlenz |
---|---|
date | Fri, 08 Jun 2007 11:08:03 +0000 |
parents | 7dbcbc3f07e0 |
children | f088d3518283 |
rev | line source |
---|---|
1 | 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'] | |
56
f40fc143439c
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
54
diff
changeset
|
19 __docformat__ = 'restructuredtext en' |
1 | 20 |
21 DEFAULT_DOMAIN = 'messages' | |
22 | |
23 | |
24 class Translations(gettext.GNUTranslations): | |
25 """An extended translation catalog class.""" | |
26 | |
27 def __init__(self, fileobj=None): | |
28 """Initialize the translations catalog. | |
29 | |
30 :param fileobj: the file-like object the translation should be read | |
31 from | |
32 """ | |
4 | 33 gettext.GNUTranslations.__init__(self, fp=fileobj) |
1 | 34 self.files = [getattr(fileobj, 'name')] |
35 | |
36 def load(cls, dirname=None, locales=None, domain=DEFAULT_DOMAIN): | |
37 """Load translations from the given directory. | |
38 | |
39 :param dirname: the directory containing the ``MO`` files | |
40 :param locales: the list of locales in order of preference (items in | |
41 this list can be either `Locale` objects or locale | |
42 strings) | |
43 :param domain: the message domain | |
44 :return: the loaded catalog, or a ``NullTranslations`` instance if no | |
45 matching translations were found | |
46 :rtype: `Translations` | |
47 """ | |
4 | 48 if not isinstance(locales, (list, tuple)): |
49 locales = [locales] | |
1 | 50 locales = [str(locale) for locale in locales] |
51 filename = gettext.find(domain, dirname, locales) | |
52 if not filename: | |
4 | 53 return gettext.NullTranslations() |
54 return cls(fileobj=open(filename, 'rb')) | |
1 | 55 load = classmethod(load) |
56 | |
57 def merge(self, translations): | |
58 """Merge the given translations into the catalog. | |
59 | |
60 Message translations in the specfied catalog override any messages with | |
61 the same identifier in the existing catalog. | |
62 | |
63 :param translations: the `Translations` instance with the messages to | |
64 merge | |
65 :return: the `Translations` instance (``self``) so that `merge` calls | |
66 can be easily chained | |
67 :rtype: `Translations` | |
68 """ | |
69 if isinstance(translations, Translations): | |
70 self._catalog.update(translations._catalog) | |
71 self.files.extend(translations.files) | |
72 return self | |
73 | |
74 def __repr__(self): | |
75 return "<%s %r>" % (type(self).__name__) |