blob: c2a549ff689360268c0daa4aa5740f998e4c963d [file] [log] [blame]
Barry Warsaw95be23d2000-08-25 19:13:37 +00001"""Internationalization and localization support.
2
3This module provides internationalization (I18N) and localization (L10N)
4support for your Python programs by providing an interface to the GNU gettext
5message catalog library.
6
7I18N refers to the operation by which a program is made aware of multiple
8languages. L10N refers to the adaptation of your program, once
Barry Warsaw33d8d702000-08-30 03:29:58 +00009internationalized, to the local language and cultural habits.
Barry Warsaw95be23d2000-08-25 19:13:37 +000010
11"""
12
Barry Warsawfa488ec2000-08-25 20:26:43 +000013# This module represents the integration of work, contributions, feedback, and
14# suggestions from the following people:
Barry Warsaw95be23d2000-08-25 19:13:37 +000015#
16# Martin von Loewis, who wrote the initial implementation of the underlying
17# C-based libintlmodule (later renamed _gettext), along with a skeletal
18# gettext.py implementation.
19#
20# Peter Funk, who wrote fintl.py, a fairly complete wrapper around intlmodule,
21# which also included a pure-Python implementation to read .mo files if
22# intlmodule wasn't available.
23#
24# James Henstridge, who also wrote a gettext.py module, which has some
25# interesting, but currently unsupported experimental features: the notion of
26# a Catalog class and instances, and the ability to add to a catalog file via
27# a Python API.
28#
29# Barry Warsaw integrated these modules, wrote the .install() API and code,
30# and conformed all C and Python code to Python's coding standards.
Barry Warsaw33d8d702000-08-30 03:29:58 +000031#
32# Francois Pinard and Marc-Andre Lemburg also contributed valuably to this
33# module.
34#
35# TODO:
36# - Lazy loading of .mo files. Currently the entire catalog is loaded into
37# memory, but that's probably bad for large translated programs. Instead,
38# the lexical sort of original strings in GNU .mo files should be exploited
39# to do binary searches and lazy initializations. Or you might want to use
40# the undocumented double-hash algorithm for .mo files with hash tables, but
41# you'll need to study the GNU gettext code to do this.
42#
43# - Support Solaris .mo file formats. Unfortunately, we've been unable to
44# find this format documented anywhere.
Barry Warsaw95be23d2000-08-25 19:13:37 +000045
46import os
47import sys
48import struct
Barry Warsaw33d8d702000-08-30 03:29:58 +000049from errno import ENOENT
Barry Warsaw95be23d2000-08-25 19:13:37 +000050
Barry Warsaw33d8d702000-08-30 03:29:58 +000051_default_localedir = os.path.join(sys.prefix, 'share', 'locale')
Barry Warsaw95be23d2000-08-25 19:13:37 +000052
53
54
Barry Warsawfa488ec2000-08-25 20:26:43 +000055def _expand_lang(locale):
56 from locale import normalize
57 locale = normalize(locale)
58 COMPONENT_CODESET = 1 << 0
59 COMPONENT_TERRITORY = 1 << 1
60 COMPONENT_MODIFIER = 1 << 2
61 # split up the locale into its base components
62 mask = 0
63 pos = locale.find('@')
64 if pos >= 0:
65 modifier = locale[pos:]
66 locale = locale[:pos]
67 mask |= COMPONENT_MODIFIER
68 else:
69 modifier = ''
70 pos = locale.find('.')
71 if pos >= 0:
72 codeset = locale[pos:]
73 locale = locale[:pos]
74 mask |= COMPONENT_CODESET
75 else:
76 codeset = ''
77 pos = locale.find('_')
78 if pos >= 0:
79 territory = locale[pos:]
80 locale = locale[:pos]
81 mask |= COMPONENT_TERRITORY
82 else:
83 territory = ''
84 language = locale
85 ret = []
86 for i in range(mask+1):
87 if not (i & ~mask): # if all components for this combo exist ...
88 val = language
89 if i & COMPONENT_TERRITORY: val += territory
90 if i & COMPONENT_CODESET: val += codeset
91 if i & COMPONENT_MODIFIER: val += modifier
92 ret.append(val)
93 ret.reverse()
94 return ret
95
96
97
Barry Warsaw33d8d702000-08-30 03:29:58 +000098class NullTranslations:
99 def __init__(self, fp=None):
100 self._info = {}
101 self._charset = None
102 if fp:
103 self._parse(fp)
Barry Warsaw95be23d2000-08-25 19:13:37 +0000104
Barry Warsaw33d8d702000-08-30 03:29:58 +0000105 def _parse(self, fp):
106 pass
107
108 def gettext(self, message):
109 return message
110
111 def ugettext(self, message):
112 return unicode(message)
113
114 def info(self):
115 return self._info
116
117 def charset(self):
118 return self._charset
119
120 def install(self, unicode=0):
121 import __builtin__
122 __builtin__.__dict__['_'] = unicode and self.ugettext or self.gettext
123
124
125class GNUTranslations(NullTranslations):
126 # Magic number of .mo files
127 LE_MAGIC = 0x950412de
Barry Warsawb76a55c2000-08-31 10:45:54 +0000128 BE_MAGIC = 0xde120495
Barry Warsaw95be23d2000-08-25 19:13:37 +0000129
130 def _parse(self, fp):
131 """Override this method to support alternative .mo formats."""
132 unpack = struct.unpack
133 filename = getattr(fp, 'name', '')
134 # Parse the .mo file header, which consists of 5 little endian 32
135 # bit words.
Barry Warsaw33d8d702000-08-30 03:29:58 +0000136 self._catalog = catalog = {}
Barry Warsaw95be23d2000-08-25 19:13:37 +0000137 buf = fp.read()
Barry Warsaw33d8d702000-08-30 03:29:58 +0000138 # Are we big endian or little endian?
139 magic = unpack('<i', buf[:4])[0]
140 if magic == self.LE_MAGIC:
141 version, msgcount, masteridx, transidx = unpack('<4i', buf[4:20])
142 ii = '<ii'
143 elif magic == self.BE_MAGIC:
144 version, msgcount, masteridx, transidx = unpack('>4i', buf[4:20])
145 ii = '>ii'
146 else:
Barry Warsaw95be23d2000-08-25 19:13:37 +0000147 raise IOError(0, 'Bad magic number', filename)
148 #
149 # Now put all messages from the .mo file buffer into the catalog
150 # dictionary.
151 for i in xrange(0, msgcount):
Barry Warsaw33d8d702000-08-30 03:29:58 +0000152 mlen, moff = unpack(ii, buf[masteridx:masteridx+8])
153 mend = moff + mlen
154 tlen, toff = unpack(ii, buf[transidx:transidx+8])
155 tend = toff + tlen
Barry Warsaw95be23d2000-08-25 19:13:37 +0000156 if mend < len(buf) and tend < len(buf):
Barry Warsaw33d8d702000-08-30 03:29:58 +0000157 tmsg = buf[toff:tend]
158 catalog[buf[moff:mend]] = tmsg
Barry Warsaw95be23d2000-08-25 19:13:37 +0000159 else:
160 raise IOError(0, 'File is corrupt', filename)
Barry Warsaw33d8d702000-08-30 03:29:58 +0000161 # See if we're looking at GNU .mo conventions for metadata
162 if mlen == 0 and tmsg.lower().startswith('project-id-version:'):
163 # Catalog description
164 for item in tmsg.split('\n'):
165 item = item.strip()
166 if not item:
167 continue
168 k, v = item.split(':', 1)
169 k = k.strip().lower()
170 v = v.strip()
171 self._info[k] = v
172 if k == 'content-type':
173 self._charset = v.split('charset=')[1]
Barry Warsaw95be23d2000-08-25 19:13:37 +0000174 # advance to next entry in the seek tables
Barry Warsawfa488ec2000-08-25 20:26:43 +0000175 masteridx += 8
176 transidx += 8
Barry Warsaw33d8d702000-08-30 03:29:58 +0000177
178 def gettext(self, message):
179 return self._catalog.get(message, message)
180
181 def ugettext(self, message):
182 tmsg = self._catalog.get(message, message)
183 return unicode(tmsg, self._charset)
Barry Warsaw95be23d2000-08-25 19:13:37 +0000184
185
186
Barry Warsaw95be23d2000-08-25 19:13:37 +0000187# Locate a .mo file using the gettext strategy
Barry Warsaw33d8d702000-08-30 03:29:58 +0000188def find(domain, localedir=None, languages=None):
Barry Warsaw95be23d2000-08-25 19:13:37 +0000189 # Get some reasonable defaults for arguments that were not supplied
Barry Warsaw95be23d2000-08-25 19:13:37 +0000190 if localedir is None:
Barry Warsaw33d8d702000-08-30 03:29:58 +0000191 localedir = _default_localedir
Barry Warsaw95be23d2000-08-25 19:13:37 +0000192 if languages is None:
193 languages = []
194 for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
195 val = os.environ.get(envar)
196 if val:
197 languages = val.split(':')
198 break
199 if 'C' not in languages:
200 languages.append('C')
Barry Warsawfa488ec2000-08-25 20:26:43 +0000201 # now normalize and expand the languages
202 langdict = {}
203 for lang in languages:
204 for nelang in _expand_lang(lang):
205 langdict[nelang] = nelang
206 languages = langdict.keys()
Barry Warsaw95be23d2000-08-25 19:13:37 +0000207 # select a language
208 for lang in languages:
209 if lang == 'C':
210 break
Barry Warsaw84314b72000-08-25 19:53:17 +0000211 mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
Barry Warsaw33d8d702000-08-30 03:29:58 +0000212 if os.path.exists(mofile):
213 return mofile
214 return None
Barry Warsaw95be23d2000-08-25 19:13:37 +0000215
216
217
Barry Warsaw33d8d702000-08-30 03:29:58 +0000218# a mapping between absolute .mo file path and Translation object
219_translations = {}
220
221def translation(domain, localedir=None, languages=None, class_=None):
222 if class_ is None:
223 class_ = GNUTranslations
224 mofile = find(domain, localedir, languages)
225 if mofile is None:
226 raise IOError(ENOENT, 'No translation file found for domain', domain)
227 key = os.path.abspath(mofile)
228 # TBD: do we need to worry about the file pointer getting collected?
229 t = _translations.setdefault(key, class_(open(mofile, 'rb')))
230 return t
231
232
233
234def install(domain, localedir=None, unicode=0):
235 translation(domain, localedir).install(unicode)
236
237
238
239# a mapping b/w domains and locale directories
240_localedirs = {}
241# current global domain, `messages' used for compatibility w/ GNU gettext
242_current_domain = 'messages'
Barry Warsaw95be23d2000-08-25 19:13:37 +0000243
244
245def textdomain(domain=None):
Barry Warsaw95be23d2000-08-25 19:13:37 +0000246 global _current_domain
Barry Warsaw33d8d702000-08-30 03:29:58 +0000247 if domain is not None:
Barry Warsaw95be23d2000-08-25 19:13:37 +0000248 _current_domain = domain
Barry Warsaw33d8d702000-08-30 03:29:58 +0000249 return _current_domain
Barry Warsaw95be23d2000-08-25 19:13:37 +0000250
251
Barry Warsaw33d8d702000-08-30 03:29:58 +0000252def bindtextdomain(domain, localedir=None):
253 global _localedirs
254 if localedir is not None:
255 _localedirs[domain] = localedir
256 return _localedirs.get(domain, _default_localedir)
Barry Warsaw95be23d2000-08-25 19:13:37 +0000257
258
259def dgettext(domain, message):
Barry Warsaw33d8d702000-08-30 03:29:58 +0000260 try:
261 t = translation(domain, _localedirs.get(domain, None))
262 except IOError:
263 return message
264 return t.gettext(message)
265
266
267def gettext(message):
268 return dgettext(_current_domain, message)
Barry Warsaw95be23d2000-08-25 19:13:37 +0000269
270
Barry Warsaw33d8d702000-08-30 03:29:58 +0000271# dcgettext() has been deemed unnecessary and is not implemented.
Barry Warsaw95be23d2000-08-25 19:13:37 +0000272
Barry Warsaw33d8d702000-08-30 03:29:58 +0000273# James Henstridge's Catalog constructor from GNOME gettext. Documented usage
274# was:
275#
276# import gettext
277# cat = gettext.Catalog(PACKAGE, localedir=LOCALEDIR)
278# _ = cat.gettext
279# print _('Hello World')
Barry Warsaw95be23d2000-08-25 19:13:37 +0000280
Barry Warsaw33d8d702000-08-30 03:29:58 +0000281# The resulting catalog object currently don't support access through a
282# dictionary API, which was supported (but apparently unused) in GNOME
283# gettext.
Barry Warsaw95be23d2000-08-25 19:13:37 +0000284
Barry Warsaw33d8d702000-08-30 03:29:58 +0000285Catalog = translation