normalize_language_tag() now returns `BCP 47`_ conformant tags
[docutils.git] / docutils / languages / __init__.py
blob47d896851addfd5d87ea220e57f6bbc6528b5176
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 tag = tag.replace('-','_') # '-' not valid in module names
31 if tag in _languages:
32 return _languages[tag]
33 try:
34 module = __import__(tag, globals(), locals(), level=0)
35 except ImportError:
36 try:
37 module = __import__(tag, globals(), locals(), level=1)
38 except ImportError:
39 continue
40 _languages[tag] = module
41 return module
42 if reporter is not None:
43 reporter.warning(
44 'language "%s" not supported: ' % language_code +
45 'Docutils-generated text will be in English.')
46 module = __import__('en', globals(), locals(), level=1)
47 _languages[tag] = module # warn only one time!
48 return module