Browse Source

Add js translation files loading

Denis K 8 years ago
parent
commit
cf13f6b81e
4 changed files with 68 additions and 2 deletions
  1. 12 1
      gulpfile.js
  2. 6 0
      jet/templates/admin/base.html
  3. 28 1
      jet/templatetags/jet_tags.py
  4. 22 0
      jet/utils.py

+ 12 - 1
gulpfile.js

@@ -65,6 +65,17 @@ gulp.task('vendor-css', function() {
         .pipe(gulp.dest('./jet/static/jet/css'));
 });
 
+gulp.task('vendor-i18n', function() {
+    gulp.src(['./node_modules/jquery-ui/ui/i18n/*.js'])
+        .pipe(gulp.dest('./jet/static/jet/js/i18n/jquery-ui/'));
+
+    gulp.src(['./node_modules/timepicker/i18n/*.js'])
+        .pipe(gulp.dest('./jet/static/jet/js/i18n/jquery-ui-timepicker/'));
+
+    gulp.src(['./node_modules/select2/dist/js/i18n/*.js'])
+        .pipe(gulp.dest('./jet/static/jet/js/i18n/select2/'));
+});
+
 gulp.task('scss', function() {
     gulp.src('./jet/static/jet/css/**/*.scss')
         .pipe(sourcemaps.init())
@@ -85,4 +96,4 @@ gulp.task('watch', function() {
     gulp.watch('./jet/static/jet/css/**/*.scss', ['scss']);
 });
 
-gulp.task('default', ['js', 'scss', 'vendor-css', 'watch']);
+gulp.task('default', ['js', 'scss', 'vendor-css', 'vendor-i18n', 'watch']);

+ 6 - 0
jet/templates/admin/base.html

@@ -19,6 +19,12 @@
         var DATETIME_FORMAT = "{% get_datetime_format %}";
     </script>
     <script src="{% static "jet/js/build/bundle.min.js" %}"></script>
+
+    {% jet_static_translation_urls as translation_urls %}
+    {% for url in translation_urls %}
+        <script src="{% static url %}"></script>
+    {% endfor %}
+
     <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
 {% endblock %}
 

+ 28 - 1
jet/templatetags/jet_tags.py

@@ -1,5 +1,6 @@
 from __future__ import unicode_literals
 import json
+import os
 import django
 from django import template
 from django.core.urlresolvers import reverse
@@ -12,7 +13,8 @@ from django.utils.safestring import mark_safe
 from jet import settings, VERSION
 from jet.models import Bookmark, PinnedApplication
 import re
-from jet.utils import get_app_list, get_model_instance_label, get_model_queryset
+from jet.utils import get_app_list, get_model_instance_label, get_model_queryset, get_possible_language_codes
+
 try:
     from urllib.parse import parse_qsl
 except ImportError:
@@ -386,3 +388,28 @@ def jet_delete_confirmation_context(context):
     if context.get('deletable_objects') is None and context.get('deleted_objects') is None:
         return ''
     return mark_safe('<div class="delete-confirmation-marker"></div>')
+
+
+@register.assignment_tag
+def jet_static_translation_urls():
+    language_codes = get_possible_language_codes()
+
+    urls = []
+    url_templates = [
+        'jet/js/i18n/jquery-ui/datepicker-__LANGUAGE_CODE__.js',
+        'jet/js/i18n/jquery-ui-timepicker/jquery.ui.timepicker-__LANGUAGE_CODE__.js',
+        'jet/js/i18n/select2/__LANGUAGE_CODE__.js'
+    ]
+
+    static_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'static')
+
+    for tpl in url_templates:
+        for language_code in language_codes:
+            url = tpl.replace('__LANGUAGE_CODE__', language_code)
+            path = os.path.join(static_dir, url)
+
+            if os.path.exists(path):
+                urls.append(url)
+                break
+
+    return urls

+ 22 - 0
jet/utils.py

@@ -1,5 +1,7 @@
 import datetime
 import json
+from django.utils import translation
+
 try:
     from django.apps.registry import apps
 except ImportError:
@@ -204,3 +206,23 @@ def get_model_queryset(model, preserved_filters=None):
         pass
 
     return queryset
+
+
+def get_possible_language_codes():
+    language_code = translation.get_language()
+
+    language_code = language_code.replace('_', '-').lower()
+    language_codes = []
+
+    # making dialect part uppercase
+    split = language_code.split('-', 2)
+    if len(split) == 2:
+        language_code = '%s-%s' % (split[0].lower(), split[1].upper()) if split[0] != split[1] else split[0]
+
+    language_codes.append(language_code)
+
+    # adding language code without dialect part
+    if len(split) == 2:
+        language_codes.append(split[0].lower())
+
+    return language_codes