i18n.py 11 KB

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