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

Commit

Permalink
feat($translatePartialLoader): accept function in urlTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris authored and knalli committed Apr 8, 2015
1 parent 798f647 commit 401204a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/service/loader-partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ angular.module('pascalprecht.translate')
* Returns a parsed url template string and replaces given target lang
* and part name it.
*
* @param {string} urlTemplate Url pattern to use.
* @param {string} targetLang Language key for language to be used.
* @param {string|function} urlTemplate - Either a string containing an url pattern (with
* '{part}' and '{lang}') or a function(part, lang)
* returning a string.
* @param {string} targetLang - Language key for language to be used.
* @return {string} Parsed url template string
*/
Part.prototype.parseUrl = function(urlTemplate, targetLang) {
if (angular.isFunction(urlTemplate)) {
return urlTemplate(this.name, targetLang);
}
return urlTemplate.replace(/\{part\}/g, this.name).replace(/\{lang\}/g, targetLang);
};

Expand Down Expand Up @@ -272,8 +277,8 @@ angular.module('pascalprecht.translate')
throw new TypeError('Unable to load data, a key is not a non-empty string.');
}

if (!isStringValid(options.urlTemplate)) {
throw new TypeError('Unable to load data, a urlTemplate is not a non-empty string.');
if (!isStringValid(options.urlTemplate) && !angular.isFunction(options.urlTemplate)) {
throw new TypeError('Unable to load data, a urlTemplate is not a non-empty string or not a function.');
}

var errorHandler = options.loadFailureHandler;
Expand Down
20 changes: 20 additions & 0 deletions test/unit/service/loader-partial.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,26 @@ describe('pascalprecht.translate', function() {
});
});

it('should support function in url template', function() {
inject(function($translatePartialLoader, $httpBackend) {
var getUrlTemplate = jasmine.createSpy('getUrlTemplate')
.and.returnValue('/locales/data.json');

$httpBackend.expectGET('/locales/data.json').respond(200, '{}');

$translatePartialLoader.addPart('part');
$translatePartialLoader({
key : 'en',
urlTemplate : getUrlTemplate
});

$httpBackend.flush();
expect(getUrlTemplate).toHaveBeenCalledWith('part', 'en');
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
});

it('should parse url template with multiple pattern occurrences', function () {
inject(function($translatePartialLoader, $httpBackend) {
$httpBackend.expectGET('/locales/part/part-en.json').respond(200, '{}');
Expand Down

0 comments on commit 401204a

Please sign in to comment.