Completed
Pull Request — master (#124)
by
unknown
01:13
created

parler.utils.get_language_title()   B

Complexity

Conditions 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
dl 0
loc 23
rs 8.7972
1
"""
2
Utils for translations
3
"""
4
from django.conf import settings
5
from django.conf.global_settings import LANGUAGES as ALL_LANGUAGES
6
from django.utils.translation import ugettext_lazy as _
7
8
__all__ = (
9
    'normalize_language_code',
10
    'is_supported_django_language',
11
    'get_language_title',
12
    'get_language_settings',
13
    'get_active_language_choices',
14
    'is_multilingual_project',
15
)
16
17
18
LANGUAGES_DICT = dict(settings.LANGUAGES)
19
ALL_LANGUAGES_DICT = dict(ALL_LANGUAGES)
20
21
# allow to override language names when  PARLER_SHOW_EXCLUDED_LANGUAGE_TABS is True:
22
ALL_LANGUAGES_DICT.update(LANGUAGES_DICT)
23
24
25
def normalize_language_code(code):
26
    """
27
    Undo the differences between language code notations
28
    """
29
    if code is None:
30
        return None
31
    else:
32
        return code.lower().replace('_', '-')
33
34
35
def is_supported_django_language(language_code):
36
    """
37
    Return whether a language code is supported.
38
    """
39
    language_code2 = language_code.split('-')[0] # e.g. if fr-ca is not supported fallback to fr
40
    return language_code in LANGUAGES_DICT or language_code2 in LANGUAGES_DICT
41
42
43
def get_language_title(language_code):
44
    """
45
    Return the verbose_name for a language code.
46
47
    Fallback to language_code if language is not found in settings.
48
    """
49
    from parler import appsettings
50
    # Avoid weird lookup errors.
51
    if not language_code:
52
        raise KeyError("Missing language_code in get_language_title()")
53
54
    if appsettings.PARLER_SHOW_EXCLUDED_LANGUAGE_TABS:
55
        # this allows to edit languages that are not enabled in current project but are already
56
        # in database
57
        languages = ALL_LANGUAGES_DICT
58
    else:
59
        languages = LANGUAGES_DICT
60
61
    try:
62
        return _(languages[language_code])
63
    except KeyError:
64
        language_code = language_code.split('-')[0]  # e.g. if fr-ca is not supported fallback to fr
65
        return _(languages.get(language_code, language_code))
66
67
68
def get_language_settings(language_code, site_id=None):
69
    """
70
    Return the language settings for the current site
71
    """
72
    # This method mainly exists for ease-of-use.
73
    # the body is part of the settings, to allow third party packages
74
    # to have their own variation of the settings with this method functionality included.
75
    from parler import appsettings
76
    return appsettings.PARLER_LANGUAGES.get_language(language_code, site_id)
77
78
79
def get_active_language_choices(language_code=None):
80
    """
81
    Find out which translations should be visible in the site.
82
    It returns a tuple with either a single choice (the current language),
83
    or a tuple with the current language + fallback language.
84
    """
85
    from parler import appsettings
86
    return appsettings.PARLER_LANGUAGES.get_active_choices(language_code)
87
88
89
def is_multilingual_project(site_id=None):
90
    """
91
    Whether the current Django project is configured for multilingual support.
92
    """
93
    from parler import appsettings
94
    if site_id is None:
95
        site_id = getattr(settings, 'SITE_ID', None)
96
    return appsettings.PARLER_SHOW_EXCLUDED_LANGUAGE_TABS or site_id in appsettings.PARLER_LANGUAGES
97