summaryrefslogtreecommitdiff
path: root/src/zope/i18n/config.py
blob: ce94ded004ac9fe43fda8f33abfca2347de8ec88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os

#: The environment variable that is consulted when this module
#: is imported to determine the value of `COMPILE_MO_FILES`.
#: Simply set this to a non-empty string to make it True.
COMPILE_MO_FILES_KEY = 'zope_i18n_compile_mo_files'
COMPILE_MO_FILES_UNSET = "__unset__"
#: Whether or not the ZCML directives will attempt to compile
#: translation files. Defaults to False.
COMPILE_MO_FILES = COMPILE_MO_FILES_UNSET

#: The environment variable that is consulted when this module
#: is imported to determine the value of `ALLOWED_LANGUAGES`.
#: If set, this should be a comma-separated list of language names.
ALLOWED_LANGUAGES_KEY = 'zope_i18n_allowed_languages'


def _parse_languages(value):
    """
    Utility function to parse languages.

        >>> _parse_languages(None) is None
        True
        >>> _parse_languages("en") == frozenset(('en',))
        True
        >>> _parse_languages('')
        ''
        >>> _parse_languages("en,es") == frozenset(('en', 'es'))
        True

    Leading, trailing and internal whitespace is ignored:

        >>> _parse_languages('en, es') == frozenset(('en', 'es'))
        True
        >>> _parse_languages(" en,es") == frozenset(('en', 'es'))
        True
        >>> _parse_languages("en,es ") == frozenset(('en', 'es'))
        True
    """
    if value:
        value = value.replace(",", " ")
        value = frozenset(value.split())
    return value


#: A set of languages that `zope.i18n.negotiate` will pass to the
#: `zope.i18n.interfaces.INegotiator` utility. If this is None,
#: no utility will be used.
ALLOWED_LANGUAGES = _parse_languages(
    os.environ.get(ALLOWED_LANGUAGES_KEY, None))