Skip to content

Commit

Permalink
[#2375] Moved translate function into separate file
Browse files Browse the repository at this point in the history
Also added a temporary shim for Jed.js a gettext implementation in
JavaScript
  • Loading branch information
aron committed Jul 3, 2012
1 parent 07736d1 commit c7f32b9
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 27 deletions.
5 changes: 0 additions & 5 deletions ckan/public/base/javascript/main.js
@@ -1,11 +1,6 @@
// Global ckan namespace
this.ckan = this.ckan || {};

// Fake localisation function.
this.ckan.trans = function (string) {
return string;
};

this.ckan.initialize = function () {
this.module.initialize();
};
Expand Down
22 changes: 11 additions & 11 deletions ckan/public/base/javascript/module.js
Expand Up @@ -17,17 +17,17 @@ this.ckan = this.ckan || {};
*
* // For element: <select data-module="language-picker"></select>
*
* // Function recieves the sandbox, options and trans() arguments.
* ckan.module('language-picker', function (sb, options, __) {
* // Function recieves the sandbox, options and translate() arguments.
* ckan.module('language-picker', function (sb, options, _) {
* // .el is the dom node the element was created with.
* sb.el.on('change', function () {
*
* // Publish the new language so other modules can update.
* sb.publish('lang', this.selected);
*
* // Display a localized notification to the user.
* // NOTE: __ is an alias for sb.trans()
* sb.alert(__('Language changed to: ' + this.selected));
* // NOTE: _ is an alias for sb.translate()
* sb.alert(_('Language changed to: ' + this.selected).fetch());
* });
*
* // listen for other updates to lang.
Expand All @@ -50,10 +50,10 @@ this.ckan = this.ckan || {};
*
* Examples
*
* ckan.module('like-button', function (mod, options, trans) {
* ckan.module('like-button', function (mod, options, _) {
* mod.element.on('click', function () {
* var url = options.endpoint;
* var message = trans('Dataset was liked!');
* var message = _('Dataset was liked!').fetch();
* mod.ajax( ... );
* });
* }, {
Expand Down Expand Up @@ -112,10 +112,10 @@ this.ckan = this.ckan || {};
*
* Examples
*
* module.createModule(function (sb, opts, trans) {
* this === sb.el[0]; // The div passed in as the second arg.
* opts === sb.opts; // Any data-module-* options on the div.
* trans === sb.trans; // A translation function.
* module.createModule(function (sb, opts, _) {
* this === sb.el[0]; // The div passed in as the second arg.
* opts === sb.opts; // Any data-module-* options on the div.
* _ === sb.translate; // A translation function.
* }, document.createElement('div'));
*
* Returns nothing.
Expand All @@ -125,7 +125,7 @@ this.ckan = this.ckan || {};
var options = $.extend(defaults, module.extractOptions(element));
var sandbox = ckan.sandbox(element, options);

factory.call(element, sandbox, sandbox.options, sandbox.trans);
factory.call(element, sandbox, sandbox.options, ckan.i18n.translate);
};

/* Extracts any properties starting with MODULE_OPTION_PREFIX from the
Expand Down
6 changes: 3 additions & 3 deletions ckan/public/base/javascript/modules/slug-preview.js
Expand Up @@ -17,7 +17,7 @@ this.ckan.module('slug-preview-target', function (mod) {
});
});

this.ckan.module('slug-preview-slug', function (mod, options, __) {
this.ckan.module('slug-preview-slug', function (mod, options, _) {
var slug = mod.el.slug();
var parent = slug.parents('.control-group');
var preview;
Expand All @@ -31,8 +31,8 @@ this.ckan.module('slug-preview-slug', function (mod, options, __) {
preview = parent.slugPreview({
prefix: options.prefix,
placeholder: options.placeholder,
trans: {
edit: __('Edit')
i18n: {
'Edit': _('Edit').fetch()
}
});

Expand Down
10 changes: 5 additions & 5 deletions ckan/public/base/javascript/plugins/jquery.slug-preview.js
Expand Up @@ -5,15 +5,15 @@
* options - An object of plugin options (defaults to slugPreview.defaults).
* prefix: An optional prefix to apply before the slug field.
* placeholder: Optional placeholder when there is no slug.
* trans: Provide alternative translations for the plugin string.
* i18n: Provide alternative translations for the plugin string.
* template: Provide alternative markup for the plugin.
*
* Examples
*
* var previews = jQuery('[name=slug]').slugPreview({
* prefix: 'example.com/resource/',
* placeholder: '<id>',
* trans: {edit: 'éditer'}
* i18n: {edit: 'éditer'}
* });
* // previews === preview objects.
* // previews.end() === [name=slug] objects.
Expand All @@ -38,7 +38,7 @@
}

preview.find('.slug-preview-prefix').text(options.prefix);
preview.find('button').text(options.trans.edit).click(function (event) {
preview.find('button').text(options.i18n['Edit']).click(function (event) {
event.preventDefault();
element.show();
preview.hide();
Expand All @@ -60,8 +60,8 @@
slugPreview.defaults = {
prefix: '',
placeholder: '',
trans: {
edit: 'Edit'
i18n: {
'Edit': 'Edit'
},
template: [
'<div class="slug-preview">',
Expand Down
7 changes: 5 additions & 2 deletions ckan/public/base/javascript/sandbox.js
Expand Up @@ -19,8 +19,11 @@ this.ckan = this.ckan || {};
return this.el(selector);
},

/* An alias for ckan.trans() */
trans: ckan.trans,
/* An alias for ckan.i18n */
i18n: ckan.i18n,

/* An alias for ckan.l18n.translate() */
translate: ckan.i18n.translate,

/* Publishes an event to all modules. Can be used to notify other modules
* that an area of the site has changed.
Expand Down
Expand Up @@ -26,7 +26,7 @@ describe('jQuery.fn.slugPreview()', function () {

it('should allow translations for strings to be provided', function () {
var target = this.element.slugPreview({
trans: {edit: 'translated'}
i18n: {'Edit': 'translated'}
});
assert.equal(target.find('button').text(), 'translated');
});
Expand Down
1 change: 1 addition & 0 deletions ckan/templates/snippets/scripts.html
Expand Up @@ -4,5 +4,6 @@
<script src="{% url_for_static "/base/javascript/plugins/jquery.slug-preview.js" %}"></script>
<script src="{% url_for_static '/base/javascript/sandbox.js' %}"></script>
<script src="{% url_for_static '/base/javascript/module.js' %}"></script>
<script src="{% url_for_static '/base/javascript/l18n.js' %}"></script>
<script src="{% url_for_static '/base/javascript/main.js' %}"></script>
<script src="{% url_for_static "/base/javascript/modules/slug-preview.js" %}"></script>

0 comments on commit c7f32b9

Please sign in to comment.