Skip to content

Instantly share code, notes, and snippets.

@gabrielfgularte
Last active August 29, 2015 14:10
Show Gist options
  • Save gabrielfgularte/8d8b0e934b254b46284a to your computer and use it in GitHub Desktop.
Save gabrielfgularte/8d8b0e934b254b46284a to your computer and use it in GitHub Desktop.
AngularBindTranslate (Extends http://angular-translate.github.io/)
// When using angular-translate it's impossible to make two-way data binding translations yet,
// and this sometimes can be a headache. So I've developed this directive to save some time.
// The view goes like this: <TAG data-bind-translate="scopeObjToTranslate.item.text"></TAG>.
angular.module('myApp', ['pascalprecht.translate']).directive('bindTranslate', [
'$translate',
'$parse',
function($translate, $parse) {
return {
restrict: 'A',
link: function($scope, iElm, iAttrs) {
var arrayToTranslate = iAttrs.bindTranslate.split('.'),
objToTranslate = '';
for (var i = 0; i < arrayToTranslate.length; i++) {
if (objToTranslate.length > 0) {
objToTranslate = objToTranslate + '.' + arrayToTranslate[i];
} else {
objToTranslate = arrayToTranslate[i];
}
};
$scope.$watch(objToTranslate, function(newVal) {
$translate(newVal).then(function (translation) {
iElm.html(translation);
});
});
}
};
}
]);
@mariojunior
Copy link

nice! o/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment