django_patch.py
5.1 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
import os
import gettext as gettext_module
from django.conf import settings
from django.utils import importlib
from django.utils.translation import check_for_language, to_locale, get_language
from django.utils._os import upath
from django.utils import six
from django.views.i18n import render_javascript_catalog
# copypasta z django.views.i18n
# jedyne zmiany to wykomentowany fragment i dodana jedna pętla zamiast
def get_javascript_catalog(locale, domain, packages):
default_locale = to_locale(settings.LANGUAGE_CODE)
packages = [p for p in packages if p == 'django.conf' or p in settings.INSTALLED_APPS]
t = {}
paths = []
en_selected = locale.startswith('en')
en_catalog_missing = True
# paths of requested packages
for package in packages:
p = importlib.import_module(package)
path = os.path.join(os.path.dirname(upath(p.__file__)), 'locale')
paths.append(path)
# add the filesystem paths listed in the LOCALE_PATHS setting
paths.extend(list(reversed(settings.LOCALE_PATHS)))
# # first load all english languages files for defaults
# for path in paths:
# try:
# catalog = gettext_module.translation(domain, path, ['en'])
# t.update(catalog._catalog)
# except IOError:
# pass
# else:
# # 'en' is the selected language and at least one of the packages
# # listed in `packages` has an 'en' catalog
# if en_selected:
# en_catalog_missing = False
# next load the settings.LANGUAGE_CODE translations if it isn't english
# if default_locale != 'en':
# for path in paths:
# try:
# catalog = gettext_module.translation(domain, path, [default_locale])
# except IOError:
# catalog = None
# if catalog is not None:
# t.update(catalog._catalog)
# # last load the currently selected language, if it isn't identical to the default.
# if locale != default_locale:
# # If the currently selected language is English but it doesn't have a
# # translation catalog (presumably due to being the language translated
# # from) then a wrong language catalog might have been loaded in the
# # previous step. It needs to be discarded.
# if en_selected and en_catalog_missing:
# t = {}
# else:
# locale_t = {}
# for path in paths:
# try:
# catalog = gettext_module.translation(domain, path, [locale])
# except IOError:
# catalog = None
# if catalog is not None:
# locale_t.update(catalog._catalog)
# if locale_t:
# t = locale_t
for path in paths:
try:
catalog = gettext_module.translation(domain, path, [locale])
except IOError:
catalog = None
if catalog is not None:
t.update(catalog._catalog)
plural = None
if '' in t:
for l in t[''].split('\n'):
if l.startswith('Plural-Forms:'):
plural = l.split(':', 1)[1].strip()
if plural is not None:
# this should actually be a compiled function of a typical plural-form:
# Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
plural = [el.strip() for el in plural.split(';') if el.strip().startswith('plural=')][0].split('=', 1)[1]
pdict = {}
maxcnts = {}
catalog = {}
for k, v in t.items():
if k == '':
continue
if isinstance(k, six.string_types):
catalog[k] = v
elif isinstance(k, tuple):
msgid = k[0]
cnt = k[1]
maxcnts[msgid] = max(cnt, maxcnts.get(msgid, 0))
pdict.setdefault(msgid, {})[cnt] = v
else:
raise TypeError(k)
for k, v in pdict.items():
catalog[k] = [v.get(i, '') for i in range(maxcnts[msgid] + 1)]
return catalog, plural
def javascript_catalog(request, domain='djangojs', packages=None):
"""
Returns the selected language catalog as a javascript library.
Receives the list of packages to check for translations in the
packages parameter either from an infodict or as a +-delimited
string from the request. Default is 'django.conf'.
Additionally you can override the gettext domain for this view,
but usually you don't want to do that, as JavaScript messages
go to the djangojs domain. But this might be needed if you
deliver your JavaScript source from Django templates.
"""
locale = to_locale(get_language())
if request.GET and 'language' in request.GET:
if check_for_language(request.GET['language']):
locale = to_locale(request.GET['language'])
if packages is None:
packages = ['django.conf']
if isinstance(packages, six.string_types):
packages = packages.split('+')
catalog, plural = get_javascript_catalog(locale, domain, packages)
return render_javascript_catalog(catalog, plural)