Completed
Push — master ( 828b34...354252 )
by Diederik van der
8s
created

parler.utils.get_language_title()   B

Complexity

Conditions 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 27
rs 8.0894
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
        language_title = languages.get(language_code, None)
66
        if language_title is not None:
67
            return _(language_title)
68
        else:
69
            return language_code
70
71
72
def get_language_settings(language_code, site_id=None):
73
    """
74
    Return the language settings for the current site
75
    """
76
    # This method mainly exists for ease-of-use.
77
    # the body is part of the settings, to allow third party packages
78
    # to have their own variation of the settings with this method functionality included.
79
    from parler import appsettings
80
    return appsettings.PARLER_LANGUAGES.get_language(language_code, site_id)
81
82
83
def get_active_language_choices(language_code=None):
84
    """
85
    Find out which translations should be visible in the site.
86
    It returns a tuple with either a single choice (the current language),
87
    or a tuple with the current language + fallback language.
88
    """
89
    from parler import appsettings
90
    return appsettings.PARLER_LANGUAGES.get_active_choices(language_code)
91
92
93
def is_multilingual_project(site_id=None):
94
    """
95
    Whether the current Django project is configured for multilingual support.
96
    """
97
    from parler import appsettings
98
    if site_id is None:
99
        site_id = getattr(settings, 'SITE_ID', None)
100
    return appsettings.PARLER_SHOW_EXCLUDED_LANGUAGE_TABS or site_id in appsettings.PARLER_LANGUAGES
101