i18n.py 8.8 KB

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