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