Repository: getsentry/sentry

Issue:
Remove superfluous localizations from vendor.js
Right now we're shipping every possible localization for moment.js in our vendor.js bundle. This currently accounts for over 800kb (after minification, before gzip).



Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -349,9 +349,9 @@ def update_manifests(self):
         # the current folder.  This will chop off the right path for the
         # manifest.
         for root in self.sentry_static_dist_path, INTEGRATION_DOC_FOLDER:
-            for dirname, dirnames, filenames in os.walk(root):
+            for dirname, _, filenames in os.walk(root):
                 for filename in filenames:
-                    filename = os.path.join(root, filename)
+                    filename = os.path.join(dirname, filename)
                     files.append(filename[len(base):].lstrip(os.path.sep))
 
         files.append('src/sentry/sentry-package.json')
diff --git a/src/sentry/conf/server.py b/src/sentry/conf/server.py
--- a/src/sentry/conf/server.py
+++ b/src/sentry/conf/server.py
@@ -173,6 +173,8 @@
 LANGUAGES = tuple((code, name) for code, name in LANGUAGES
                   if code in CATALOGS)
 
+SUPPORTED_LANGUAGES = frozenset(CATALOGS)
+
 SITE_ID = 1
 
 # If you set this to False, Django will make some optimizations so as not
diff --git a/src/sentry/templatetags/sentry_assets.py b/src/sentry/templatetags/sentry_assets.py
--- a/src/sentry/templatetags/sentry_assets.py
+++ b/src/sentry/templatetags/sentry_assets.py
@@ -35,3 +35,18 @@ def crossorigin():
         # They share the same domain prefix, so we don't need CORS
         return ''
     return ' crossorigin="anonymous"'
+
+
+@register.simple_tag(takes_context=True)
+def locale_js_include(context):
+    """
+    If the user has a non-English locale set, returns a <script> tag pointing
+    to the relevant locale JavaScript file
+    """
+    request = context['request']
+    lang_code = request.LANGUAGE_CODE
+    if lang_code == 'en' or lang_code not in settings.SUPPORTED_LANGUAGES:
+        return ''
+
+    href = get_asset_url("sentry", "dist/moment/locale/" + lang_code + ".js")
+    return "<script src=\"{0}\"{1}></script>".format(href, crossorigin())


Test patch:
diff --git a/tests/sentry/templatetags/test_sentry_assets.py b/tests/sentry/templatetags/test_sentry_assets.py
new file mode 100644
--- /dev/null
+++ b/tests/sentry/templatetags/test_sentry_assets.py
@@ -0,0 +1,41 @@
+from __future__ import absolute_import
+
+from django.template import Context, Template
+from mock import Mock
+
+from sentry.testutils import TestCase
+
+
+class FeaturesTest(TestCase):
+    TEMPLATE = Template("""
+        {% load sentry_assets %}
+        {% locale_js_include %}
+    """)
+
+    def test_supported_foreign_lang(self):
+        result = self.TEMPLATE.render(Context({
+            'request': Mock(LANGUAGE_CODE='fr'),  # French, in locale/catalogs.json
+        }))
+
+        assert '<script src="/_static/{version}/sentry/dist/moment/locale/fr.js"></script>' in result
+
+    def test_unsupported_foreign_lang(self):
+        result = self.TEMPLATE.render(Context({
+            'request': Mock(LANGUAGE_CODE='ro'),  # Romanian, not in locale/catalogs.json
+        }))
+
+        assert result.strip() == ''
+
+    def test_english(self):
+        result = self.TEMPLATE.render(Context({
+            'request': Mock(LANGUAGE_CODE='en'),
+        }))
+
+        assert result.strip() == ''
+
+    def test_no_lang(self):
+        result = self.TEMPLATE.render(Context({
+            'request': Mock(),
+        }))
+
+        assert result.strip() == ''