Skip to content

Commit ababb8a

Browse files
Fix duplicate translation and improve performance
1 parent 7ec0c74 commit ababb8a

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/directive.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ angular.module('gettext').directive('translate', function (gettextCatalog, $pars
4444
post: function (scope, element, attrs) {
4545
var countFn = $parse(attrs.translateN);
4646
var pluralScope = null;
47+
var linking = true;
4748

4849
function update() {
4950
// Fetch correct translated string.
@@ -56,11 +57,22 @@ angular.module('gettext').directive('translate', function (gettextCatalog, $pars
5657
translated = gettextCatalog.getString(msgid, null, translateContext);
5758
}
5859

60+
var oldContents = element.contents();
61+
62+
// Avoid redundant swaps
63+
if(translated === oldContents[0].textContent){
64+
// Take care of unlinked content
65+
if(linking){
66+
$compile(oldContents)(scope);
67+
}
68+
return;
69+
}
70+
5971
// Swap in the translation
6072
var newWrapper = angular.element('<span>' + translated + '</span>');
6173
$compile(newWrapper.contents())(scope);
62-
var oldContents = element.contents();
6374
var newContents = newWrapper.contents();
75+
6476
$animate.enter(newContents, element);
6577
$animate.leave(oldContents);
6678
}
@@ -72,6 +84,7 @@ angular.module('gettext').directive('translate', function (gettextCatalog, $pars
7284
scope.$on('gettextLanguageChanged', update);
7385

7486
update();
87+
linking = false;
7588
}
7689
};
7790
}

0 commit comments

Comments
 (0)