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

Commit

Permalink
feat(translateDirective): teaches directive custom translate-value-* …
Browse files Browse the repository at this point in the history
…attr

you're now able to add translate values via

```js
<ANY translate="TRANSLATION_ID"
     translate-value-foo="bar"
     translate-value-another="yay"
     ></ANY>
```

you're able to pass in a string or an interpolation so things like

```js
<ANY translate="TRANSLATION_ID"
     translate-value-foo="{{evaluate}}"
     ></ANY>
```

also work.

And yea this feature is progressive enhancement which means you can use it
along with `translate-values` as well without breaking your code. :)

Fixes #188
  • Loading branch information
0x-r4bbit committed Dec 19, 2013
1 parent 5f41952 commit 5c27467
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/directive/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,18 @@ angular.module('pascalprecht.translate')
scope: true,
compile: function (tElement, tAttr) {

var translateValuesExist = (tAttr.translateValues && !angular.equals(tAttr.translateValues, ''));
var translateInterpolation = (tAttr.translateInterpolation) ? tAttr.translateInterpolation : undefined;
var translateValuesExist = (tAttr.translateValues) ?
tAttr.translateValues : undefined

var translateInterpolation = (tAttr.translateInterpolation) ?
tAttr.translateInterpolation : undefined;

var translateValueExist = tElement[0].outerHTML.match(/translate-value-+/i);

This comment has been minimized.

Copy link
@knalli

knalli Dec 19, 2013

Member

constant for that regexp being perhaps more flexible?

/update: at least extracted internally

This comment has been minimized.

Copy link
@0x-r4bbit

0x-r4bbit Dec 20, 2013

Author Member

For now it'd be just an added assignment. I'd say we leave how it is and change it, once we need the RegEx somewhere else, is that okay for you? Or are there any other concerns?

This comment has been minimized.

Copy link
@knalli

knalli via email Dec 20, 2013

Member

return function linkFn(scope, iElement, iAttr) {

scope.interpolateParams = {};

// Ensures any change of the attribute "translate" containing the id will
// be re-stored to the scope's "translationId".
// If the attribute has no content, the element's text value (white spaces trimmed off) will be used.
Expand All @@ -104,14 +111,27 @@ angular.module('pascalprecht.translate')
iAttr.$observe('translateValues', function (interpolateParams) {
if (interpolateParams) {
scope.$parent.$watch(function () {
scope.interpolateParams = $parse(interpolateParams)(scope.$parent);
angular.extend(scope.interpolateParams, $parse(interpolateParams)(scope.$parent));
});
}
});
}

if (translateValueExist) {
for (attr in iAttr) {
if (iAttr.hasOwnProperty(attr) && attr.substr(0, 14) === 'translateValue' && attr !== 'translateValues') {
(function () {
var attrName = attr;
iAttr.$observe(attr, function (value) {
scope.interpolateParams[angular.lowercase(attrName.substr(14))] = value;
});
}());
}
}
}

var updateTranslationFn = (function () {
if (!translateValuesExist) {
if (!translateValuesExist && !translateValueExist) {
return function () {
var unwatch = scope.$watch('translationId', function (value) {
if (value) {
Expand Down
47 changes: 47 additions & 0 deletions test/unit/directive/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,51 @@ describe('pascalprecht.translate', function () {
expect(element.text()).toEqual('custom interpolation');
});
});

describe('custom translate-value-* attributes', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider) {
$translateProvider.translations({
'FOO': 'hello my name is {{name}}',
'BAR': 'and I\'m {{age}} years old',
'BAZINGA': 'hello my name is {{name}} and I\'m {{age}} years old.',
'YAY': 'hello my name is {{name}} and I\'m {{age}} years old. {{foo}}'
});
}));

beforeEach(inject(function (_$rootScope_, _$compile_) {
$rootScope = _$rootScope_;
$compile = _$compile_;

$rootScope.name = 'Pascal';
$rootScope.age = 22;
}));

it('should use custom translate-value-* attributes for variable replacement', function () {
element = $compile('<p translate="FOO" translate-value-name="Pascal"></p>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('hello my name is Pascal');
element = $compile('<p translate="BAR" translate-value-age="22"></p>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('and I\'m 22 years old');
element = $compile('<p translate="BAZINGA" translate-value-name="Pascal" translate-value-age="22"></p>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('hello my name is Pascal and I\'m 22 years old.');
element = $compile('<p translate="YAY" translate-value-name="Pascal" translate-value-age="22" translate-values=\'{ foo: "bar" }\'></p>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('hello my name is Pascal and I\'m 22 years old. bar');
});

it('should use custom translate-value-* attributes with interpolation', function () {
element = $compile('<p translate="FOO" translate-value-name="{{name}}"></p>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('hello my name is Pascal');
element = $compile('<p translate="BAR" translate-value-age="{{age}}"></p>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('and I\'m 22 years old');
element = $compile('<p translate="BAZINGA" translate-value-name="{{name}}" translate-value-age="{{age}}"></p>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('hello my name is Pascal and I\'m 22 years old.');
});
});
});

2 comments on commit 5c27467

@0x-r4bbit
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@knalli this is so neat <3

@knalli
Copy link
Member

@knalli knalli commented on 5c27467 Dec 19, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep :)

Please sign in to comment.