Skip to content

Commit

Permalink
[#2802] Add an client method for loading the localisations
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Aug 6, 2012
1 parent c846ee2 commit 43792bb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
20 changes: 19 additions & 1 deletion ckan/public/base/javascript/client.js
Expand Up @@ -20,11 +20,29 @@
*/
url: function (path) {
if (!(/^https?:\/\//i).test(path)) {
path = this.endpoint + '/' + path.replace(/^\//, '');
path = this.endpoint + '/' + encodeURI(path).replace(/^\//, '');
}
return path;
},

/* Fetches the current locale translation from the API.
*
* locale - The current page locale.
*
* Examples
*
* var locale = jQuery('html').attr('lang');
* client.getLocaleData(locale, function (data) {
* // Load into the localizer.
* });
*
* Returns a jQuery xhr promise.
*/
getLocaleData: function (locale, success, error) {
var url = this.url('/api/i18n/' + (locale || ''));
return jQuery.getJSON(url).then(success, error);
},

/* Retrieves a list of auto-completions from one of the various endpoints
* and normalises the results into an array of tags.
*
Expand Down
23 changes: 23 additions & 0 deletions ckan/public/base/test/spec/client.spec.js
Expand Up @@ -39,6 +39,29 @@ describe('ckan.Client()', function () {
});
});

describe('.getLocaleData(locale, success, error)', function () {
beforeEach(function () {
this.fakePromise = sinon.stub(jQuery.Deferred());
this.fakePromise.then.returns(this.fakePromise);
sinon.stub(jQuery, 'getJSON').returns(this.fakePromise);
});

afterEach(function () {
jQuery.getJSON.restore();
});

it('should return a jQuery promise', function () {
var target = this.client.getLocaleData('en');
assert.ok(target === this.fakePromise, 'target === this.fakePromise');
});

it('should request the locale provided', function () {
var target = this.client.getLocaleData('en');
assert.called(jQuery.getJSON);
assert.calledWith(jQuery.getJSON, '/api/i18n/en');
});
});

describe('.getCompletions(url, options, success, error)', function () {
beforeEach(function () {
this.fakePiped = sinon.stub(jQuery.Deferred());
Expand Down

0 comments on commit 43792bb

Please sign in to comment.