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

Commit

Permalink
feat(translateService): adds method to configure indicators for not f…
Browse files Browse the repository at this point in the history
…ound translations

- adds translateProvider.translationNotFoundIndicatorLeft()
- adds translateProvider.translationNotFoundIndicatorRight()
- adds translateProvider.translationNotFoundIndicator()

the latter makes use of the former two methods. this means you could set
both indicators explicitly if needed.

Closes #77
  • Loading branch information
0x-r4bbit committed Jul 15, 2013
1 parent 629bb8d commit 52a039f
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
$missingTranslationHandlerFactory,
$loaderFactory,
$loaderOptions,
$notFoundIndicatorLeft,
$notFoundIndicatorRight,
NESTED_OBJECT_DELIMITER = '.';

/**
Expand Down Expand Up @@ -165,6 +167,66 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
}
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#translationNotFoundIndicator
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Sets an indicator which is used when a translation isn't found. E.g. when
* setting the indicator as 'X' and one tries to translate a translation id
* called `NOT_FOUND`, this will result in `X NOT_FOUND X`.
*
* Internally this methods sets a left indicator and a right indicator using
* `$translateProvider.translationNotFoundIndicatorLeft()` and
* `$translateProvider.translationNotFoundIndicatorRight()`.
*
* **Note**: These methods automatically add a whitespace between the indicators
* and the translation id.
*
* @param {string} indicator An indicator, could be any string.
*/
this.translationNotFoundIndicator = function (indicator) {
this.translationNotFoundIndicatorLeft(indicator);
this.translationNotFoundIndicatorRight(indicator);
};

/**
* ngdoc function
* @name pascalprecht.translate.$translateProvider#translationNotFoundIndicatorLeft
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Sets an indicator which is used when a translation isn't found left to the
* translation id.
*
* @param {string} indicator An indicator.
*/
this.translationNotFoundIndicatorLeft = function (indicator) {
if (!indicator) {
return $notFoundIndicatorLeft;
}
$notFoundIndicatorLeft = indicator;
};

/**
* ngdoc function
* @name pascalprecht.translate.$translateProvider#translationNotFoundIndicatorLeft
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Sets an indicator which is used when a translation isn't found right to the
* translation id.
*
* @param {string} indicator An indicator.
*/
this.translationNotFoundIndicatorRight = function (indicator) {
if (!indicator) {
return $notFoundIndicatorRight;
}
$notFoundIndicatorRight = indicator;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#fallbackLanguage
Expand Down Expand Up @@ -439,6 +501,14 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
}
}

if ($notFoundIndicatorLeft) {
translationId = [$notFoundIndicatorLeft, translationId].join(' ');
}

if ($notFoundIndicatorRight) {
translationId = [translationId, $notFoundIndicatorRight].join(' ');
}

return translationId;
};

Expand Down
41 changes: 41 additions & 0 deletions test/unit/translateServiceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,45 @@ describe('pascalprecht.translate', function () {
});
});
});

describe('translationNotFoundIndicator', function () {

describe('setting both indicators implicitly', function () {
beforeEach(module('pascalprecht.translate', function ($translateProvider) {
$translateProvider.translationNotFoundIndicator('✘');
}));

it('should inject indicators for not found translations', function () {
inject(function ($translate) {
expect($translate('NOT_FOUND')).toEqual('✘ NOT_FOUND ✘');
});
});
});

describe('setting left indicator explicitly', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider) {
$translateProvider.translationNotFoundIndicatorLeft('✘');
}));

it('should inject left indicator for not found translations', function () {
inject(function ($translate) {
expect($translate('NOT_FOUND')).toEqual('✘ NOT_FOUND');
});
});
});

describe('setting right indicator explicitly', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider) {
$translateProvider.translationNotFoundIndicatorRight('✘');
}));

it('should inject right indicator for not found translations', function () {
inject(function ($translate) {
expect($translate('NOT_FOUND')).toEqual('NOT_FOUND ✘');
});
});
});
});
});

0 comments on commit 52a039f

Please sign in to comment.