Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

i18n javascript_catalog View Pollutes Global Namespace #13

Closed
wants to merge 14 commits into from

6 participants

Matthew Dapena-Tretter Florian Apolloner Alexey Boriskin Aymeric Augustin Jannis Leidel Ramiro Morales
Matthew Dapena-Tretter

This pull request corresponds to Trac Ticket number 18231

In addition to the translation functions (gettext, ngettext, etc.), the global variables catalog and formats are created. These variables are required to exist and be in the correct format in order for the translation functions to work.

Seeing as how these are common words, with a relatively high likelihood of being clobbered, I propose we use the module pattern (an immediately invoked function).

This patch also uses vanilla objects for catalog and formats as those are the preferred type for dictionaries in JavaScript.

Florian Apolloner
Owner

@matthewwithanm Does this have any backward compat issues? Eg if someone used this global scope vars?

Matthew Dapena-Tretter

Yeah, this creates the catalog and formats dictionaries in IIFEs, so they aren't global anymore.

Florian Apolloner
Owner

Hmm I guess we could provide a deprecation path for that, but who reads js warnings?!

Alexey Boriskin

What do you think about the following idea: make all the generated javascript to be a string with django template syntax? That would allow us not to worry about indentation changes.

Matthew Dapena-Tretter

I'm very in favor of that. I think it would also make the JS much more maintainable. I would've done that with the initial PR but thought it might be too drastic a change.

Florian Apolloner
Owner

That would definitely help, @jezdez any objections?

Matthew Dapena-Tretter

I'm getting ready to embark on a weeklong trip, but if there aren't any objections in the meantime, I'll knock this out afterwards, before DjangoCon. I'll also update the dict creation to use object literals instead of repeated assignment.

Aymeric Augustin
Owner

This PR is stale; since our triage options on GitHub are limited to "open" or "closed" I'm going to close it.

Please re-open if you have a chance to update it.

If no one reviews it, write to the django-developers mailing list. Thanks!

Aymeric Augustin aaugustin closed this
matthewwithanm added some commits
Matthew Dapena-Tretter matthewwithanm Use module pattern to prevent global scope pollution db2cdf1
Matthew Dapena-Tretter matthewwithanm Use object literals for dictionaries 82a13d3
Matthew Dapena-Tretter matthewwithanm Use template for javascript catalog
The same template is used for the `javascript_catalog` and
`null_javascript_catalog` views; they're simply supplied with different
context variables. In addition, the catalog dictionary is defined using
an object literal (serialized using simplejson), which eliminates a few
bytes when compared to the original method.
dfe7953
Matthew Dapena-Tretter matthewwithanm End function definitions with semicolons 592d774
Matthew Dapena-Tretter matthewwithanm Serialize formats dict with simplejson
Also, include all keys in object definition instead of using repeated
assignment.
861bd70
Matthew Dapena-Tretter matthewwithanm A little PEP8ing a540d58
Matthew Dapena-Tretter

@aaugustin I've rebased these commits onto the current master, however, I don't have permissions to re-open this issue and, unless the issue is open, I can't update the pull request. I believe if you reopen the issue I can force push to this branch and GitHub will update the PR. Alternatively, I can open a new PR if you'd rather.

Aymeric Augustin aaugustin reopened this
Aymeric Augustin
Owner

I thought you could re-open a PR simply by pushing new commits :/

Matthew Dapena-Tretter

