Skip to content

Commit

Permalink
Update the translate directive to gracefully handle the native HTML t…
Browse files Browse the repository at this point in the history
…ranslate="yes|no" attribute.

This change will bypass the normal `angular-gettext` processing when the `translate` attribute has a "yes" or "no" value, and continue processing as normal otherwise.
  • Loading branch information
cmlenz committed Dec 4, 2020
1 parent f5cf0fc commit 86937d6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
10 changes: 10 additions & 0 deletions dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,16 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
restrict: 'AE',
terminal: true,
compile: function compile(element, attrs) {
var translate = attrs.translate;
if (translate && translate.match(/^yes|no$/i)) {
// Ignore the translate attribute if it has a "yes" or "no" value, assuming that it is the HTML
// native translate attribute, see
// https://html.spec.whatwg.org/multipage/dom.html#the-translate-attribute
//
// In that case we skip processing as this attribute is intended for the user agent itself.
return;
}

// Validate attributes
gettextUtil.assert(!attrs.translatePlural || attrs.translateN, 'translate-n', 'translate-plural');
gettextUtil.assert(!attrs.translateN || attrs.translatePlural, 'translate-plural', 'translate-n');
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-gettext.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ angular.module('gettext').directive('translate', function (gettextCatalog, $pars
restrict: 'AE',
terminal: true,
compile: function compile(element, attrs) {
var translate = attrs.translate;
if (translate && translate.match(/^yes|no$/i)) {
// Ignore the translate attribute if it has a "yes" or "no" value, assuming that it is the HTML
// native translate attribute, see
// https://html.spec.whatwg.org/multipage/dom.html#the-translate-attribute
//
// In that case we skip processing as this attribute is intended for the user agent itself.
return;
}

// Validate attributes
gettextUtil.assert(!attrs.translatePlural || attrs.translateN, 'translate-n', 'translate-plural');
gettextUtil.assert(!attrs.translateN || attrs.translatePlural, 'translate-plural', 'translate-n');
Expand Down
25 changes: 25 additions & 0 deletions test/unit/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,29 @@ describe("Directive", function () {
assert.equal(el.text(), "Dziś Ruben spotyka się z Ernest przez 22 minuty.");
});
});

describe("Passthrough native HTML translate attribute", function () {
it("Should ignore translate='no' attribute", function () {
catalog.setCurrentLanguage("nl");
var el = $compile("<div><h1 translate='no'>Hello</h1></div>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "Hello");
assert.equal(el.find("h1").attr("translate"), "no");
});

it("Should ignore translate='yes' attribute", function () {
catalog.setCurrentLanguage("nl");
var el = $compile("<div><h1 translate='yes'>Hello</h1></div>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "Hello");
assert.equal(el.find("h1").attr("translate"), "yes");
});

it.only("Should not ignore the XHTML boolean form translate='translate'", function () {
catalog.setCurrentLanguage("nl");
var el = $compile("<div><h1 translate='translate'>Hello</h1></div>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "Hallo");
});
});
});

0 comments on commit 86937d6

Please sign in to comment.