Fix [3541369] Relative __import__ also with Python 3.3.
[docutils.git] / docutils / languages / __init__.py
blob57d3ec20559368283f4200c51e9e7fb4ebcecbb2
1 # $Id$
2 # Author: David Goodger <goodger@python.org>
3 # Copyright: This module has been placed in the public domain.
5 # Internationalization details are documented in
6 # <http://docutils.sf.net/docs/howto/i18n.html>.
8 """
9 This package contains modules for language-dependent features of Docutils.
10 """
12 __docformat__ = 'reStructuredText'
14 import sys
16 from docutils.utils import normalize_language_tag
17 if sys.version_info < (2,5):
18 from docutils._compat import __import__
20 _languages = {}
22 def get_language(language_code, reporter=None):
23 """Return module with language localizations.
25 `language_code` is a "BCP 47" language tag.
26 If there is no matching module, warn and fall back to English.
27 """
28 # TODO: use a dummy module returning emtpy strings?, configurable?
29 for tag in normalize_language_tag(language_code):
30 if tag in _languages:
31 return _languages[tag]
32 try:
33 module = __import__(tag, globals(), locals(), level=1)
34 except ImportError:
35 continue
36 _languages[tag] = module
37 return module
38 if reporter is not None:
39 reporter.warning(
40 'language "%s" not supported: ' % language_code +
41 'Docutils-generated text will be in English.')
42 module = __import__('en', globals(), locals(), level=1)
43 _languages[tag] = module # warn only one time!
44 return module