Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing scope variable directly to directive #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,22 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
assert(!attrs.translatePlural || attrs.translateN, 'translate-n', 'translate-plural');
assert(!attrs.translateN || attrs.translatePlural, 'translate-plural', 'translate-n');

var msgid = trim(element.html());
var html = trim(element.html());
var translatePlural = attrs.translatePlural;

return {
post: function (scope, element, attrs) {
var countFn = $parse(attrs.translateN);
var pluralScope = null;
var msgidExp = attrs.translate ? $parse(attrs.translate) : function () { return html; };

function update() {
var msgid = msgidExp(scope);
if (!msgid) {
// wait until msgid is initialized
return;
}

// Fetch correct translated string.
var translated;
if (translatePlural) {
Expand All @@ -161,6 +168,10 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
scope.$watch(attrs.translateN, update);
}

if (attrs.translate) {
scope.$watch(attrs.translate, update);
}

scope.$on('gettextLanguageChanged', update);

update();
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.

13 changes: 12 additions & 1 deletion src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@ angular.module('gettext').directive('translate', function (gettextCatalog, $pars
assert(!attrs.translatePlural || attrs.translateN, 'translate-n', 'translate-plural');
assert(!attrs.translateN || attrs.translatePlural, 'translate-plural', 'translate-n');

var msgid = trim(element.html());
var html = trim(element.html());
var translatePlural = attrs.translatePlural;

return {
post: function (scope, element, attrs) {
var countFn = $parse(attrs.translateN);
var pluralScope = null;
var msgidExp = attrs.translate ? $parse(attrs.translate) : function () { return html; };

function update() {
var msgid = msgidExp(scope);
if (!msgid) {
// wait until msgid is initialized
return;
}

// Fetch correct translated string.
var translated;
if (translatePlural) {
Expand All @@ -58,6 +65,10 @@ angular.module('gettext').directive('translate', function (gettextCatalog, $pars
scope.$watch(attrs.translateN, update);
}

if (attrs.translate) {
scope.$watch(attrs.translate, update);
}

scope.$on('gettextLanguageChanged', update);

update();
Expand Down
21 changes: 21 additions & 0 deletions test/unit/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,25 @@ describe("Directive", function () {
$rootScope.$digest();
assert.equal(el.text(), "Die skakel: http://google.com sal die 'ng-binding' klass aangevoeg hê voor die translate directive dit kan vasvat.");
});

describe("translate=", function () {
it("Should allow passing scope variable directly to translate directive", function () {
$rootScope.greeting = "Hello";
catalog.currentLanguage = "nl";
var el = $compile("<div translate=\"greeting\"></div>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "Hallo");
});

it("Shouldn't set text until it's initialized", function () {
catalog.currentLanguage = "nl";
var el = $compile("<div translate=\"greeting\"></div>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "");

$rootScope.greeting = "Hello";
$rootScope.$digest();
assert.equal(el.text(), "Hallo");
});
});
});