Skip to content

Commit

Permalink
[#2375] Merge translations branch into development
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Aug 6, 2012
2 parents 70d4037 + f443d96 commit 3b2427e
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 61 deletions.
12 changes: 6 additions & 6 deletions ckan/public/base/javascript/module.js
Expand Up @@ -124,17 +124,17 @@ this.ckan = this.ckan || {};
* options: {
* saved: _('Saved!'), // A translation object.
* loading: 'Loading', // A plain string (not a good idea).
* itemCount: function (items) {
* itemCount: function (data) {
* // A function can be used to provide more complex translations
* // where the arguments may affect the outcome.
* return _('There is one item').isPlural(items, 'There are %d items')
* return _('There is one item').isPlural(data.items, 'There are %(items)d items')
* }
* },
* example: function () {
* this.i18n('saved'); // 'Saved!'
* this.i18n('loading'); // 'Loading'
* this.i18n('itemCount', 1); // 'There is one item'
* this.i18n('itemCount', 3); // 'There are 3 items'
* this.i18n('saved'); // 'Saved!'
* this.i18n('loading'); // 'Loading'
* this.i18n('itemCount', {items: 1}); // 'There is one item'
* this.i18n('itemCount', {items: 3}); // 'There are 3 items'
* }
*
* Returns the translated string or the key if not found.
Expand Down
6 changes: 3 additions & 3 deletions ckan/public/base/javascript/modules/autocomplete.js
Expand Up @@ -24,9 +24,9 @@ this.ckan.module('autocomplete', function (jQuery, _) {
i18n: {
noMatches: _('No matches found'),
emptySearch: _('Start typing…'),
inputTooShort: function (n) {
inputTooShort: function (data) {
return _('Input is too short, must be at least one character')
.ifPlural(n, 'Input is too short, must be at least %d characters');
.ifPlural(data.min, 'Input is too short, must be at least %(min)d characters');
}
}
},
Expand Down Expand Up @@ -181,7 +181,7 @@ this.ckan.module('autocomplete', function (jQuery, _) {
* Returns a string.
*/
formatInputTooShort: function (term, min) {
return this.i18n('inputTooShort', min);
return this.i18n('inputTooShort', {min: min});
},

/* Takes a string and converts it into an object used by the select2 plugin.
Expand Down
20 changes: 14 additions & 6 deletions ckan/public/base/javascript/modules/resource-upload-field.js
Expand Up @@ -21,6 +21,14 @@ this.ckan.module('resource-upload-field', function (jQuery, _, i18n) {
file: 'file',
params: []
},
i18n: {
label: _('Upload a file'),
errorTitle: _('An Error Occurred'),
uploadSuccess: _('Resource uploaded'),
uploadError: _('Unable to upload file'),
authError: _('Unable to authenticate upload'),
metadataError: _('Unable to get data for uploaded file')
},
template: [
'<span class="resource-upload-field">',
'<i class="ckan-icon ckan-icon-link-plugin"></i>',
Expand Down Expand Up @@ -52,7 +60,7 @@ this.ckan.module('resource-upload-field', function (jQuery, _, i18n) {
setupFileUpload: function () {
var options = this.options;

this.upload.find('label').text(_('Upload a file').fetch());
this.upload.find('label').text(this.i18n('label'));
this.upload.find('input[type=file]').fileupload({
type: options.form.method,
paramName: options.form.file,
Expand Down Expand Up @@ -139,7 +147,7 @@ this.ckan.module('resource-upload-field', function (jQuery, _, i18n) {
* Returns nothing.
*/
notify: function (message, type) {
var title = _('An Error Occurred').fetch();
var title = this.i18n('errorTitle');
this.sandbox.notify(title, message, type);
},

Expand Down Expand Up @@ -183,7 +191,7 @@ this.ckan.module('resource-upload-field', function (jQuery, _, i18n) {
* a file.
*/
_onUploadFail: function () {
this.sandbox.notify(_('Unable to upload file').fetch());
this.sandbox.notify(this.i18n('uploadError'));
},

/* Callback called when jQuery file upload plugin sends a file */
Expand Down Expand Up @@ -233,21 +241,21 @@ this.ckan.module('resource-upload-field', function (jQuery, _, i18n) {

/* Called when the request for auth credentials fails. */
_onAuthError: function (event, data) {
this.sandbox.notify(_('Unable to authenticate upload').fetch());
this.sandbox.notify(this.i18n('authError'));
this._onUploadComplete();
},

/* Called when the request for file metadata succeeds */
_onMetadataSuccess: function (data, response) {
var resource = this.sandbox.client.convertStorageMetadataToResource(response);

this.sandbox.notify(_('Resource uploaded').fetch(), '', 'success');
this.sandbox.notify(this.i18n('uploadSuccess'), '', 'success');
this.sandbox.publish('resource:uploaded', resource);
},

/* Called when the request for file metadata fails */
_onMetadataError: function () {
this.sandbox.notify(_('Unable to get data for uploaded file').fetch());
this.sandbox.notify(this.i18n('metadataError'));
this._onUploadComplete();
}
};
Expand Down
84 changes: 44 additions & 40 deletions ckan/public/base/javascript/modules/slug-preview.js
Expand Up @@ -23,52 +23,56 @@ this.ckan.module('slug-preview-target', {
}
});

this.ckan.module('slug-preview-slug', {
this.ckan.module('slug-preview-slug', function (jQuery, _) {
return {
options: {
prefix: '',
placeholder: '<slug>',
i18n: {
edit: _('Edit')
}
},

options: {
prefix: '',
placeholder: '<slug>'
},
initialize: function () {
var sandbox = this.sandbox;
var options = this.options;
var el = this.el;
var _ = sandbox.translate;

initialize: function () {
var sandbox = this.sandbox;
var options = this.options;
var el = this.el;
var _ = sandbox.translate;
var slug = el.slug();
var parent = slug.parents('.control-group');
var preview;

var slug = el.slug();
var parent = slug.parents('.control-group');
var preview;
if (!(parent.length)) {
return;
}

if (!(parent.length)) {
return;
}
// Leave the slug field visible
if (!parent.hasClass('error')) {
preview = parent.slugPreview({
prefix: options.prefix,
placeholder: options.placeholder,
i18n: {
'Edit': this.i18n('edit')
}
});

// Leave the slug field visible
if (!parent.hasClass('error')) {
preview = parent.slugPreview({
prefix: options.prefix,
placeholder: options.placeholder,
i18n: {
'Edit': _('Edit').fetch()
}
});
// If the user manually enters text into the input we cancel the slug
// listeners so that we don't clobber the slug when the title next changes.
slug.keypress(function () {
if (event.charCode) {
sandbox.publish('slug-preview-modified', preview[0]);
}
});

// If the user manually enters text into the input we cancel the slug
// listeners so that we don't clobber the slug when the title next changes.
slug.keypress(function () {
if (event.charCode) {
sandbox.publish('slug-preview-modified', preview[0]);
}
});
sandbox.publish('slug-preview-created', preview[0]);
}

sandbox.publish('slug-preview-created', preview[0]);
// Watch for updates to the target field and update the hidden slug field
// triggering the "change" event manually.
sandbox.subscribe('slug-target-changed', function (value) {
slug.val(value).trigger('change');
});
}

// Watch for updates to the target field and update the hidden slug field
// triggering the "change" event manually.
sandbox.subscribe('slug-target-changed', function (value) {
slug.val(value).trigger('change');
});
}
};
});
39 changes: 39 additions & 0 deletions ckan/public/base/test/spec/modules/autocomplete.spec.js
Expand Up @@ -59,6 +59,8 @@ describe('ckan.modules.AutocompleteModule()', function () {
assert.calledWith(this.select2, {
width: 'resolve',
query: this.module._onQuery,
dropdownCssClass: '',
containerCssClass: '',
formatResult: this.module.formatResult,
formatNoMatches: this.module.formatNoMatches,
formatInputTooShort: this.module.formatInputTooShort,
Expand All @@ -75,13 +77,50 @@ describe('ckan.modules.AutocompleteModule()', function () {
assert.calledWith(this.select2, {
width: 'resolve',
tags: this.module._onQuery,
dropdownCssClass: '',
containerCssClass: '',
formatResult: this.module.formatResult,
formatNoMatches: this.module.formatNoMatches,
formatInputTooShort: this.module.formatInputTooShort,
initSelection: this.module.formatInitialValue
});

it('should watch the keydown event on the select2 input');

it('should allow a custom css class to be added to the dropdown', function () {
this.module.options.dropdownClass = 'tags';
this.module.setupAutoComplete();

assert.called(this.select2);
assert.calledWith(this.select2, {
width: 'resolve',
tags: this.module._onQuery,
dropdownCssClass: 'tags',
containerCssClass: '',
formatResult: this.module.formatResult,
formatNoMatches: this.module.formatNoMatches,
formatInputTooShort: this.module.formatInputTooShort,
initSelection: this.module.formatInitialValue
});
});

it('should allow a custom css class to be added to the container', function () {
this.module.options.containerClass = 'tags';
this.module.setupAutoComplete();

assert.called(this.select2);
assert.calledWith(this.select2, {
width: 'resolve',
tags: this.module._onQuery,
dropdownCssClass: '',
containerCssClass: 'tags',
formatResult: this.module.formatResult,
formatNoMatches: this.module.formatNoMatches,
formatInputTooShort: this.module.formatInputTooShort,
initSelection: this.module.formatInitialValue
});
});

});
});

Expand Down
36 changes: 30 additions & 6 deletions doc/frontend-development.rst
Expand Up @@ -242,6 +242,29 @@ factory function and the ``this.sandbox.translate()`` object.
}
});

String interpolation can be provided using the `sprintf formatting <http://www.diveintojavascript.com/projects/javascript-sprintf>`_.
We always use the named arguments to keep in line with the Python translations.
And we name the translate function passed into ``ckan.module()`` ``_``.

::

ckan.module('my-module', function (jQuery, _) {
return {
initialize: function () {
// Keyword arguments
_('Hello %(name)s').fetch({name: 'Bill'}); // Hello Bill

// Multiple.
_("I like your %(color)s %(fruit)s.").fetch({color: 'red', fruit: 'apple');

// Plurals.
_("I have %(num)d apple.")
.ifPlural(2, "I have %(num)d apples.")
.fetch({num: 2, fruit: 'apple');
}
};
});

Modules
-------

Expand Down Expand Up @@ -321,12 +344,12 @@ String interpolation can be provided by passing extra arguments.
return {
options: {
i18n: {
hello: _('Hello %s')
hello: _('Hello %(name)s')
}
},
initialize: function () {
var name = 'Dave';
this.i18n('hello', name); // "Hello Dave"
this.i18n('hello', {name: name}); // "Hello Dave"
}
}
});
Expand All @@ -340,15 +363,16 @@ Jed API.
return {
options: {
i18n: {
apples: function (n) {
return _('I have %d apple').isPlural(n, 'I have %d apples');
apples: function (params) {
var n = params.num;
return _('I have %(num)d apple').isPlural(n, 'I have %(num)d apples');
}
}
},
initialize: function () {
var total = 1;
this.i18n('apples', total); // "I have 1 apple"
this.i18n('apples', 3); // "I have 3 apples"
this.i18n('apples', {num: total}); // "I have 1 apple"
this.i18n('apples', {num: 3}); // "I have 3 apples"
}
}
});
Expand Down

0 comments on commit 3b2427e

Please sign in to comment.