i18n.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import itertools
  2. import json
  3. import os
  4. import re
  5. from urllib.parse import unquote
  6. from django.apps import apps
  7. from django.conf import settings
  8. from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
  9. from django.template import Context, Engine
  10. from django.urls import translate_url
  11. from django.utils.formats import get_format
  12. from django.utils.http import url_has_allowed_host_and_scheme
  13. from django.utils.translation import (
  14. LANGUAGE_SESSION_KEY, check_for_language, get_language,
  15. )
  16. from django.utils.translation.trans_real import DjangoTranslation
  17. from django.views.generic import View
  18. LANGUAGE_QUERY_PARAMETER = 'language'
  19. def set_language(request):
  20. """
  21. Redirect to a given URL while setting the chosen language in the session
  22. (if enabled) and in a cookie. The URL and the language code need to be
  23. specified in the request parameters.
  24. Since this view changes how the user will see the rest of the site, it must
  25. only be accessed as a POST request. If called as a GET request, it will
  26. redirect to the page in the request (the 'next' parameter) without changing
  27. any state.
  28. """
  29. next = request.POST.get('next', request.GET.get('next'))
  30. if (
  31. (next or not request.is_ajax()) and
  32. not url_has_allowed_host_and_scheme(
  33. url=next, allowed_hosts={request.get_host()}, require_https=request.is_secure(),
  34. )
  35. ):
  36. next = request.META.get('HTTP_REFERER')
  37. next = next and unquote(next) # HTTP_REFERER may be encoded.
  38. if not url_has_allowed_host_and_scheme(
  39. url=next, allowed_hosts={request.get_host()}, require_https=request.is_secure(),
  40. ):
  41. next = '/'
  42. response = HttpResponseRedirect(next) if next else HttpResponse(status=204)
  43. if request.method == 'POST':
  44. lang_code = request.POST.get(LANGUAGE_QUERY_PARAMETER)
  45. if lang_code and check_for_language(lang_code):
  46. if next:
  47. next_trans = translate_url(next, lang_code)
  48. if next_trans != next:
  49. response = HttpResponseRedirect(next_trans)
  50. if hasattr(request, 'session'):
  51. # Storing the language in the session is deprecated.
  52. # (RemovedInDjango40Warning)
  53. request.session[LANGUAGE_SESSION_KEY] = lang_code
  54. response.set_cookie(
  55. settings.LANGUAGE_COOKIE_NAME, lang_code,
  56. max_age=settings.LANGUAGE_COOKIE_AGE,
  57. path=settings.LANGUAGE_COOKIE_PATH,
  58. domain=settings.LANGUAGE_COOKIE_DOMAIN,
  59. secure=settings.LANGUAGE_COOKIE_SECURE,
  60. httponly=settings.LANGUAGE_COOKIE_HTTPONLY,
  61. samesite=settings.LANGUAGE_COOKIE_SAMESITE,
  62. )
  63. return response
  64. def get_formats():
  65. """Return all formats strings required for i18n to work."""
  66. FORMAT_SETTINGS = (
  67. 'DATE_FORMAT', 'DATETIME_FORMAT', 'TIME_FORMAT',
  68. 'YEAR_MONTH_FORMAT', 'MONTH_DAY_FORMAT', 'SHORT_DATE_FORMAT',
  69. 'SHORT_DATETIME_FORMAT', 'FIRST_DAY_OF_WEEK', 'DECIMAL_SEPARATOR',
  70. 'THOUSAND_SEPARATOR', 'NUMBER_GROUPING',
  71. 'DATE_INPUT_FORMATS', 'TIME_INPUT_FORMATS', 'DATETIME_INPUT_FORMATS'
  72. )
  73. return {attr: get_format(attr) for attr in FORMAT_SETTINGS}
  74. js_catalog_template = r"""
  75. {% autoescape off %}
  76. (function(globals) {
  77. var django = globals.django || (globals.django = {});
  78. {% if plural %}
  79. django.pluralidx = function(n) {
  80. var v={{ plural }};
  81. if (typeof(v) == 'boolean') {
  82. return v ? 1 : 0;
  83. } else {
  84. return v;
  85. }
  86. };
  87. {% else %}
  88. django.pluralidx = function(count) { return (count == 1) ? 0 : 1; };
  89. {% endif %}
  90. /* gettext library */
  91. django.catalog = django.catalog || {};
  92. {% if catalog_str %}
  93. var newcatalog = {{ catalog_str }};
  94. for (var key in newcatalog) {
  95. django.catalog[key] = newcatalog[key];
  96. }
  97. {% endif %}
  98. if (!django.jsi18n_initialized) {
  99. django.gettext = function(msgid) {
  100. var value = django.catalog[msgid];
  101. if (typeof(value) == 'undefined') {
  102. return msgid;
  103. } else {
  104. return (typeof(value) == 'string') ? value : value[0];
  105. }
  106. };
  107. django.ngettext = function(singular, plural, count) {
  108. var value = django.catalog[singular];
  109. if (typeof(value) == 'undefined') {
  110. return (count == 1) ? singular : plural;
  111. } else {
  112. return value.constructor === Array ? value[django.pluralidx(count)] : value;
  113. }
  114. };
  115. django.gettext_noop = function(msgid) { return msgid; };
  116. django.pgettext = function(context, msgid) {
  117. var value = django.gettext(context + '\x04' + msgid);
  118. if (value.indexOf('\x04') != -1) {
  119. value = msgid;
  120. }
  121. return value;
  122. };
  123. django.npgettext = function(context, singular, plural, count) {
  124. var value = django.ngettext(context + '\x04' + singular, context + '\x04' + plural, count);
  125. if (value.indexOf('\x04') != -1) {
  126. value = django.ngettext(singular, plural, count);
  127. }
  128. return value;
  129. };
  130. django.interpolate = function(fmt, obj, named) {
  131. if (named) {
  132. return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])});
  133. } else {
  134. return fmt.replace(/%s/g, function(match){return String(obj.shift())});
  135. }
  136. };
  137. /* formatting library */
  138. django.formats = {{ formats_str }};
  139. django.get_format = function(format_type) {
  140. var value = django.formats[format_type];
  141. if (typeof(value) == 'undefined') {
  142. return format_type;
  143. } else {
  144. return value;
  145. }
  146. };
  147. /* add to global namespace */
  148. globals.pluralidx = django.pluralidx;
  149. globals.gettext = django.gettext;
  150. globals.ngettext = django.ngettext;
  151. globals.gettext_noop = django.gettext_noop;
  152. globals.pgettext = django.pgettext;
  153. globals.npgettext = django.npgettext;
  154. globals.interpolate = django.interpolate;
  155. globals.get_format = django.get_format;
  156. django.jsi18n_initialized = true;
  157. }
  158. }(this));
  159. {% endautoescape %}
  160. """
  161. class JavaScriptCatalog(View):
  162. """
  163. Return the selected language catalog as a JavaScript library.
  164. Receive the list of packages to check for translations in the `packages`
  165. kwarg either from the extra dictionary passed to the url() function or as a
  166. plus-sign delimited string from the request. Default is 'django.conf'.
  167. You can override the gettext domain for this view, but usually you don't
  168. want to do that as JavaScript messages go to the djangojs domain. This
  169. might be needed if you deliver your JavaScript source from Django templates.
  170. """
  171. domain = 'djangojs'
  172. packages = None
  173. def get(self, request, *args, **kwargs):
  174. locale = get_language()
  175. domain = kwargs.get('domain', self.domain)
  176. # If packages are not provided, default to all installed packages, as
  177. # DjangoTranslation without localedirs harvests them all.
  178. packages = kwargs.get('packages', '')
  179. packages = packages.split('+') if packages else self.packages
  180. paths = self.get_paths(packages) if packages else None
  181. self.translation = DjangoTranslation(locale, domain=domain, localedirs=paths)
  182. context = self.get_context_data(**kwargs)
  183. return self.render_to_response(context)
  184. def get_paths(self, packages):
  185. allowable_packages = {app_config.name: app_config for app_config in apps.get_app_configs()}
  186. app_configs = [allowable_packages[p] for p in packages if p in allowable_packages]
  187. if len(app_configs) < len(packages):
  188. excluded = [p for p in packages if p not in allowable_packages]
  189. raise ValueError(
  190. 'Invalid package(s) provided to JavaScriptCatalog: %s' % ','.join(excluded)
  191. )
  192. # paths of requested packages
  193. return [os.path.join(app.path, 'locale') for app in app_configs]
  194. @property
  195. def _num_plurals(self):
  196. """
  197. Return the number of plurals for this catalog language, or 2 if no
  198. plural string is available.
  199. """
  200. match = re.search(r'nplurals=\s*(\d+)', self._plural_string or '')
  201. if match:
  202. return int(match.groups()[0])
  203. return 2
  204. @property
  205. def _plural_string(self):
  206. """
  207. Return the plural string (including nplurals) for this catalog language,
  208. or None if no plural string is available.
  209. """
  210. if '' in self.translation._catalog:
  211. for line in self.translation._catalog[''].split('\n'):
  212. if line.startswith('Plural-Forms:'):
  213. return line.split(':', 1)[1].strip()
  214. return None
  215. def get_plural(self):
  216. plural = self._plural_string
  217. if plural is not None:
  218. # This should be a compiled function of a typical plural-form:
  219. # Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 :
  220. # n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
  221. plural = [el.strip() for el in plural.split(';') if el.strip().startswith('plural=')][0].split('=', 1)[1]
  222. return plural
  223. def get_catalog(self):
  224. pdict = {}
  225. num_plurals = self._num_plurals
  226. catalog = {}
  227. trans_cat = self.translation._catalog
  228. trans_fallback_cat = self.translation._fallback._catalog if self.translation._fallback else {}
  229. seen_keys = set()
  230. for key, value in itertools.chain(trans_cat.items(), trans_fallback_cat.items()):
  231. if key == '' or key in seen_keys:
  232. continue
  233. if isinstance(key, str):
  234. catalog[key] = value
  235. elif isinstance(key, tuple):
  236. msgid, cnt = key
  237. pdict.setdefault(msgid, {})[cnt] = value
  238. else:
  239. raise TypeError(key)
  240. seen_keys.add(key)
  241. for k, v in pdict.items():
  242. catalog[k] = [v.get(i, '') for i in range(num_plurals)]
  243. return catalog
  244. def get_context_data(self, **kwargs):
  245. return {
  246. 'catalog': self.get_catalog(),
  247. 'formats': get_formats(),
  248. 'plural': self.get_plural(),
  249. }
  250. def render_to_response(self, context, **response_kwargs):
  251. def indent(s):
  252. return s.replace('\n', '\n ')
  253. template = Engine().from_string(js_catalog_template)
  254. context['catalog_str'] = indent(
  255. json.dumps(context['catalog'], sort_keys=True, indent=2)
  256. ) if context['catalog'] else None
  257. context['formats_str'] = indent(json.dumps(context['formats'], sort_keys=True, indent=2))
  258. return HttpResponse(template.render(Context(context)), 'text/javascript; charset="utf-8"')
  259. class JSONCatalog(JavaScriptCatalog):
  260. """
  261. Return the selected language catalog as a JSON object.
  262. Receive the same parameters as JavaScriptCatalog and return a response
  263. with a JSON object of the following format:
  264. {
  265. "catalog": {
  266. # Translations catalog
  267. },
  268. "formats": {
  269. # Language formats for date, time, etc.
  270. },
  271. "plural": '...' # Expression for plural forms, or null.
  272. }
  273. """
  274. def render_to_response(self, context, **response_kwargs):
  275. return JsonResponse(context)