This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(translateProvider): add use*() methods for async loaders
- add translateProvider.useUrlLoader(), - add translateProvider.useStaticFilesLoader(), - add translateProvider.useLoader() One is now able to define a loader service factory and just use it via useLoader(). This makes code more testable and maintainable. registerLoader() is now deprecated. Closes #58
- Loading branch information
Showing
6 changed files
with
335 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
angular.module('pascalprecht.translate') | ||
/** | ||
* @ngdoc factory | ||
* @name pascalprecht.translate.factory:$translateUrlLoader | ||
* @requires $q | ||
* @requires $http | ||
* | ||
* @description | ||
* Creates a loading function for a typical static file url pattern: | ||
* "lang-en_US.json", "lang-de_DE.json", etc. Using this builder, | ||
* the response of these urls must be an object of key-value pairs. | ||
* | ||
* @param {object} options Options object, which gets prefix, suffix and key. | ||
*/ | ||
.factory('$translateStaticFilesLoader', ['$q', '$http', function ($q, $http) { | ||
|
||
return function (options) { | ||
|
||
if (!options || (!options.prefix || !options.suffix)) { | ||
throw new Error('Couldn\'t load static files, no prefix or suffix specified!'); | ||
} | ||
|
||
var deferred = $q.defer(); | ||
|
||
$http({ | ||
url: [ | ||
options.prefix, | ||
options.key, | ||
options.suffix | ||
].join(''), | ||
method: 'GET', | ||
params: '' | ||
}).success(function (data) { | ||
deferred.resolve(data); | ||
}).error(function (data) { | ||
deferred.reject(options.key); | ||
}); | ||
|
||
return deferred.promise; | ||
}; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
angular.module('pascalprecht.translate') | ||
/** | ||
* @ngdoc factory | ||
* @name pascalprecht.translate.factory:$translateUrlLoader | ||
* @requires $q | ||
* @requires $http | ||
* | ||
* @description | ||
* Creates a loading function for a typical dynamic url pattern: | ||
* "locale.php?lang=en_US", "locale.php?lang=de_DE", etc. Prefixing the specified | ||
* url, the current requested, language id will be applied with "?lang={key}". | ||
* Using this service, the response of these urls must be an object of | ||
* key-value pairs. | ||
* | ||
* @param {object} options Options object, which gets the url and key. | ||
*/ | ||
.factory('$translateUrlLoader', ['$q', '$http', function ($q, $http) { | ||
|
||
return function (options) { | ||
|
||
if (!options || !options.url) { | ||
throw new Error('Couldn\'t use urlLoader since no url is given!'); | ||
} | ||
|
||
var deferred = $q.defer(); | ||
|
||
$http({ | ||
url: options.url, | ||
params: { lang: options.key }, | ||
method: 'GET' | ||
}).success(function (data) { | ||
deferred.resolve(data); | ||
}).error(function (data) { | ||
deferred.reject(options.key); | ||
}); | ||
|
||
return deferred.promise; | ||
}; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
describe('pascalprecht.translate', function () { | ||
|
||
describe('$translateStaticFilesLoader', function () { | ||
|
||
var $translate, $httpBackend; | ||
|
||
beforeEach(module('pascalprecht.translate')); | ||
|
||
beforeEach(inject(function (_$translate_, _$httpBackend_) { | ||
$httpBackend = _$httpBackend_; | ||
$translate = _$translate_; | ||
|
||
$httpBackend.when('GET', 'lang_de_DE.json').respond({HEADER: 'Ueberschrift'}); | ||
$httpBackend.when('GET', 'lang_en_US.json').respond({HEADER:'Header'}); | ||
$httpBackend.when('GET', 'lang_nt_VD.json').respond(404); | ||
})); | ||
|
||
afterEach(function() { | ||
$httpBackend.verifyNoOutstandingExpectation(); | ||
$httpBackend.verifyNoOutstandingRequest(); | ||
}); | ||
|
||
it('should be defined', function () { | ||
inject(function ($translateStaticFilesLoader) { | ||
expect($translateStaticFilesLoader).toBeDefined(); | ||
}); | ||
}); | ||
|
||
it('should be a function', function () { | ||
inject(function ($translateStaticFilesLoader) { | ||
expect(typeof $translateStaticFilesLoader).toBe('function'); | ||
}); | ||
}); | ||
|
||
it('should throw an error when called without prefix or suffix', function () { | ||
inject(function ($translateStaticFilesLoader) { | ||
expect(function () { | ||
$translateStaticFilesLoader(); | ||
}).toThrow('Couldn\'t load static files, no prefix or suffix specified!'); | ||
}); | ||
}); | ||
|
||
it('should fetch static files when invoking', function () { | ||
inject(function ($translateStaticFilesLoader) { | ||
$httpBackend.expectGET('lang_de_DE.json'); | ||
$translateStaticFilesLoader({ | ||
key: 'de_DE', | ||
prefix: 'lang_', | ||
suffix: '.json' | ||
}); | ||
$httpBackend.flush(); | ||
}); | ||
}); | ||
|
||
it('should return a promise', function () { | ||
inject(function ($translateStaticFilesLoader) { | ||
var promise = $translateStaticFilesLoader({ | ||
key: 'de_DE', | ||
prefix: 'lang_', | ||
suffix: '.json' | ||
}); | ||
expect(promise.then).toBeDefined(); | ||
expect(typeof promise.then).toBe('function'); | ||
$httpBackend.flush(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
describe('pascalprecht.translate', function () { | ||
|
||
describe('$translateUrlLoader', function () { | ||
|
||
var $translate, $httpBackend; | ||
|
||
beforeEach(module('pascalprecht.translate')); | ||
|
||
beforeEach(inject(function (_$translate_, _$httpBackend_) { | ||
$translate = _$translate_; | ||
$httpBackend = _$httpBackend_; | ||
|
||
$httpBackend.when('GET', 'foo/bar.json?lang=de_DE').respond({ | ||
it: 'works' | ||
}); | ||
})); | ||
|
||
afterEach(function () { | ||
$httpBackend.verifyNoOutstandingExpectation(); | ||
$httpBackend.verifyNoOutstandingRequest(); | ||
}); | ||
|
||
it('should be defined', function () { | ||
inject(function ($translateUrlLoader) { | ||
expect($translateUrlLoader).toBeDefined(); | ||
}); | ||
}); | ||
|
||
it('should be a function', function () { | ||
inject(function ($translateUrlLoader) { | ||
expect(typeof $translateUrlLoader).toBe('function'); | ||
}); | ||
}); | ||
|
||
it('should throw an error when called without url option', function () { | ||
inject(function ($translateUrlLoader) { | ||
expect(function () { | ||
$translateUrlLoader(); | ||
}).toThrow('Couldn\'t use urlLoader since no url is given!'); | ||
}); | ||
}); | ||
|
||
it('should fetch url when invoking', function () { | ||
inject(function ($translateUrlLoader) { | ||
$httpBackend.expectGET('foo/bar.json?lang=de_DE'); | ||
$translateUrlLoader({ | ||
key: 'de_DE', | ||
url: 'foo/bar.json' | ||
}); | ||
$httpBackend.flush(); | ||
}); | ||
}); | ||
|
||
it('should return a promise', function () { | ||
inject(function ($translateUrlLoader) { | ||
var promise = $translateUrlLoader({ | ||
key: 'de_DE', | ||
url: 'foo/bar.json' | ||
}); | ||
expect(promise.then).toBeDefined(); | ||
expect(typeof promise.then).toBe('function'); | ||
$httpBackend.flush(); | ||
}); | ||
}); | ||
}); | ||
}); |