From b4e286d72d4dc1caebaf2d2f3dc276071ee7f5eb Mon Sep 17 00:00:00 2001 From: Gabe Gorelick Date: Fri, 5 Dec 2014 18:44:03 -0500 Subject: [PATCH] Allow passing scope variable directly to directive E.g. `
` --- dist/angular-gettext.js | 13 ++++++++++++- dist/angular-gettext.min.js | 2 +- src/directive.js | 13 ++++++++++++- test/unit/directive.js | 21 +++++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/dist/angular-gettext.js b/dist/angular-gettext.js index 4790821..0e17639 100644 --- a/dist/angular-gettext.js +++ b/dist/angular-gettext.js @@ -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) { @@ -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(); diff --git a/dist/angular-gettext.min.js b/dist/angular-gettext.min.js index 8424fd2..6d0c69f 100644 --- a/dist/angular-gettext.min.js +++ b/dist/angular-gettext.min.js @@ -1 +1 @@ -angular.module("gettext",[]),angular.module("gettext").constant("gettext",function(a){return a}),angular.module("gettext").factory("gettextCatalog",["gettextPlurals","$http","$cacheFactory","$interpolate","$rootScope",function(a,b,c,d,e){function f(){e.$broadcast("gettextLanguageChanged")}var g,h=function(a){return g.debug&&g.currentLanguage!==g.baseLanguage?g.debugPrefix+a:a},i=function(a){return g.showTranslatedMarkers?g.translatedMarkerPrefix+a+g.translatedMarkerSuffix:a};return g={debug:!1,debugPrefix:"[MISSING]: ",showTranslatedMarkers:!1,translatedMarkerPrefix:"[",translatedMarkerSuffix:"]",strings:{},baseLanguage:"en",currentLanguage:"en",cache:c("strings"),setCurrentLanguage:function(a){this.currentLanguage=a,f()},setStrings:function(a,b){this.strings[a]||(this.strings[a]={});for(var c in b){var d=b[c];this.strings[a][c]="string"==typeof d?[d]:d}f()},getStringForm:function(a,b){var c=this.strings[this.currentLanguage]||{},d=c[a]||[];return d[b]},getString:function(a,b){return a=this.getStringForm(a,0)||h(a),a=b?d(a)(b):a,i(a)},getPlural:function(b,c,e,f){var g=a(this.currentLanguage,b);return c=this.getStringForm(c,g)||h(1===b?c:e),c=f?d(c)(f):c,i(c)},loadRemote:function(a){return b({method:"GET",url:a,cache:g.cache}).success(function(a){for(var b in a)g.setStrings(b,a[b])})}}}]),angular.module("gettext").directive("translate",["gettextCatalog","$parse","$animate","$compile",function(a,b,c,d){function e(a,b,c){if(!a)throw new Error("You should add a "+b+" attribute whenever you add a "+c+" attribute.")}var f=function(){return String.prototype.trim?function(a){return"string"==typeof a?a.trim():a}:function(a){return"string"==typeof a?a.replace(/^\s*/,"").replace(/\s*$/,""):a}}();return{restrict:"A",terminal:!0,compile:function(g,h){e(!h.translatePlural||h.translateN,"translate-n","translate-plural"),e(!h.translateN||h.translatePlural,"translate-plural","translate-n");var i=f(g.html()),j=h.translatePlural;return{post:function(e,f,g){function h(){var b;j?(e=l||(l=e.$new()),e.$count=k(e),b=a.getPlural(e.$count,i,j)):b=a.getString(i);var g=angular.element(""+b+"");d(g.contents())(e);var h=f.contents(),m=g.contents();c.enter(m,f),c.leave(h)}var k=b(g.translateN),l=null;g.translateN&&e.$watch(g.translateN,h),e.$on("gettextLanguageChanged",h),h()}}}}}]),angular.module("gettext").filter("translate",["gettextCatalog",function(a){function b(b){return a.getString(b)}return b.$stateful=!0,b}]),angular.module("gettext").factory("gettextPlurals",function(){return function(a,b){switch(a){case"ay":case"bo":case"cgg":case"dz":case"fa":case"id":case"ja":case"jbo":case"ka":case"kk":case"km":case"ko":case"ky":case"lo":case"ms":case"my":case"sah":case"su":case"th":case"tt":case"ug":case"vi":case"wo":case"zh":return 0;case"is":return b%10!=1||b%100==11?1:0;case"jv":return 0!=b?1:0;case"mk":return 1==b||b%10==1?0:1;case"ach":case"ak":case"am":case"arn":case"br":case"fil":case"fr":case"gun":case"ln":case"mfe":case"mg":case"mi":case"oc":case"pt_BR":case"tg":case"ti":case"tr":case"uz":case"wa":case"zh":return b>1?1:0;case"lv":return b%10==1&&b%100!=11?0:0!=b?1:2;case"lt":return b%10==1&&b%100!=11?0:b%10>=2&&(10>b%100||b%100>=20)?1:2;case"be":case"bs":case"hr":case"ru":case"sr":case"uk":return b%10==1&&b%100!=11?0:b%10>=2&&4>=b%10&&(10>b%100||b%100>=20)?1:2;case"mnk":return 0==b?0:1==b?1:2;case"ro":return 1==b?0:0==b||b%100>0&&20>b%100?1:2;case"pl":return 1==b?0:b%10>=2&&4>=b%10&&(10>b%100||b%100>=20)?1:2;case"cs":case"sk":return 1==b?0:b>=2&&4>=b?1:2;case"sl":return b%100==1?1:b%100==2?2:b%100==3||b%100==4?3:0;case"mt":return 1==b?0:0==b||b%100>1&&11>b%100?1:b%100>10&&20>b%100?2:3;case"gd":return 1==b||11==b?0:2==b||12==b?1:b>2&&20>b?2:3;case"cy":return 1==b?0:2==b?1:8!=b&&11!=b?2:3;case"kw":return 1==b?0:2==b?1:3==b?2:3;case"ga":return 1==b?0:2==b?1:7>b?2:11>b?3:4;case"ar":return 0==b?0:1==b?1:2==b?2:b%100>=3&&10>=b%100?3:b%100>=11?4:5;default:return 1!=b?1:0}}}); \ No newline at end of file +angular.module("gettext",[]),angular.module("gettext").constant("gettext",function(a){return a}),angular.module("gettext").factory("gettextCatalog",["gettextPlurals","$http","$cacheFactory","$interpolate","$rootScope",function(a,b,c,d,e){function f(){e.$broadcast("gettextLanguageChanged")}var g,h=function(a){return g.debug&&g.currentLanguage!==g.baseLanguage?g.debugPrefix+a:a},i=function(a){return g.showTranslatedMarkers?g.translatedMarkerPrefix+a+g.translatedMarkerSuffix:a};return g={debug:!1,debugPrefix:"[MISSING]: ",showTranslatedMarkers:!1,translatedMarkerPrefix:"[",translatedMarkerSuffix:"]",strings:{},baseLanguage:"en",currentLanguage:"en",cache:c("strings"),setCurrentLanguage:function(a){this.currentLanguage=a,f()},setStrings:function(a,b){this.strings[a]||(this.strings[a]={});for(var c in b){var d=b[c];this.strings[a][c]="string"==typeof d?[d]:d}f()},getStringForm:function(a,b){var c=this.strings[this.currentLanguage]||{},d=c[a]||[];return d[b]},getString:function(a,b){return a=this.getStringForm(a,0)||h(a),a=b?d(a)(b):a,i(a)},getPlural:function(b,c,e,f){var g=a(this.currentLanguage,b);return c=this.getStringForm(c,g)||h(1===b?c:e),c=f?d(c)(f):c,i(c)},loadRemote:function(a){return b({method:"GET",url:a,cache:g.cache}).success(function(a){for(var b in a)g.setStrings(b,a[b])})}}}]),angular.module("gettext").directive("translate",["gettextCatalog","$parse","$animate","$compile",function(a,b,c,d){function e(a,b,c){if(!a)throw new Error("You should add a "+b+" attribute whenever you add a "+c+" attribute.")}var f=function(){return String.prototype.trim?function(a){return"string"==typeof a?a.trim():a}:function(a){return"string"==typeof a?a.replace(/^\s*/,"").replace(/\s*$/,""):a}}();return{restrict:"A",terminal:!0,compile:function(g,h){e(!h.translatePlural||h.translateN,"translate-n","translate-plural"),e(!h.translateN||h.translatePlural,"translate-plural","translate-n");var i=f(g.html()),j=h.translatePlural;return{post:function(e,f,g){function h(){var b=m(e);if(b){var g;j?(e=l||(l=e.$new()),e.$count=k(e),g=a.getPlural(e.$count,b,j)):g=a.getString(b);var h=angular.element(""+g+"");d(h.contents())(e);var i=f.contents(),n=h.contents();c.enter(n,f),c.leave(i)}}var k=b(g.translateN),l=null,m=g.translate?b(g.translate):function(){return i};g.translateN&&e.$watch(g.translateN,h),g.translate&&e.$watch(g.translate,h),e.$on("gettextLanguageChanged",h),h()}}}}}]),angular.module("gettext").filter("translate",["gettextCatalog",function(a){function b(b){return a.getString(b)}return b.$stateful=!0,b}]),angular.module("gettext").factory("gettextPlurals",function(){return function(a,b){switch(a){case"ay":case"bo":case"cgg":case"dz":case"fa":case"id":case"ja":case"jbo":case"ka":case"kk":case"km":case"ko":case"ky":case"lo":case"ms":case"my":case"sah":case"su":case"th":case"tt":case"ug":case"vi":case"wo":case"zh":return 0;case"is":return b%10!=1||b%100==11?1:0;case"jv":return 0!=b?1:0;case"mk":return 1==b||b%10==1?0:1;case"ach":case"ak":case"am":case"arn":case"br":case"fil":case"fr":case"gun":case"ln":case"mfe":case"mg":case"mi":case"oc":case"pt_BR":case"tg":case"ti":case"tr":case"uz":case"wa":case"zh":return b>1?1:0;case"lv":return b%10==1&&b%100!=11?0:0!=b?1:2;case"lt":return b%10==1&&b%100!=11?0:b%10>=2&&(10>b%100||b%100>=20)?1:2;case"be":case"bs":case"hr":case"ru":case"sr":case"uk":return b%10==1&&b%100!=11?0:b%10>=2&&4>=b%10&&(10>b%100||b%100>=20)?1:2;case"mnk":return 0==b?0:1==b?1:2;case"ro":return 1==b?0:0==b||b%100>0&&20>b%100?1:2;case"pl":return 1==b?0:b%10>=2&&4>=b%10&&(10>b%100||b%100>=20)?1:2;case"cs":case"sk":return 1==b?0:b>=2&&4>=b?1:2;case"sl":return b%100==1?1:b%100==2?2:b%100==3||b%100==4?3:0;case"mt":return 1==b?0:0==b||b%100>1&&11>b%100?1:b%100>10&&20>b%100?2:3;case"gd":return 1==b||11==b?0:2==b||12==b?1:b>2&&20>b?2:3;case"cy":return 1==b?0:2==b?1:8!=b&&11!=b?2:3;case"kw":return 1==b?0:2==b?1:3==b?2:3;case"ga":return 1==b?0:2==b?1:7>b?2:11>b?3:4;case"ar":return 0==b?0:1==b?1:2==b?2:b%100>=3&&10>=b%100?3:b%100>=11?4:5;default:return 1!=b?1:0}}}); \ No newline at end of file diff --git a/src/directive.js b/src/directive.js index ede862e..5e47b0f 100644 --- a/src/directive.js +++ b/src/directive.js @@ -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) { @@ -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(); diff --git a/test/unit/directive.js b/test/unit/directive.js index d581143..fe2b838 100644 --- a/test/unit/directive.js +++ b/test/unit/directive.js @@ -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("
")($rootScope); + $rootScope.$digest(); + assert.equal(el.text(), "Hallo"); + }); + + it("Shouldn't set text until it's initialized", function () { + catalog.currentLanguage = "nl"; + var el = $compile("
")($rootScope); + $rootScope.$digest(); + assert.equal(el.text(), ""); + + $rootScope.greeting = "Hello"; + $rootScope.$digest(); + assert.equal(el.text(), "Hallo"); + }); + }); });