No problem. There they be (:

Jannis Leidel
Owner

I think using the django namespace like we do in the admin makes sense for this.

Matthew Dapena-Tretter

@jezdez Sounds good. 952f32a makes that change and 95b1fe3 updates the other creation of the django namespace so as not to clobber it if it already exists. (I think that's the only place it's created?)

Note that I'm not exporting the catalog or format dictionaries to the global namespace but we could if we wanted to preserve backwards compatibility. (Since the functions are using the namespaced versions, they would still continue to work if somebody overwrote the global reference.)

Ramiro Morales ramiro commented on the diff
django/views/i18n.py
((29 lines not shown))
-var catalog = new Array();
-"""
+js_catalog_template = r"""
Ramiro Morales Collaborator
ramiro added a note

Nitpick: Append a \ to the end so it doesn't generate an empty first line.

@ramiro I'm using a raw string, so I don't think that'll work here. We could move it onto the same line as the triple quote, but IMO the added readability in the source is worth the extra empty first line. Thoughts?

Ramiro Morales Collaborator
ramiro added a note

You are completely right. What about this? Too ugly?:

js_catalog_template = \
r"""{% autoescape off %}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
matthewwithanm added some commits
Matthew Dapena-Tretter matthewwithanm Merge branch 'master' into js-i18n-patch e5ece5f
Matthew Dapena-Tretter matthewwithanm Update test
This test was expecting the catalog to be in the old form.
ad0705c
Matthew Dapena-Tretter matthewwithanm Fix bug with storage of plurals
This should also eliminate a bit of the redundancy from the original
implementation since we're constructing one array per message id,
instead of an array filled with empty string which we later fill.
9ed390c
Matthew Dapena-Tretter matthewwithanm Fix context separator
There was an extra escape character since we're using a raw string for
the template.
e2e5a72
Matthew Dapena-Tretter matthewwithanm Use json instead of simplejson 2dd49b3
Matthew Dapena-Tretter matthewwithanm Clean up indentation code d954aec
Matthew Dapena-Tretter

@ramiro Let me know if anything is missing.

Ramiro Morales
Collaborator

@matthewwithanm I've reviewed the PR and it's in good shape.Will commit RSN after some i18n problems in master are settled so I can test the changes in isolation and ask for opinions from a couple of devs experienced on i18n. Thanks for a great job.

Matthew Dapena-Tretter

No problem; thanks for reviewing!

Ramiro Morales
Collaborator

Fixed in a506b69. Thanks!

Ramiro Morales ramiro closed this
Zach Metcalf zmetcalf referenced this pull request in fusionbox/django-widgy
Closed

JS i18n - Issue #82 #140

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 1, 2013
  1. Matthew Dapena-Tretter
  2. Matthew Dapena-Tretter

    Use object literals for dictionaries

    matthewwithanm authored matthewwithanm committed
  3. Matthew Dapena-Tretter

    Use template for javascript catalog

    matthewwithanm authored
    The same template is used for the `javascript_catalog` and
    `null_javascript_catalog` views; they're simply supplied with different
    context variables. In addition, the catalog dictionary is defined using
    an object literal (serialized using simplejson), which eliminates a few
    bytes when compared to the original method.
  4. Matthew Dapena-Tretter
  5. Matthew Dapena-Tretter

    Serialize formats dict with simplejson

    matthewwithanm authored
    Also, include all keys in object definition instead of using repeated
    assignment.
  6. Matthew Dapena-Tretter

    A little PEP8ing

    matthewwithanm authored
Commits on Feb 23, 2013
  1. Matthew Dapena-Tretter
  2. Matthew Dapena-Tretter
Commits on Mar 5, 2013
  1. Matthew Dapena-Tretter
  2. Matthew Dapena-Tretter

    Update test

    matthewwithanm authored
    This test was expecting the catalog to be in the old form.
  3. Matthew Dapena-Tretter

    Fix bug with storage of plurals

    matthewwithanm authored
    This should also eliminate a bit of the redundancy from the original
    implementation since we're constructing one array per message id,
    instead of an array filled with empty string which we later fill.
  4. Matthew Dapena-Tretter

    Fix context separator

    matthewwithanm authored
    There was an extra escape character since we're using a raw string for
    the template.
  5. Matthew Dapena-Tretter
  6. Matthew Dapena-Tretter
This page is out of date. Refresh to see the latest.
5 django/contrib/admin/static/admin/js/jquery.init.js
View
@@ -3,6 +3,5 @@
* namespace (i.e. this preserves pre-existing values for both window.$ and
* window.jQuery).
*/
-var django = {
- "jQuery": jQuery.noConflict(true)
-};
+var django = django || {};
+django.jQuery = jQuery.noConflict(true);
240 django/views/i18n.py
View
@@ -1,11 +1,12 @@
+import json
import os
import gettext as gettext_module
from django import http
from django.conf import settings
+from django.template import Context, Template
from django.utils import importlib
from django.utils.translation import check_for_language, activate, to_locale, get_language
-from django.utils.text import javascript_quote
from django.utils.encoding import smart_text
from django.utils.formats import get_format_modules, get_format
from django.utils._os import upath
@@ -38,6 +39,7 @@ def set_language(request):
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)
return response
+
def get_formats():
"""
Returns all formats strings required for i18n to work
@@ -53,120 +55,142 @@ def get_formats():
for module in [settings] + get_format_modules(reverse=True):
for attr in FORMAT_SETTINGS:
result[attr] = get_format(attr)
- src = []
+ formats = {}
for k, v in result.items():
if isinstance(v, (six.string_types, int)):
- src.append("formats['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(smart_text(v))))
+ formats[k] = smart_text(v)
elif isinstance(v, (tuple, list)):
- v = [javascript_quote(smart_text(value)) for value in v]
- src.append("formats['%s'] = ['%s'];\n" % (javascript_quote(k), "', '".join(v)))
- return ''.join(src)
-
-NullSource = """
-/* gettext identity library */
-
-function gettext(msgid) { return msgid; }
-function ngettext(singular, plural, count) { return (count == 1) ? singular : plural; }
-function gettext_noop(msgid) { return msgid; }
-function pgettext(context, msgid) { return msgid; }
-function npgettext(context, singular, plural, count) { return (count == 1) ? singular : plural; }
-"""
+ formats[k] = [smart_text(value) for value in v]
+ return formats
-LibHead = """
-/* gettext library */
-var catalog = new Array();
-"""
+js_catalog_template = r"""
Ramiro Morales Collaborator
ramiro added a note

Nitpick: Append a \ to the end so it doesn't generate an empty first line.

@ramiro I'm using a raw string, so I don't think that'll work here. We could move it onto the same line as the triple quote, but IMO the added readability in the source is worth the extra empty first line. Thoughts?

Ramiro Morales Collaborator
ramiro added a note

You are completely right. What about this? Too ugly?:

js_catalog_template = \
r"""{% autoescape off %}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+{% autoescape off %}
+(function (globals) {
-LibFoot = """
-
-function gettext(msgid) {
- var value = catalog[msgid];
- if (typeof(value) == 'undefined') {
- return msgid;
- } else {
- return (typeof(value) == 'string') ? value : value[0];
- }
-}
-
-function ngettext(singular, plural, count) {
- value = catalog[singular];
- if (typeof(value) == 'undefined') {
- return (count == 1) ? singular : plural;
- } else {
- return value[pluralidx(count)];
- }
-}
-
-function gettext_noop(msgid) { return msgid; }
-
-function pgettext(context, msgid) {
- var value = gettext(context + '\\x04' + msgid);
- if (value.indexOf('\\x04') != -1) {
- value = msgid;
- }
- return value;
-}
-
-function npgettext(context, singular, plural, count) {
- var value = ngettext(context + '\\x04' + singular, context + '\\x04' + plural, count);
- if (value.indexOf('\\x04') != -1) {
- value = ngettext(singular, plural, count);
- }
- return value;
-}
-"""
+ var django = globals.django || (globals.django = {});
-LibFormatHead = """
-/* formatting library */
+ {% if plural %}
+ django.pluralidx = function (n) {
+ var v={{ plural }};
+ if (typeof(v) == 'boolean') {
+ return v ? 1 : 0;
+ } else {
+ return v;
+ }
+ };
+ {% else %}
+ django.pluralidx = function (count) { return (count == 1) ? 0 : 1; };
+ {% endif %}
-var formats = new Array();
+ {% if catalog_str %}
+ /* gettext library */
-"""
+ django.catalog = {{ catalog_str }};
-LibFormatFoot = """
-function get_format(format_type) {
- var value = formats[format_type];
+ django.gettext = function (msgid) {
+ var value = django.catalog[msgid];
+ if (typeof(value) == 'undefined') {
+ return msgid;
+ } else {
+ return (typeof(value) == 'string') ? value : value[0];
+ }
+ };
+
+ django.ngettext = function (singular, plural, count) {
+ value = django.catalog[singular];
+ if (typeof(value) == 'undefined') {
+ return (count == 1) ? singular : plural;
+ } else {
+ return value[django.pluralidx(count)];
+ }
+ };
+
+ django.gettext_noop = function (msgid) { return msgid; };
+
+ django.pgettext = function (context, msgid) {
+ var value = django.gettext(context + '\x04' + msgid);
+ if (value.indexOf('\x04') != -1) {
+ value = msgid;
+ }
+ return value;
+ };
+
+ django.npgettext = function (context, singular, plural, count) {
+ var value = django.ngettext(context + '\x04' + singular, context + '\x04' + plural, count);
+ if (value.indexOf('\x04') != -1) {
+ value = django.ngettext(singular, plural, count);
+ }
+ return value;
+ };
+ {% else %}
+ /* gettext identity library */
+
+ django.gettext = function (msgid) { return msgid; };
+ django.ngettext = function (singular, plural, count) { return (count == 1) ? singular : plural; };
+ django.gettext_noop = function (msgid) { return msgid; };
+ django.pgettext = function (context, msgid) { return msgid; };
+ django.npgettext = function (context, singular, plural, count) { return (count == 1) ? singular : plural; };
+ {% endif %}
+
+ django.interpolate = function (fmt, obj, named) {
+ if (named) {
+ return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])});
+ } else {
+ return fmt.replace(/%s/g, function(match){return String(obj.shift())});
+ }
+ };
+
+
+ /* formatting library */
+
+ django.formats = {{ formats_str }};
+
+ django.get_format = function (format_type) {
+ var value = django.formats[format_type];
if (typeof(value) == 'undefined') {
return format_type;
} else {
return value;
}
-}
-"""
+ };
-SimplePlural = """
-function pluralidx(count) { return (count == 1) ? 0 : 1; }
-"""
+ /* add to global namespace */
+ globals.pluralidx = django.pluralidx;
+ globals.gettext = django.gettext;
+ globals.ngettext = django.ngettext;
+ globals.gettext_noop = django.gettext_noop;
+ globals.pgettext = django.pgettext;
+ globals.npgettext = django.npgettext;
+ globals.interpolate = django.interpolate;
+ globals.get_format = django.get_format;
-InterPolate = r"""
-function interpolate(fmt, obj, named) {
- if (named) {
- return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])});
- } else {
- return fmt.replace(/%s/g, function(match){return String(obj.shift())});
- }
-}
+}(this));
+{% endautoescape %}
"""
-PluralIdx = r"""
-function pluralidx(n) {
- var v=%s;
- if (typeof(v) == 'boolean') {
- return v ? 1 : 0;
- } else {
- return v;
- }
-}
-"""
+
+def render_javascript_catalog(catalog=None, plural=None):
+ template = Template(js_catalog_template)
+ indent = lambda s: s.replace('\n', '\n ')
+ context = Context({
+ 'catalog_str': indent(json.dumps(
+ catalog, sort_keys=True, indent=2)) if catalog else None,
+ 'formats_str': indent(json.dumps(
+ get_formats(), sort_keys=True, indent=2)),
+ 'plural': plural,
+ })
+
+ return http.HttpResponse(template.render(context), 'text/javascript')
+
def null_javascript_catalog(request, domain=None, packages=None):
"""
Returns "identity" versions of the JavaScript i18n functions -- i.e.,
versions that don't actually do anything.
"""
- src = [NullSource, InterPolate, LibFormatHead, get_formats(), LibFormatFoot]
- return http.HttpResponse(''.join(src), 'text/javascript')
+ return render_javascript_catalog()
+
def javascript_catalog(request, domain='djangojs', packages=None):
"""
@@ -243,42 +267,32 @@ def javascript_catalog(request, domain='djangojs', packages=None):
locale_t.update(catalog._catalog)
if locale_t:
t = locale_t
- src = [LibHead]
plural = None
if '' in t:
for l in t[''].split('\n'):
if l.startswith('Plural-Forms:'):
- plural = l.split(':',1)[1].strip()
+ 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]
- src.append(PluralIdx % plural)
- else:
- src.append(SimplePlural)
- csrc = []
+ 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):
- csrc.append("catalog['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(v)))
+ catalog[k] = v
elif isinstance(k, tuple):
- if k[0] not in pdict:
- pdict[k[0]] = k[1]
- else:
- pdict[k[0]] = max(k[1], pdict[k[0]])
- csrc.append("catalog['%s'][%d] = '%s';\n" % (javascript_quote(k[0]), k[1], javascript_quote(v)))
+ msgid = k[0]
+ cnt = k[1]
+ maxcnts[msgid] = max(cnt, maxcnts.get(msgid, 0))
+ pdict.setdefault(msgid, {})[cnt] = v
else:
raise TypeError(k)
- csrc.sort()
for k, v in pdict.items():
- src.append("catalog['%s'] = [%s];\n" % (javascript_quote(k), ','.join(["''"]*(v+1))))
- src.extend(csrc)
- src.append(LibFoot)
- src.append(InterPolate)
- src.append(LibFormatHead)
- src.append(get_formats())
- src.append(LibFormatFoot)
- src = ''.join(src)
- return http.HttpResponse(src, 'text/javascript')
+ catalog[k] = [v.get(i, '') for i in range(maxcnts[msgid] + 1)]
+
+ return render_javascript_catalog(catalog, plural)
4 tests/view_tests/tests/i18n.py
View
@@ -62,12 +62,12 @@ def test_jsi18n(self):
trans_txt = catalog.ugettext('this is to be translated')
response = self.client.get('/views/jsi18n/')
# in response content must to be a line like that:
- # catalog['this is to be translated'] = 'same_that_trans_txt'
+ # "this is to be translated": "same_that_trans_txt"
# javascript_quote is used to be able to check unicode strings
self.assertContains(response, javascript_quote(trans_txt), 1)
if lang_code == 'fr':
# Message with context (msgctxt)
- self.assertContains(response, "['month name\x04May'] = 'mai';", 1)
+ self.assertContains(response, '"month name\u0004May": "mai"', 1)
class JsI18NTests(TestCase):
Something went wrong with that request. Please try again.