Skip to content

Commit

Permalink
[#2802] Load localisations on page load
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Aug 6, 2012
1 parent 43792bb commit aec8e7b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 15 deletions.
24 changes: 22 additions & 2 deletions ckan/public/base/javascript/i18n.js
@@ -1,11 +1,31 @@
this.ckan = this.ckan || {};

(function (ckan, jQuery, Jed) {
// Fake localisation function. A basic drop in for Jed.
// See: http://slexaxton.github.com/Jed/
ckan.i18n = new Jed({});
var domain = {
"": {
"domain": "ckan",
"lang": "en",
"plural_forms": "nplurals=2; plural=(n != 1);"
}
};

ckan.i18n = new Jed({
domain: 'ckan',
locale_data: {
ckan: domain
}
});

ckan.i18n.translate = jQuery.proxy(ckan.i18n.translate, ckan.i18n);

ckan.i18n.load = function (data) {
if (data && data['']) {
// Extend our default domain data with the new keys.
jQuery.extend(domain, data);;
}
};

ckan.sandbox.extend({
/* An alias for ckan.i18n */
i18n: ckan.i18n,
Expand Down
44 changes: 31 additions & 13 deletions ckan/public/base/javascript/main.js
Expand Up @@ -6,20 +6,38 @@ this.ckan = this.ckan || {};
ckan.DEVELOPMENT = 'development';
ckan.TESTING = 'testing';

/* Initialises the CKAN JavaScript setting up environment variables and
* loading localisations etc. Should be called once the page is ready.
*
* Examples
*
* jQuery(function () {
* ckan.initialize();
* });
*
* Returns nothing.
*/
ckan.initialize = function () {
var body = jQuery('body');
var location = window.location;
var root = location.protocol + '//' + location.host;

function getRootFromData(key) {
return (body.data(key) || root).replace(/\/$/, '');
}

this.SITE_ROOT = getRootFromData('siteRoot');
this.LOCALE_ROOT = getRootFromData('localeRoot');
this.API_ROOT = getRootFromData('apiRoot');

this.module.initialize();
jQuery.when.apply(jQuery, ckan.queue).done(function () {
var body = jQuery('body');
var locale = jQuery('html').attr('lang');
var location = window.location;
var root = location.protocol + '//' + location.host;

function getRootFromData(key) {
return (body.data(key) || root).replace(/\/$/, '');
}

ckan.SITE_ROOT = getRootFromData('siteRoot');
ckan.LOCALE_ROOT = getRootFromData('localeRoot');
ckan.API_ROOT = getRootFromData('apiRoot');

// Load the localisations before instantiating the modules.
ckan.sandbox().client.getLocaleData(locale).done(function (data) {
ckan.i18n.load(data);
ckan.module.initialize();
});
});
};

/* Returns a full url for the current site with the provided path appended.
Expand Down
39 changes: 39 additions & 0 deletions ckan/public/base/test/spec/ckan.spec.js
@@ -1,3 +1,42 @@
describe('ckan.initialize()', function () {
beforeEach(function () {
this.promise = jQuery.Deferred();
this.target = sinon.stub(ckan.Client.prototype, 'getLocaleData').returns(this.promise);
});

afterEach(function () {
this.target.restore();
});

it('should load the localisations for the current page', function () {
ckan.initialize()
assert.called(this.target);
});

it('should load the localisations into the i18n library', function () {
var target = sinon.stub(ckan.i18n, 'load');
var data = {lang: {}};

ckan.initialize();
this.promise.resolve(data);

assert.called(target);
assert.calledWith(target, data);

target.restore();
});

it('should initialize the module on the page', function () {
var target = sinon.stub(ckan.module, 'initialize');

ckan.initialize();
this.promise.resolve();

assert.called(target);
target.restore();
});
});

describe('ckan.url()', function () {
beforeEach(function () {
ckan.SITE_ROOT = 'http://example.com';
Expand Down

0 comments on commit aec8e7b

Please sign in to comment.