Bladeren bron

Refactor js translate

Denis K 8 jaren geleden
bovenliggende
commit
8ca41e6f26

+ 2 - 1
jet/static/jet/js/src/features/changeform.js

@@ -1,4 +1,5 @@
 var $ = require('jquery');
+var t = require('../utils/translate');
 
 var ChangeForm = function($changeForm) {
     this.$changeForm = $changeForm;
@@ -7,7 +8,7 @@ var ChangeForm = function($changeForm) {
 ChangeForm.prototype = {
     changeDetected: false,
     onWindowBeforeUnload: function() {
-        return django.gettext('Warning: you have unsaved changes');
+        return t('Warning: you have unsaved changes');
     },
     onFormInputChanged: function($inputs) {
         $inputs.off('change', this.onFormInputChanged);

+ 5 - 13
jet/static/jet/js/src/features/dashboard.js

@@ -1,6 +1,7 @@
 require('./../utils/jquery-slidefade');
 
 var $ = require('jquery');
+var t = require('../utils/translate');
 
 require('jquery-ui/ui/core');
 require('jquery-ui/ui/widget');
@@ -17,15 +18,7 @@ var Dashboard = function($dashboard) {
 };
 
 Dashboard.prototype = {
-    t: function(s) {
-        if (window.django == undefined) {
-            return s;
-        }
-        return django.gettext(s);
-    },
     initTools: function($dashboard) {
-        var self = this;
-
         $dashboard.find('.dashboard-tools-toggle').on('click', function (e) {
             e.preventDefault();
             $dashboard.find('.dashboard-tools').toggleClass('visible');
@@ -78,12 +71,12 @@ Dashboard.prototype = {
                 });
             };
 
-            buttons[self.t('Yes')] = function() {
+            buttons[t('Yes')] = function() {
                 resetDashboard();
                 $(this).dialog('close');
             };
 
-            buttons[self.t('Cancel')] = function() {
+            buttons[t('Cancel')] = function() {
                 $(this).dialog('close');
             };
 
@@ -182,7 +175,6 @@ Dashboard.prototype = {
         });
     },
     initDeletableModules: function($dashboard) {
-        var self = this;
         var $form = $dashboard.find('#remove-dashboard-module-form');
 
         $dashboard.find('.dashboard-item.deletable').each(function () {
@@ -208,12 +200,12 @@ Dashboard.prototype = {
                     });
                 };
 
-                buttons[self.t('Delete')] = function () {
+                buttons[t('Delete')] = function () {
                     deleteModule();
                     $(this).dialog('close');
                 };
 
-                buttons[self.t('Cancel')] = function () {
+                buttons[t('Cancel')] = function () {
                     $(this).dialog('close');
                 };
 

+ 3 - 2
jet/static/jet/js/src/features/selects.js

@@ -1,6 +1,7 @@
 require('select2');
 
 var $ = require('jquery');
+var t = require('../utils/translate');
 
 var Select2 = function() { };
 
@@ -105,10 +106,10 @@ Select2.prototype = {
                 buttons =
                     '<div class="select2-buttons">' +
                     '<a href="#" class="select2-buttons-button select2-buttons-button-select-all">' +
-                    django.gettext('select all') +
+                    t('select all') +
                     '</a> ' +
                     '<a href="#" class="select2-buttons-button select2-buttons-button-deselect-all">' +
-                    django.gettext('deselect all') +
+                    t('deselect all') +
                     '</a>' +
                     '</div>';
             }

+ 5 - 10
jet/static/jet/js/src/features/sidebar/bookmarks.js

@@ -1,4 +1,5 @@
 var $ = require('jquery');
+var t = require('../../utils/translate');
 
 require('jquery-ui/ui/core');
 require('jquery-ui/ui/widget');
@@ -13,12 +14,6 @@ var SideBarBookmarks = function($sidebar) {
 };
 
 SideBarBookmarks.prototype = {
-    t: function(s) {
-        if (window.django == undefined) {
-            return s;
-        }
-        return django.gettext(s);
-    },
     addBookmark: function($form, $container) {
         $.ajax({
             url: $form.attr('action'),
@@ -82,12 +77,12 @@ SideBarBookmarks.prototype = {
 
             var buttons = {};
 
-            buttons[self.t('Add')] = function() {
+            buttons[t('Add')] = function() {
                 self.addBookmark($form, $container);
                 $(this).dialog('close');
             };
 
-            buttons[self.t('Cancel')] = function() {
+            buttons[t('Cancel')] = function() {
                 $(this).dialog('close');
             };
 
@@ -115,12 +110,12 @@ SideBarBookmarks.prototype = {
 
             var buttons = {};
 
-            buttons[self.t('Delete')] = function() {
+            buttons[t('Delete')] = function() {
                 self.deleteBookmark($form, $item);
                 $(this).dialog('close');
             };
 
-            buttons[self.t('Cancel')] = function() {
+            buttons[t('Cancel')] = function() {
                 $(this).dialog('close');
             };
 

+ 3 - 9
jet/static/jet/js/src/layout-updaters/changeform-tabs.js

@@ -1,24 +1,18 @@
 var $ = require('jquery');
+var t = require('../utils/translate');
 
 var ChangeFormTabsUpdater = function($changeform) {
     this.$changeform = $changeform;
 };
 
 ChangeFormTabsUpdater.prototype = {
-    t: function(s) {
-        if (window.django == undefined) {
-            return s;
-        }
-        return django.gettext(s);
-    },
     findTabs: function($modules, $inlines) {
-        var self = this;
         var tabs = [];
 
         $modules.each(function(i) {
             var $module = $(this);
             var $header = $module.find('> h2').first();
-            var title = $header.length != 0 ? $header.html() : self.t('General');
+            var title = $header.length != 0 ? $header.html() : t('General');
             var className = 'module_' + i;
 
             $module.addClass(className);
@@ -33,7 +27,7 @@ ChangeFormTabsUpdater.prototype = {
         $inlines.each(function(i) {
             var $inline = $(this);
             var $header = $inline.find('> h2, > fieldset.module > h2, .tabular.inline-related > .module > h2').first();
-            var title = $header.length != 0 ? $header.html() : self.t('Tab');
+            var title = $header.length != 0 ? $header.html() : t('Tab');
             var className = 'inline_' + i;
 
             $inline.addClass(className);

+ 6 - 0
jet/static/jet/js/src/utils/translate.js

@@ -0,0 +1,6 @@
+module.exports = function(str) {
+    if (window.django == undefined) {
+        return str;
+    }
+    return django.gettext(str);
+};