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

Commit

Permalink
feat(service): support for default translation in missingTranslationH…
Browse files Browse the repository at this point in the history
…andler
  • Loading branch information
LoicMahieu authored and knalli committed Mar 5, 2016
1 parent 6a1c9fb commit 8c5044c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1366,14 +1366,16 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
* Translate by missing translation handler.
*
* @param translationId
* @param interpolateParams
* @param defaultTranslationText
* @returns translation created by $missingTranslationHandler or translationId is $missingTranslationHandler is
* absent
*/
var translateByHandler = function (translationId, interpolateParams) {
var translateByHandler = function (translationId, interpolateParams, defaultTranslationText) {
// If we have a handler factory - we might also call it here to determine if it provides
// a default text for a translationid that can't be found anywhere in our tables
if ($missingTranslationHandlerFactory) {
var resultString = $injector.get($missingTranslationHandlerFactory)(translationId, $uses, interpolateParams);
var resultString = $injector.get($missingTranslationHandlerFactory)(translationId, $uses, interpolateParams, defaultTranslationText);
if (resultString !== undefined) {
return resultString;
} else {
Expand Down Expand Up @@ -1420,7 +1422,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
} else {
// if no default translation is set and an error handler is defined, send it to the handler
// and then return the result
deferred.resolve(translateByHandler(translationId, interpolateParams));
deferred.resolve(translateByHandler(translationId, interpolateParams, defaultTranslationText));
}
}
return deferred.promise;
Expand Down Expand Up @@ -1504,7 +1506,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
var missingTranslationHandlerTranslation;
// for logging purposes only (as in $translateMissingTranslationHandlerLog), value is not returned to promise
if ($missingTranslationHandlerFactory && !pendingLoader) {
missingTranslationHandlerTranslation = translateByHandler(translationId, interpolateParams);
missingTranslationHandlerTranslation = translateByHandler(translationId, interpolateParams, defaultTranslationText);
}

// since we couldn't translate the inital requested translation id,
Expand Down
23 changes: 19 additions & 4 deletions test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1950,8 +1950,8 @@ describe('pascalprecht.translate', function () {
.useMissingTranslationHandler('customHandler');

$provide.factory('customHandler', function () {
return function (translationId, language, params) {
missingTranslations[translationId] = { lang: language, params: params };
return function (translationId, language, params, defaultTranslation) {
missingTranslations[translationId] = { lang: language, params: params, defaultTranslation: defaultTranslation };
};
});

Expand All @@ -1973,7 +1973,8 @@ describe('pascalprecht.translate', function () {
expect(missingTranslations).toEqual({
'NOT_EXISTING_TRANSLATION_ID': {
lang: 'en',
params: {}
params: {},
defaultTranslation: undefined
}
});
});
Expand All @@ -1985,7 +1986,21 @@ describe('pascalprecht.translate', function () {
lang: 'en',
params: {
name: 'name'
}
},
defaultTranslation: undefined
}
});
});

it('should pass on defaultTranslationText to missingTranslationHandler', function () {
$translate('NOT_EXISTING_TRANSLATION_ID', {name: 'name'}, '', 'DEFAULT');
expect(missingTranslations).toEqual({
'NOT_EXISTING_TRANSLATION_ID': {
lang: 'en',
params: {
name: 'name'
},
defaultTranslation: 'DEFAULT'
}
});
});
Expand Down

0 comments on commit 8c5044c

Please sign in to comment.