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

Commit

Permalink
fix($translate): return $missingTranslationHandler result when no tra…
Browse files Browse the repository at this point in the history
…nslation was found

Instant translations do not return result of $missingTranslationHandler invocation.
  • Loading branch information
tunguski authored and 0x-r4bbit committed Jun 16, 2014
1 parent be62131 commit 7625951
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 28 deletions.
77 changes: 49 additions & 28 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,32 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
return result;
};


/**
* @name translateByHandler
* @private
*
* Translate by missing translation handler.
*
* @param translationId
* @returns translation created by $missingTranslationHandler or translationId is $missingTranslationHandler is
* absent
*/
var translateByHandler = function (translationId) {
// 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);
if (resultString !== undefined) {
return resultString;
} else {
return translationId;
}
} else {
return translationId;
}
};

/**
* @name resolveForFallbackLanguage
* @private
Expand Down Expand Up @@ -1050,19 +1076,7 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
);
} else {
// No translation found in any fallback language

// 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);
if (resultString !== undefined) {
deferred.resolve(resultString);
} else {
deferred.resolve(translationId);
}
} else {
deferred.resolve(translationId);
}
deferred.resolve(translateByHandler(translationId));
}
return deferred.promise;
};
Expand Down Expand Up @@ -1139,24 +1153,27 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
deferred.resolve(Interpolator.interpolate(translation, interpolateParams));
}
} else {
// looks like the requested translation id doesn't exists.
// Now, if there is a registered handler for missing translations and no
// asyncLoader is pending, we execute the handler
var missingTranslationHandlerTranslation;
// for logging purposes only (as in $translateMissingTranslationHandlerLog), value is not returned to promise
if ($missingTranslationHandlerFactory && !pendingLoader) {
$injector.get($missingTranslationHandlerFactory)(translationId, $uses);
missingTranslationHandlerTranslation = translateByHandler(translationId);
}

// since we couldn't translate the inital requested translation id,
// we try it now with one or more fallback languages, if fallback language(s) is
// configured.
if ($uses && $fallbackLanguage && $fallbackLanguage.length) {
fallbackTranslation(translationId, interpolateParams, Interpolator)
.then(function (translation) {
deferred.resolve(translation);
}, function (_translationId) {
deferred.reject(applyNotFoundIndicators(_translationId));
});

.then(function (translation) {
deferred.resolve(translation);
}, function (_translationId) {
deferred.reject(applyNotFoundIndicators(_translationId));
});
} else if ($missingTranslationHandlerFactory && !pendingLoader && missingTranslationHandlerTranslation) {
// looks like the requested translation id doesn't exists.
// Now, if there is a registered handler for missing translations and no
// asyncLoader is pending, we execute the handler
deferred.resolve(missingTranslationHandlerTranslation);
} else {
deferred.reject(applyNotFoundIndicators(translationId));
}
Expand All @@ -1180,11 +1197,10 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
result = Interpolator.interpolate(translation, interpolateParams);
}
} else {
// looks like the requested translation id doesn't exists.
// Now, if there is a registered handler for missing translations and no
// asyncLoader is pending, we execute the handler
var missingTranslationHandlerTranslation;
// for logging purposes only (as in $translateMissingTranslationHandlerLog), value is not returned to promise
if ($missingTranslationHandlerFactory && !pendingLoader) {
$injector.get($missingTranslationHandlerFactory)(translationId, $uses);
missingTranslationHandlerTranslation = translateByHandler(translationId);
}

// since we couldn't translate the inital requested translation id,
Expand All @@ -1193,6 +1209,11 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
if ($uses && $fallbackLanguage && $fallbackLanguage.length) {
fallbackIndex = 0;
result = fallbackTranslationInstant(translationId, interpolateParams, Interpolator);
} else if ($missingTranslationHandlerFactory && !pendingLoader && missingTranslationHandlerTranslation) {
// looks like the requested translation id doesn't exists.
// Now, if there is a registered handler for missing translations and no
// asyncLoader is pending, we execute the handler
result = missingTranslationHandlerTranslation;
} else {
result = applyNotFoundIndicators(translationId);
}
Expand Down Expand Up @@ -1577,7 +1598,7 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
// Return translation if not found anything.
result = translationId;
if ($missingTranslationHandlerFactory && !pendingLoader) {
$injector.get($missingTranslationHandlerFactory)(translationId, $uses);
result = translateByHandler(translationId);
}
}

Expand Down
30 changes: 30 additions & 0 deletions test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,36 @@ describe('pascalprecht.translate', function () {
});
});

describe('translate returns handler result', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {
$translateProvider
.translations('de_DE', translationMock)
.preferredLanguage('de_DE');

// factory provides a default fallback text being defined in the factory
// gives a maximum of flexibility
$provide.factory('elementReturningTranslationHandler', function () {
return function (translationID, uses) {
return '<nkf>' + translationID + '</nkf>';
};
});
$translateProvider.useMissingTranslationHandler('elementReturningTranslationHandler');
}));

var $translate, $rootScope, $q;

beforeEach(inject(function (_$translate_, _$rootScope_, _$q_) {
$translate = _$translate_;
$rootScope = _$rootScope_;
$q = _$q_;
}));

it('when instant translate', function () {
expect($translate.instant('false.key')).toBe('<nkf>false.key</nkf>');
});
});

describe('multi fallback language', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {
Expand Down

0 comments on commit 7625951

Please sign in to comment.