Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
feat(translateProvider): add useLoaderFactory() as shortcut method
Browse files Browse the repository at this point in the history
  • Loading branch information
0x-r4bbit committed May 12, 2013
1 parent 3d16593 commit 2915e8b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ngTranslate/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,21 @@ angular.module('ngTranslate').provider('$translate', ['$STORAGE_KEY', function (
$asyncLoaders.push($loader);
};

/**
* @ngdoc function
* @name ngTranslate.$translateProvider#registerLoaderFactory
* @methodOf ngTranslate.$translateProvider
*
* @description
* Shortcut method for `$translateProvider#registerLoader`.
*
* @param {function | string} loader A string or a function with its dependencies
*
*/
this.useLoaderFactory = function (loader) {
this.registerLoader(loader);
};

/**
* @ngdoc function
* @name ngTranslate.$translateProvider#useLocalStorage
Expand Down
37 changes: 37 additions & 0 deletions test/unit/translateServiceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,43 @@ describe('ngTranslate', function () {

});

describe('register loader with useLoaderFactory()', function () {

beforeEach(module('ngTranslate', function ($translateProvider) {
$translateProvider.useLoaderFactory(function ($q, $timeout) {
return function (key) {
var data = (key !== 'de_DE') ? null : {
'KEY1': 'Schluessel 1',
'KEY2': 'Schluessel 2'
};
var deferred = $q.defer();
$timeout(function () {
deferred.resolve(data);
}, 200);
return deferred.promise;
};
});
}));

it('implicit invoking loader should be successful', inject(function ($translate, $timeout) {
var called = false;
$translate.uses('de_DE').then(function (){
called = true;
});
$timeout.flush();
expect(called).toEqual(true);
}));

it('should return the correct translation after change', inject(function ($translate, $timeout) {
var called = false;
// Check that the start point is the translation id itself.
expect($translate('KEY1')).toEqual('KEY1');
$translate.uses('de_DE');
$timeout.flush(); // finish loader
expect($translate('KEY1')).toEqual('Schluessel 1');
}));
});

describe('register loader as url string', function () {

beforeEach(module('ngTranslate', function ($translateProvider) {
Expand Down

0 comments on commit 2915e8b

Please sign in to comment.