Permalink
Browse files

Merge branch 'master' of github.com:andywaite/angular-gettext

Conflicts:
	dist/angular-gettext.min.js
  • Loading branch information...
2 parents c220beb + 17c1236 commit 6acc718082110c05e6789168295ca49315e27c01 @rubenv committed Jun 17, 2014
Showing with 32 additions and 11 deletions.
  1. +8 −5 dist/angular-gettext.js
  2. +1 −1 dist/angular-gettext.min.js
  3. +7 −5 src/catalog.js
  4. +16 −0 test/unit/catalog.coffee
View
13 dist/angular-gettext.js
@@ -12,7 +12,8 @@ angular.module('gettext').factory('gettextCatalog', [
'gettextPlurals',
'$http',
'$cacheFactory',
- function (gettextPlurals, $http, $cacheFactory) {
+ '$interpolate',
+ function (gettextPlurals, $http, $cacheFactory, $interpolate) {
var catalog;
var prefixDebug = function (string) {
if (catalog.debug && catalog.currentLanguage !== catalog.baseLanguage) {
@@ -45,12 +46,14 @@ angular.module('gettext').factory('gettextCatalog', [
var plurals = stringTable[string] || [];
return plurals[n];
},
- getString: function (string) {
- return this.getStringForm(string, 0) || prefixDebug(string);
+ getString: function (string, context) {
+ string = this.getStringForm(string, 0) || prefixDebug(string);
+ return context ? $interpolate(string)(context) : string;
},
- getPlural: function (n, string, stringPlural) {
+ getPlural: function (n, string, stringPlural, context) {
var form = gettextPlurals(this.currentLanguage, n);
- return this.getStringForm(string, form) || prefixDebug(n === 1 ? string : stringPlural);
+ string = this.getStringForm(string, form) || prefixDebug(n === 1 ? string : stringPlural);
+ return context ? $interpolate(string)(context) : string;
},
loadRemote: function (url) {
return $http({
View
2 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",function(a,b,c){var d,e=function(a){return d.debug&&d.currentLanguage!==d.baseLanguage?"[MISSING]: "+a:a};return d={debug:!1,strings:{},baseLanguage:"en",currentLanguage:"en",cache:c("strings"),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}},getStringForm:function(a,b){var c=this.strings[this.currentLanguage]||{},d=c[a]||[];return d[b]},getString:function(a){return this.getStringForm(a,0)||e(a)},getPlural:function(b,c,d){var f=a(this.currentLanguage,b);return this.getStringForm(c,f)||e(1===b?c:d)},loadRemote:function(a){return b({method:"GET",url:a,cache:d.cache}).success(function(a){for(var b in a)d.setStrings(b,a[b])})}}}]),angular.module("gettext").directive("translate",["gettextCatalog","$interpolate","$parse","$compile",function(a,b,c,d){var e=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{transclude:"element",priority:499,compile:function(f,g,h){return function(f,i){var j=function(a,b,c){if(!a)throw new Error("You should add a "+b+" attribute whenever you add a "+c+" attribute.")};if(j(!g.translatePlural||g.translateN,"translate-n","translate-plural"),j(!g.translateN||g.translatePlural,"translate-plural","translate-n"),g.ngIf)throw new Error("You should not combine translate with ng-if, this will lead to problems.");if(g.ngSwitchWhen)throw new Error("You should not combine translate with ng-switch-when, this will lead to problems.");var k=c(g.translateN),l=null;h(f,function(c){var h=e(c.html());return c.removeAttr("translate"),i.replaceWith(c),f.$watch(function(){var e,i=c.html();g.translatePlural?(f=l||(l=f.$new()),f.$count=k(f),e=a.getPlural(f.$count,h,g.translatePlural)):e=a.getString(h);var j=b(e)(f);return i!==j?(c.html(j),void 0!==g.translateCompile&&d(c.contents())(f),c):void 0})})}}}}]),angular.module("gettext").filter("translate",["gettextCatalog",function(a){return function(b){return a.getString(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}}});
+angular.module("gettext",[]),angular.module("gettext").constant("gettext",function(a){return a}),angular.module("gettext").factory("gettextCatalog",["gettextPlurals","$http","$cacheFactory","$interpolate",function(a,b,c,d){var e,f=function(a){return e.debug&&e.currentLanguage!==e.baseLanguage?"[MISSING]: "+a:a};return e={debug:!1,strings:{},baseLanguage:"en",currentLanguage:"en",cache:c("strings"),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}},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)||f(a),b?d(a)(b):a},getPlural:function(b,c,e,g){var h=a(this.currentLanguage,b);return c=this.getStringForm(c,h)||f(1===b?c:e),g?d(c)(g):c},loadRemote:function(a){return b({method:"GET",url:a,cache:e.cache}).success(function(a){for(var b in a)e.setStrings(b,a[b])})}}}]),angular.module("gettext").directive("translate",["gettextCatalog","$interpolate","$parse","$compile",function(a,b,c,d){var e=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{transclude:"element",priority:499,compile:function(f,g,h){return function(f,i){var j=function(a,b,c){if(!a)throw new Error("You should add a "+b+" attribute whenever you add a "+c+" attribute.")};if(j(!g.translatePlural||g.translateN,"translate-n","translate-plural"),j(!g.translateN||g.translatePlural,"translate-plural","translate-n"),g.ngIf)throw new Error("You should not combine translate with ng-if, this will lead to problems.");if(g.ngSwitchWhen)throw new Error("You should not combine translate with ng-switch-when, this will lead to problems.");var k=c(g.translateN),l=null;h(f,function(c){var h=e(c.html());return c.removeAttr("translate"),i.replaceWith(c),f.$watch(function(){var e,i=c.html();g.translatePlural?(f=l||(l=f.$new()),f.$count=k(f),e=a.getPlural(f.$count,h,g.translatePlural)):e=a.getString(h);var j=b(e)(f);return i!==j?(c.html(j),void 0!==g.translateCompile&&d(c.contents())(f),c):void 0})})}}}}]),angular.module("gettext").filter("translate",["gettextCatalog",function(a){return function(b){return a.getString(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}}});
View
12 src/catalog.js
@@ -1,4 +1,4 @@
-angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $http, $cacheFactory) {
+angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $http, $cacheFactory, $interpolate) {
var catalog;
var prefixDebug = function (string) {
@@ -37,13 +37,15 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
return plurals[n];
},
- getString: function (string) {
- return this.getStringForm(string, 0) || prefixDebug(string);
+ getString: function (string, context) {
+ string = this.getStringForm(string, 0) || prefixDebug(string);
+ return context ? $interpolate(string)(context) : string;
},
- getPlural: function (n, string, stringPlural) {
+ getPlural: function (n, string, stringPlural, context) {
var form = gettextPlurals(this.currentLanguage, n);
- return this.getStringForm(string, form) || prefixDebug((n === 1 ? string : stringPlural));
+ string = this.getStringForm(string, form) || prefixDebug(n === 1 ? string : stringPlural);
+ return context ? $interpolate(string)(context) : string;
},
loadRemote: function (url) {
View
16 test/unit/catalog.coffee
@@ -74,3 +74,19 @@ describe 'Catalog', ->
catalog.debug = true
catalog.currentLanguage = 'nl'
assert.equal(catalog.getPlural(2, 'Bird', 'Birds'), '[MISSING]: Birds')
+
+ it 'Can return an interpolated string', ->
+ strings = { 'Hello {{name}}!': 'Hallo {{name}}!' }
+ assert.deepEqual(catalog.strings, {})
+ catalog.currentLanguage = 'nl'
+ catalog.setStrings('nl', strings)
+ assert.equal(catalog.getString('Hello {{name}}!', {name: 'Andrew'}), 'Hallo Andrew!')
+
+ it 'Can return an interpolated plural string', ->
+ assert.deepEqual(catalog.strings, {})
+ catalog.currentLanguage = 'gb'
+ catalog.setStrings('gb', {
+ 'There is {{count}} bird': [ 'There is {{count}} bird', 'There are {{count}} birds' ]
+ })
+ assert.equal(catalog.getPlural(2, 'There is {{count}} bird', 'There are {{count}} birds', { count: 2}), 'There are 2 birds')
+ assert.equal(catalog.getPlural(1, 'There is {{count}} bird', 'There are {{count}} birds', { count: 1}), 'There is 1 bird')

0 comments on commit 6acc718

Please sign in to comment.