i18n.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import importlib
  2. import json
  3. import os
  4. import gettext as gettext_module
  5. from django import http
  6. from django.apps import apps
  7. from django.conf import settings
  8. from django.template import Context, Template
  9. from django.utils.translation import check_for_language, to_locale, get_language, LANGUAGE_SESSION_KEY
  10. from django.utils.encoding import smart_text
  11. from django.utils.formats import get_format_modules, get_format
  12. from django.utils._os import upath
  13. from django.utils.http import is_safe_url
  14. from django.utils import six
  15. def set_language(request):
  16. """
  17. Redirect to a given url while setting the chosen language in the
  18. session or 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 = request.POST.get('next', request.GET.get('next'))
  26. if not is_safe_url(url=next, host=request.get_host()):
  27. next = request.META.get('HTTP_REFERER')
  28. if not is_safe_url(url=next, host=request.get_host()):
  29. next = '/'
  30. response = http.HttpResponseRedirect(next)
  31. if request.method == 'POST':
  32. lang_code = request.POST.get('language', None)
  33. if lang_code and check_for_language(lang_code):
  34. if hasattr(request, 'session'):
  35. request.session[LANGUAGE_SESSION_KEY] = lang_code
  36. else:
  37. response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code,
  38. max_age=settings.LANGUAGE_COOKIE_AGE,
  39. path=settings.LANGUAGE_COOKIE_PATH,
  40. domain=settings.LANGUAGE_COOKIE_DOMAIN)
  41. return response
  42. def get_formats():
  43. """
  44. Returns all formats strings required for i18n to work
  45. """
  46. FORMAT_SETTINGS = (
  47. 'DATE_FORMAT', 'DATETIME_FORMAT', 'TIME_FORMAT',
  48. 'YEAR_MONTH_FORMAT', 'MONTH_DAY_FORMAT', 'SHORT_DATE_FORMAT',
  49. 'SHORT_DATETIME_FORMAT', 'FIRST_DAY_OF_WEEK', 'DECIMAL_SEPARATOR',
  50. 'THOUSAND_SEPARATOR', 'NUMBER_GROUPING',
  51. 'DATE_INPUT_FORMATS', 'TIME_INPUT_FORMATS', 'DATETIME_INPUT_FORMATS'
  52. )
  53. result = {}
  54. for module in [settings] + get_format_modules(reverse=True):
  55. for attr in FORMAT_SETTINGS:
  56. result[attr] = get_format(attr)
  57. formats = {}
  58. for k, v in result.items():
  59. if isinstance(v, (six.string_types, int)):
  60. formats[k] = smart_text(v)
  61. elif isinstance(v, (tuple, list)):
  62. formats[k] = [smart_text(value) for value in v]
  63. return formats
  64. js_catalog_template = r"""
  65. {% autoescape off %}
  66. (function (globals) {
  67. var django = globals.django || (globals.django = {});
  68. {% if plural %}
  69. django.pluralidx = function (n) {
  70. var v={{ plural }};
  71. if (typeof(v) == 'boolean') {
  72. return v ? 1 : 0;
  73. } else {
  74. return v;
  75. }
  76. };
  77. {% else %}
  78. django.pluralidx = function (count) { return (count == 1) ? 0 : 1; };
  79. {% endif %}
  80. {% if catalog_str %}
  81. /* gettext library */
  82. django.catalog = {{ catalog_str }};
  83. django.gettext = function (msgid) {
  84. var value = django.catalog[msgid];
  85. if (typeof(value) == 'undefined') {
  86. return msgid;
  87. } else {
  88. return (typeof(value) == 'string') ? value : value[0];
  89. }
  90. };
  91. django.ngettext = function (singular, plural, count) {
  92. var value = django.catalog[singular];
  93. if (typeof(value) == 'undefined') {
  94. return (count == 1) ? singular : plural;
  95. } else {
  96. return value[django.pluralidx(count)];
  97. }
  98. };
  99. django.gettext_noop = function (msgid) { return msgid; };
  100. django.pgettext = function (context, msgid) {
  101. var value = django.gettext(context + '\x04' + msgid);
  102. if (value.indexOf('\x04') != -1) {
  103. value = msgid;
  104. }
  105. return value;
  106. };
  107. django.npgettext = function (context, singular, plural, count) {
  108. var value = django.ngettext(context + '\x04' + singular, context + '\x04' + plural, count);
  109. if (value.indexOf('\x04') != -1) {
  110. value = django.ngettext(singular, plural, count);
  111. }
  112. return value;
  113. };
  114. {% else %}
  115. /* gettext identity library */
  116. django.gettext = function (msgid) { return msgid; };
  117. django.ngettext = function (singular, plural, count) { return (count == 1) ? singular : plural; };
  118. django.gettext_noop = function (msgid) { return msgid; };
  119. django.pgettext = function (context, msgid) { return msgid; };
  120. django.npgettext = function (context, singular, plural, count) { return (count == 1) ? singular : plural; };
  121. {% endif %}
  122. django.interpolate = function (fmt, obj, named) {
  123. if (named) {
  124. return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])});
  125. } else {
  126. return fmt.replace(/%s/g, function(match){return String(obj.shift())});
  127. }
  128. };
  129. /* formatting library */
  130. django.formats = {{ formats_str }};
  131. django.get_format = function (format_type) {
  132. var value = django.formats[format_type];
  133. if (typeof(value) == 'undefined') {
  134. return format_type;
  135. } else {
  136. return value;
  137. }
  138. };
  139. /* add to global namespace */
  140. globals.pluralidx = django.pluralidx;
  141. globals.gettext = django.gettext;
  142. globals.ngettext = django.ngettext;
  143. globals.gettext_noop = django.gettext_noop;
  144. globals.pgettext = django.pgettext;
  145. globals.npgettext = django.npgettext;
  146. globals.interpolate = django.interpolate;
  147. globals.get_format = django.get_format;
  148. }(this));
  149. {% endautoescape %}
  150. """
  151. def render_javascript_catalog(catalog=None, plural=None):
  152. template = Template(js_catalog_template)
  153. indent = lambda s: s.replace('\n', '\n ')
  154. context = Context({
  155. 'catalog_str': indent(json.dumps(
  156. catalog, sort_keys=True, indent=2)) if catalog else None,
  157. 'formats_str': indent(json.dumps(
  158. get_formats(), sort_keys=True, indent=2)),
  159. 'plural': plural,
  160. })
  161. return http.HttpResponse(template.render(context), 'text/javascript')
  162. def get_javascript_catalog(locale, domain, packages):
  163. default_locale = to_locale(settings.LANGUAGE_CODE)
  164. app_configs = apps.get_app_configs()
  165. allowable_packages = set(app_config.name for app_config in app_configs)
  166. allowable_packages.add('django.conf')
  167. packages = [p for p in packages if p in allowable_packages]
  168. t = {}
  169. paths = []
  170. en_selected = locale.startswith('en')
  171. en_catalog_missing = True
  172. # paths of requested packages
  173. for package in packages:
  174. p = importlib.import_module(package)
  175. path = os.path.join(os.path.dirname(upath(p.__file__)), 'locale')
  176. paths.append(path)
  177. # add the filesystem paths listed in the LOCALE_PATHS setting
  178. paths.extend(list(reversed(settings.LOCALE_PATHS)))
  179. # first load all english languages files for defaults
  180. for path in paths:
  181. try:
  182. catalog = gettext_module.translation(domain, path, ['en'])
  183. t.update(catalog._catalog)
  184. except IOError:
  185. pass
  186. else:
  187. # 'en' is the selected language and at least one of the packages
  188. # listed in `packages` has an 'en' catalog
  189. if en_selected:
  190. en_catalog_missing = False
  191. # next load the settings.LANGUAGE_CODE translations if it isn't english
  192. if default_locale != 'en':
  193. for path in paths:
  194. try:
  195. catalog = gettext_module.translation(domain, path, [default_locale])
  196. except IOError:
  197. catalog = None
  198. if catalog is not None:
  199. t.update(catalog._catalog)
  200. # last load the currently selected language, if it isn't identical to the default.
  201. if locale != default_locale:
  202. # If the currently selected language is English but it doesn't have a
  203. # translation catalog (presumably due to being the language translated
  204. # from) then a wrong language catalog might have been loaded in the
  205. # previous step. It needs to be discarded.
  206. if en_selected and en_catalog_missing:
  207. t = {}
  208. else:
  209. locale_t = {}
  210. for path in paths:
  211. try:
  212. catalog = gettext_module.translation(domain, path, [locale])
  213. except IOError:
  214. catalog = None
  215. if catalog is not None:
  216. locale_t.update(catalog._catalog)
  217. if locale_t:
  218. t = locale_t
  219. plural = None
  220. if '' in t:
  221. for l in t[''].split('\n'):
  222. if l.startswith('Plural-Forms:'):
  223. plural = l.split(':', 1)[1].strip()
  224. if plural is not None:
  225. # this should actually be a compiled function of a typical plural-form:
  226. # 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;
  227. plural = [el.strip() for el in plural.split(';') if el.strip().startswith('plural=')][0].split('=', 1)[1]
  228. pdict = {}
  229. maxcnts = {}
  230. catalog = {}
  231. for k, v in t.items():
  232. if k == '':
  233. continue
  234. if isinstance(k, six.string_types):
  235. catalog[k] = v
  236. elif isinstance(k, tuple):
  237. msgid = k[0]
  238. cnt = k[1]
  239. maxcnts[msgid] = max(cnt, maxcnts.get(msgid, 0))
  240. pdict.setdefault(msgid, {})[cnt] = v
  241. else:
  242. raise TypeError(k)
  243. for k, v in pdict.items():
  244. catalog[k] = [v.get(i, '') for i in range(maxcnts[msgid] + 1)]
  245. return catalog, plural
  246. def null_javascript_catalog(request, domain=None, packages=None):
  247. """
  248. Returns "identity" versions of the JavaScript i18n functions -- i.e.,
  249. versions that don't actually do anything.
  250. """
  251. return render_javascript_catalog()
  252. def javascript_catalog(request, domain='djangojs', packages=None):
  253. """
  254. Returns the selected language catalog as a javascript library.
  255. Receives the list of packages to check for translations in the
  256. packages parameter either from an infodict or as a +-delimited
  257. string from the request. Default is 'django.conf'.
  258. Additionally you can override the gettext domain for this view,
  259. but usually you don't want to do that, as JavaScript messages
  260. go to the djangojs domain. But this might be needed if you
  261. deliver your JavaScript source from Django templates.
  262. """
  263. locale = to_locale(get_language())
  264. if request.GET and 'language' in request.GET:
  265. if check_for_language(request.GET['language']):
  266. locale = to_locale(request.GET['language'])
  267. if packages is None:
  268. packages = ['django.conf']
  269. if isinstance(packages, six.string_types):
  270. packages = packages.split('+')
  271. catalog, plural = get_javascript_catalog(locale, domain, packages)
  272. return render_javascript_catalog(catalog, plural)