Skip to content

Commit

Permalink
[ckan#3151] Update JS modules to new i18n functions.
Browse files Browse the repository at this point in the history
* Usages of `this.i18n` are replaced by `this._` and `this.ngettext`.

* Module initialization functions only take a single parameter (jQuery).

* The `confirm-action` module allows to pass the content via a HTML
  `data-module-`-parameter.  That parameter has been renamed from
  `data-module-i18n-content` to `data-module-content` to reflect the
  deprecation of the `this.i18n`-functionality. The old name still works
  but the templates have been updated to use the new name instead.
  • Loading branch information
torfsen committed Nov 18, 2016
1 parent 254f770 commit 9e81917
Show file tree
Hide file tree
Showing 34 changed files with 157 additions and 199 deletions.
9 changes: 3 additions & 6 deletions ckan/public/base/javascript/modules/activity-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
* - id: what's the id of the context?
* - offset: what's the current offset?
*/
this.ckan.module('activity-stream', function($, _) {
this.ckan.module('activity-stream', function($) {
return {
/* options object can be extended using data-module-* attributes */
options : {
more: null,
id: null,
context: null,
offset: null,
loading: false,
i18n: {
loading: _('Loading...')
}
loading: false
},

/* Initialises the module setting up elements and event listeners.
Expand Down Expand Up @@ -97,7 +94,7 @@ this.ckan.module('activity-stream', function($, _) {
var options = this.options;
if (!options.loading) {
options.loading = true;
$('.load-more a', this.el).html(this.i18n('loading')).addClass('disabled');
$('.load-more a', this.el).html(this._('Loading...')).addClass('disabled');
this.sandbox.client.call('GET', options.context+'_activity_list_html', '?id='+options.id+'&offset='+options.offset, this._onActivitiesLoaded);
}
},
Expand Down
12 changes: 4 additions & 8 deletions ckan/public/base/javascript/modules/api-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@
* <a data-module="api-info" data-module-template="http://example.com/path/to/template">API</a>
*
*/
this.ckan.module('api-info', function (jQuery, _) {
this.ckan.module('api-info', function (jQuery) {
return {

/* holds the loaded lightbox */
modal: null,

options: {
template: null,
i18n: {
noTemplate: _('There is no API data to load for this resource'),
loadError: _('Failed to load data API information')
}
template: null
},

/* Sets up the API info module.
Expand Down Expand Up @@ -97,7 +93,7 @@ this.ckan.module('api-info', function (jQuery, _) {
*/
loadTemplate: function () {
if (!this.options.template) {
this.sandbox.notify(this.i18n('noTemplate'));
this.sandbox.notify(this._('There is no API data to load for this resource'));
return jQuery.Deferred().reject().promise();
}

Expand Down Expand Up @@ -125,7 +121,7 @@ this.ckan.module('api-info', function (jQuery, _) {
/* error handler when the template fails to load */
_onTemplateError: function () {
this.loading(false);
this.sandbox.notify(this.i18n('loadError'));
this.sandbox.notify(this._('Failed to load data API information'));
}
};
});
20 changes: 8 additions & 12 deletions ckan/public/base/javascript/modules/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* // <input name="tags" data-module="autocomplete" data-module-source="http://" />
*
*/
this.ckan.module('autocomplete', function (jQuery, _) {
this.ckan.module('autocomplete', function (jQuery) {
return {
/* Options for the module */
options: {
Expand All @@ -26,15 +26,7 @@ this.ckan.module('autocomplete', function (jQuery, _) {
source: null,
interval: 1000,
dropdownClass: '',
containerClass: '',
i18n: {
noMatches: _('No matches found'),
emptySearch: _('Start typing…'),
inputTooShort: function (data) {
return _('Input is too short, must be at least one character')
.ifPlural(data.min, 'Input is too short, must be at least %(min)d characters');
}
}
containerClass: ''
},

/* Sets up the module, binding methods, creating elements etc. Called
Expand Down Expand Up @@ -200,7 +192,7 @@ this.ckan.module('autocomplete', function (jQuery, _) {
* Returns a text string.
*/
formatNoMatches: function (term) {
return !term ? this.i18n('emptySearch') : this.i18n('noMatches');
return !term ? this._('Start typing…') : this._('No matches found');
},

/* Formatter used by the select2 plugin that returns a string when the
Expand All @@ -209,7 +201,11 @@ this.ckan.module('autocomplete', function (jQuery, _) {
* Returns a string.
*/
formatInputTooShort: function (term, min) {
return this.i18n('inputTooShort', {min: min});
return this.ngettext(
'Input is too short, must be at least one character',
'Input is too short, must be at least %(num)d characters',
min
);
},

/* Takes a string and converts it into an object used by the select2 plugin.
Expand Down
4 changes: 2 additions & 2 deletions ckan/public/base/javascript/modules/basic-form.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
this.ckan.module('basic-form', function (jQuery, _) {
this.ckan.module('basic-form', function (jQuery) {
return {
initialize: function () {
var message = _('There are unsaved modifications to this form').fetch();
var message = this._('There are unsaved modifications to this form');
this.el.incompleteFormWarning(message);
// Internet Explorer 7 fix for forms with <button type="submit">
if ($('html').hasClass('ie7')) {
Expand Down
36 changes: 26 additions & 10 deletions ckan/public/base/javascript/modules/confirm-action.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
this.ckan.module('confirm-action', function (jQuery, _) {
this.ckan.module('confirm-action', function (jQuery) {
return {
/* An object of module options */
options: {
/* Locale options can be overidden with data-module-i18n attribute */
/* Content can be overriden by setting data-module-content to a
* *translated* string inside the template, e.g.
*
* <a href="..."
* data-module="confirm-action"
* data-module-content="{{ _('Are you sure?') }}">
* {{ _('Delete') }}
* </a>
*/
content: '',

/* This is part of the old i18n system and is kept for backwards-
* compatibility for templates which set the content via the
* `i18n.content` attribute instead of via the `content` attribute
* as described above.
*/
i18n: {
heading: _('Please Confirm Action'),
content: _('Are you sure you want to perform this action?'),
confirm: _('Confirm'),
cancel: _('Cancel')
content: '',
},

template: [
'<div class="modal">',
'<div class="modal-header">',
Expand Down Expand Up @@ -81,10 +94,13 @@ this.ckan.module('confirm-action', function (jQuery, _) {
element.on('click', '.btn-cancel', this._onConfirmCancel);
element.modal({show: false});

element.find('h3').text(this.i18n('heading'));
element.find('.modal-body').text(this.i18n('content'));
element.find('.btn-primary').text(this.i18n('confirm'));
element.find('.btn-cancel').text(this.i18n('cancel'));
element.find('h3').text(this._('Please Confirm Action'));
var content = this.options.content ||
this.options.i18n.content || /* Backwards-compatibility */
this._('Are you sure you want to perform this action?');
element.find('.modal-body').text(content);
element.find('.btn-primary').text(this._('Confirm'));
element.find('.btn-cancel').text(this._('Cancel'));
}
return this.modal;
},
Expand Down
2 changes: 1 addition & 1 deletion ckan/public/base/javascript/modules/custom-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* See the snippets/custom_form_fields.html for an example.
*/
this.ckan.module('custom-fields', function (jQuery, _) {
this.ckan.module('custom-fields', function (jQuery) {
return {
options: {
/* The selector used for each custom field wrapper */
Expand Down
2 changes: 1 addition & 1 deletion ckan/public/base/javascript/modules/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* <div data-module="dashboard"></div>
*
*/
this.ckan.module('dashboard', function ($, _) {
this.ckan.module('dashboard', function ($) {
return {
button: null,
popover: null,
Expand Down
2 changes: 1 addition & 1 deletion ckan/public/base/javascript/modules/dataset-visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* When no organization is selected in the org dropdown then set visibility to
* public always and disable dropdown
*/
this.ckan.module('dataset-visibility', function ($, _) {
this.ckan.module('dataset-visibility', function ($) {
return {
currentValue: false,
options: {
Expand Down
12 changes: 4 additions & 8 deletions ckan/public/base/javascript/modules/follow.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@
* <a data-module="follow" data-module-action="follow" data-module-type="user" data-module-id="{user_id}">Follow User</a>
*
*/
this.ckan.module('follow', function($, _) {
this.ckan.module('follow', function($) {
return {
/* options object can be extended using data-module-* attributes */
options : {
action: null,
type: null,
id: null,
loading: false,
i18n: {
follow: _('Follow'),
unfollow: _('Unfollow')
}
loading: false
},

/* Initialises the module setting up elements and event listeners.
Expand Down Expand Up @@ -70,10 +66,10 @@ this.ckan.module('follow', function($, _) {
this.el.removeClass('disabled');
if (options.action == 'follow') {
options.action = 'unfollow';
this.el.html('<i class="icon-remove-sign"></i> ' + this.i18n('unfollow')).removeClass('btn-success').addClass('btn-danger');
this.el.html('<i class="icon-remove-sign"></i> ' + this._('Unfollow')).removeClass('btn-success').addClass('btn-danger');
} else {
options.action = 'follow';
this.el.html('<i class="icon-plus-sign"></i> ' + this.i18n('follow')).removeClass('btn-danger').addClass('btn-success');
this.el.html('<i class="icon-plus-sign"></i> ' + this._('Follow')).removeClass('btn-danger').addClass('btn-success');
}
sandbox.publish('follow-' + options.action + '-' + options.id);
}
Expand Down
42 changes: 19 additions & 23 deletions ckan/public/base/javascript/modules/image-upload.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Image Upload
*
*/
this.ckan.module('image-upload', function($, _) {
this.ckan.module('image-upload', function($) {
return {
/* options object can be extended using data-module-* attributes */
options: {
Expand All @@ -11,17 +11,7 @@ this.ckan.module('image-upload', function($, _) {
field_url: 'image_url',
field_clear: 'clear_upload',
field_name: 'name',
upload_label: '',
i18n: {
upload: _('Upload'),
url: _('Link'),
remove: _('Remove'),
upload_label: _('Image'),
label_for_url: _('URL'),
label_for_upload: _('File'),
upload_tooltip: _('Upload a file on your computer'),
url_tooltip: _('Link to a URL on the internet (you can also link to an API)')
}
upload_label: ''
},

/* Should be changed to true if user modifies resource's name
Expand Down Expand Up @@ -65,30 +55,36 @@ this.ckan.module('image-upload', function($, _) {
.appendTo(this.el);

// Button to set the field to be a URL
this.button_url = $('<a href="javascript:;" class="btn"><i class="icon-globe"></i>'+this.i18n('url')+'</a>')
.prop('title', this.i18n('url_tooltip'))
this.button_url = $('<a href="javascript:;" class="btn">' +
'<i class="icon-globe"></i>' +
this._('Link') + '</a>')
.prop('title', this._('Link to a URL on the internet (you can also link to an API)'))
.on('click', this._onFromWeb)
.insertAfter(this.input);

// Button to attach local file to the form
this.button_upload = $('<a href="javascript:;" class="btn"><i class="icon-cloud-upload"></i>'+this.i18n('upload')+'</a>')
this.button_upload = $('<a href="javascript:;" class="btn">' +
'<i class="icon-cloud-upload"></i>' +
this._('Upload') + '</a>')
.insertAfter(this.input);

// Button for resetting the form when there is a URL set
$('<a href="javascript:;" class="btn btn-danger btn-remove-url">'+this.i18n('remove')+'</a>')
.prop('title', this.i18n('remove'))
var removeText = this._('Remove');
$('<a href="javascript:;" class="btn btn-danger btn-remove-url">'
+ removeText + '</a>')
.prop('title', removeText)
.on('click', this._onRemove)
.insertBefore(this.field_url_input);

// Update the main label (this is displayed when no data/image has been uploaded/linked)
$('label[for="field-image-upload"]').text(options.upload_label || this.i18n('upload_label'));
$('label[for="field-image-upload"]').text(options.upload_label || this._('Image'));

// Setup the file input
this.input
.on('mouseover', this._onInputMouseOver)
.on('mouseout', this._onInputMouseOut)
.on('change', this._onInputChange)
.prop('title', this.i18n('upload_tooltip'))
.prop('title', this._('Upload a file on your computer'))
.css('width', this.button_upload.outerWidth());

// Fields storage. Used in this.changeState
Expand All @@ -111,7 +107,7 @@ this.ckan.module('image-upload', function($, _) {
if (options.is_url) {
this._showOnlyFieldUrl();

this._updateUrlLabel(this.i18n('label_for_url'));
this._updateUrlLabel(this._('URL'));
} else if (options.is_upload) {
this._showOnlyFieldUrl();

Expand All @@ -120,7 +116,7 @@ this.ckan.module('image-upload', function($, _) {
var filename = this._fileNameFromUpload(this.field_url_input.val());
this.field_url_input.val(filename);

this._updateUrlLabel(this.i18n('label_for_upload'));
this._updateUrlLabel(this._('File'));
} else {
this._showOnlyButtons();
}
Expand Down Expand Up @@ -174,7 +170,7 @@ this.ckan.module('image-upload', function($, _) {
this.field_clear.val('true');
}

this._updateUrlLabel(this.i18n('label_for_url'));
this._updateUrlLabel(this._('URL'));
},

/* Event listener for resetting the field back to the blank state
Expand Down Expand Up @@ -205,7 +201,7 @@ this.ckan.module('image-upload', function($, _) {

this._autoName(file_name);

this._updateUrlLabel(this.i18n('label_for_upload'));
this._updateUrlLabel(this._('File'));
},

/* Show only the buttons, hiding all others
Expand Down
4 changes: 2 additions & 2 deletions ckan/public/base/javascript/modules/media-grid.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* Media Grid
* Super simple plugin that waits for all the images to be loaded in the media
* grid and then applies the jQuery.masonry to then
*/
this.ckan.module('media-grid', function ($, _) {
*/
this.ckan.module('media-grid', function ($) {
return {
initialize: function () {
var wrapper = this.el;
Expand Down
9 changes: 3 additions & 6 deletions ckan/public/base/javascript/modules/popover-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ window.popover_context = {
}
};

this.ckan.module('popover-context', function($, _) {
this.ckan.module('popover-context', function($) {
return {

/* options object can be extended using data-module-* attributes */
Expand All @@ -40,10 +40,7 @@ this.ckan.module('popover-context', function($, _) {
loading: false,
error: false,
authed: false,
throbber: '<img src="{SITE_ROOT}/base/images/loading-spinner.gif">',
i18n: {
loading: _('Loading...')
}
throbber: '<img src="{SITE_ROOT}/base/images/loading-spinner.gif">'
},

/* Initialises the module setting up elements and event listeners.
Expand All @@ -62,7 +59,7 @@ this.ckan.module('popover-context', function($, _) {
this.el.popover({
animation: false,
html: true,
content: this.options.throbber.replace('{SITE_ROOT}', ckan.SITE_ROOT) + this.i18n('loading'),
content: this.options.throbber.replace('{SITE_ROOT}', ckan.SITE_ROOT) + this._('Loading...'),
placement: 'bottom'
});
this.el.on('mouseover', this._onMouseOver);
Expand Down
2 changes: 1 addition & 1 deletion ckan/public/base/javascript/modules/resource-form.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Module for the resource form. Handles validation and updating the form
* with external data such as from a file upload.
*/
this.ckan.module('resource-form', function (jQuery, _) {
this.ckan.module('resource-form', function (jQuery) {
return {
/* Called by the ckan core if a corresponding element is found on the page.
* Handles setting up event listeners, adding elements to the page etc.
Expand Down
Loading

0 comments on commit 9e81917

Please sign in to comment.