forked from django-parler/django-parler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.py
100 lines (81 loc) · 3.17 KB
/
i18n.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Utils for translations
"""
from django.conf import settings
from django.conf.global_settings import LANGUAGES as ALL_LANGUAGES
from django.utils.translation import ugettext_lazy as _
__all__ = (
'normalize_language_code',
'is_supported_django_language',
'get_language_title',
'get_language_settings',
'get_active_language_choices',
'is_multilingual_project',
)
LANGUAGES_DICT = dict(settings.LANGUAGES)
ALL_LANGUAGES_DICT = dict(ALL_LANGUAGES)
# allow to override language names when PARLER_SHOW_EXCLUDED_LANGUAGE_TABS is True:
ALL_LANGUAGES_DICT.update(LANGUAGES_DICT)
def normalize_language_code(code):
"""
Undo the differences between language code notations
"""
if code is None:
return None
else:
return code.lower().replace('_', '-')
def is_supported_django_language(language_code):
"""
Return whether a language code is supported.
"""
language_code2 = language_code.split('-')[0] # e.g. if fr-ca is not supported fallback to fr
return language_code in LANGUAGES_DICT or language_code2 in LANGUAGES_DICT
def get_language_title(language_code):
"""
Return the verbose_name for a language code.
Fallback to language_code if language is not found in settings.
"""
from parler import appsettings
# Avoid weird lookup errors.
if not language_code:
raise KeyError("Missing language_code in get_language_title()")
if appsettings.PARLER_SHOW_EXCLUDED_LANGUAGE_TABS:
# this allows to edit languages that are not enabled in current project but are already
# in database
languages = ALL_LANGUAGES_DICT
else:
languages = LANGUAGES_DICT
try:
return _(languages[language_code])
except KeyError:
language_code = language_code.split('-')[0] # e.g. if fr-ca is not supported fallback to fr
language_title = languages.get(language_code, None)
if language_title is not None:
return _(language_title)
else:
return language_code
def get_language_settings(language_code, site_id=None):
"""
Return the language settings for the current site
"""
# This method mainly exists for ease-of-use.
# the body is part of the settings, to allow third party packages
# to have their own variation of the settings with this method functionality included.
from parler import appsettings
return appsettings.PARLER_LANGUAGES.get_language(language_code, site_id)
def get_active_language_choices(language_code=None):
"""
Find out which translations should be visible in the site.
It returns a tuple with either a single choice (the current language),
or a tuple with the current language + fallback language.
"""
from parler import appsettings
return appsettings.PARLER_LANGUAGES.get_active_choices(language_code)
def is_multilingual_project(site_id=None):
"""
Whether the current Django project is configured for multilingual support.
"""
from parler import appsettings
if site_id is None:
site_id = getattr(settings, 'SITE_ID', None)
return appsettings.PARLER_SHOW_EXCLUDED_LANGUAGE_TABS or site_id in appsettings.PARLER_LANGUAGES