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

Commit

Permalink
feat(missingFallbackDefaultText): enables a feature to return a defau…
Browse files Browse the repository at this point in the history
…lt text for displaying in case of missing translations including fallback stack

In case you have defined a missingTranslationHandlerFactory, you now have the possibility to define a return value which is set instead of the translationId.
This gives a lot of flexibility for certain use cases.
Example factory:
        $provide.factory('customTranslationHandler', function () {
          return function (translationID, uses) {

          };
        });
  • Loading branch information
tspaeth authored and 0x-r4bbit committed Apr 22, 2014
1 parent 1572b59 commit f24b15e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,19 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
);
} else {
// No translation found in any fallback language
deferred.resolve(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) {
deferred.resolve(resultString);
} else {
deferred.resolve(translationId);
}
} else {
deferred.resolve(translationId);
}
}
return deferred.promise;
};
Expand Down
42 changes: 41 additions & 1 deletion test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,14 +472,23 @@ describe('pascalprecht.translate', function () {

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

beforeEach(module('pascalprecht.translate', function ($translateProvider) {
beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {
$translateProvider
.translations('de_DE', translationMock)
.translations('en_EN', { 'TRANSLATION__ID': 'bazinga' })
.translations('fr_FR', { 'SUPERTEST': 'it works!' })
.translations('en_UK', { 'YAY': 'it really does!' })
.fallbackLanguage(['en_EN', 'fr_FR', 'en_UK'])
.preferredLanguage('de_DE');

// factory provides a default fallback text being defined in the factory
// gives a maximum of flexibility
$provide.factory('customTranslationHandler', function () {
return function (translationID, uses) {
return 'NO KEY FOUND';
};
});
$translateProvider.useMissingTranslationHandler('customTranslationHandler');
}));

var $translate, $rootScope, $q;
Expand Down Expand Up @@ -518,8 +527,39 @@ describe('pascalprecht.translate', function () {
expect(value[2]).toEqual('it works!');
expect(value[3]).toEqual('it really does!');
});


it('should use fallback languages and miss one of the translation keys', function () {
var deferred = $q.defer(),
promise = deferred.promise,
value;

promise.then(function (translations) {
value = translations;
});

$q.all([
$translate('EXISTING_TRANSLATION_ID'),
$translate('TRANSLATION__ID'),
$translate('SUPERTEST'),
$translate('YAY'),
$translate('NOT_EXISTING')
]).then(function (translations) {
deferred.resolve(translations);
});

$rootScope.$digest();

expect(value[0]).toEqual('foo');
expect(value[1]).toEqual('bazinga');
expect(value[2]).toEqual('it works!');
expect(value[3]).toEqual('it really does!');
expect(value[4]).toEqual('NO KEY FOUND');
});
});



describe('registered loader', function () {

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

0 comments on commit f24b15e

Please sign in to comment.