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

Commit

Permalink
feat(directive): add option to define a default translation text
Browse files Browse the repository at this point in the history
This adds an optional opt-in feature:

```html
<span translate=„my.key.for.a.welcome.message“ translate-default=„Welcome {{username}}!“></span>
```

or even

```html
<span translate translate-default=„Welcome {{username}}!“>my.key.for.a.welcome.message</span>
```
  • Loading branch information
knalli authored and 0x-r4bbit committed Apr 1, 2014
1 parent 57bd07c commit fc57d26
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/directive/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ angular.module('pascalprecht.translate')
}
});

iAttr.$observe('translateDefault', function (value) {
scope.defaultText = value;
});

if (translateValuesExist) {
iAttr.$observe('translateValues', function (interpolateParams) {
if (interpolateParams) {
Expand All @@ -129,7 +133,10 @@ angular.module('pascalprecht.translate')
}
}

var applyElementContent = function (value, scope) {
var applyElementContent = function (value, scope, successful) {
if (!successful && typeof scope.defaultText !== 'undefined') {
value = scope.defaultText;
}
iElement.html(value);
var globallyEnabled = $translate.isPostCompilingEnabled();
var locallyDefined = typeof tAttr.translateCompile !== 'undefined';
Expand All @@ -146,10 +153,10 @@ angular.module('pascalprecht.translate')
if (scope.translationId && value) {
$translate(value, {}, translateInterpolation)
.then(function (translation) {
applyElementContent(translation, scope);
applyElementContent(translation, scope, true);
unwatch();
}, function (translationId) {
applyElementContent(translationId, scope);
applyElementContent(translationId, scope, false);
unwatch();
});
}
Expand All @@ -162,9 +169,9 @@ angular.module('pascalprecht.translate')
if (scope.translationId && scope.interpolateParams) {
$translate(scope.translationId, scope.interpolateParams, translateInterpolation)
.then(function (translation) {
applyElementContent(translation, scope);
applyElementContent(translation, scope, true);
}, function (translationId) {
applyElementContent(translationId, scope);
applyElementContent(translationId, scope, false);
});
}
};
Expand Down
19 changes: 19 additions & 0 deletions test/unit/directive/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ describe('pascalprecht.translate', function () {
expect(element.text()).toBe('TEXT');
});

it('should return default text if translation doesn\'t exist', function () {
element = $compile('<div translate="TEXT" translate-default="Not translated"></div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('Not translated');
});

it('should return default text if translation doesn\'t exist', function () {
element = $compile('<div translate translate-default="Not translated">TEXT</div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('Not translated');
});

it('should return interpolated default text if translation doesn\'t exist', function () {
$rootScope.v = '123';
element = $compile('<div translate="TEXT" translate-default="Not translated {{v}}"></div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('Not translated 123');
});

it('should return translation if translation id exist', function () {
element = $compile('<div translate="TRANSLATION_ID"></div>')($rootScope);
$rootScope.$digest();
Expand Down

0 comments on commit fc57d26

Please sign in to comment.