Skip to content

Commit 5e6cb34

Browse files
committed
Update contexts support.
1 parent 846116a commit 5e6cb34

6 files changed

Lines changed: 98 additions & 142 deletions

File tree

dist/angular-gettext.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ angular.module('gettext').constant('gettext', function (str) {
1212

1313
angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http", "$cacheFactory", "$interpolate", "$rootScope", function (gettextPlurals, $http, $cacheFactory, $interpolate, $rootScope) {
1414
var catalog;
15+
var noContext = '$$noContext';
1516

1617
var prefixDebug = function (string) {
1718
if (catalog.debug && catalog.currentLanguage !== catalog.baseLanguage) {
@@ -56,30 +57,29 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
5657

5758
for (var key in strings) {
5859
var val = strings[key];
59-
if (!angular.isArray(val)) {
60-
this.strings[language][key] = [val];
61-
} else {
62-
this.strings[language][key] = val;
60+
if (angular.isString(val) || angular.isArray(val)) {
61+
// No context, wrap it in $$noContext.
62+
var obj = {};
63+
obj[noContext] = val;
64+
val = obj;
6365
}
66+
67+
// Expand single strings for each context.
68+
for (var context in val) {
69+
var str = val[context];
70+
val[context] = angular.isArray(str) ? str : [str];
71+
}
72+
this.strings[language][key] = val;
6473
}
6574

6675
broadcastUpdated();
6776
},
6877

6978
getStringForm: function (string, n, context) {
7079
var stringTable = this.strings[this.currentLanguage] || {};
71-
var plurals = stringTable[string] || [];
72-
var translation;
73-
74-
// Translation is an object with context bound translations for the string
75-
if (angular.isObject(plurals[0])){
76-
plurals = (plurals[0][context] || []);
77-
if (!angular.isArray(plurals)){
78-
throw new Error('Context bound translations must be wrapped in a array');
79-
}
80-
}
81-
translation = plurals[n];
82-
return translation;
80+
var contexts = stringTable[string] || {};
81+
var plurals = contexts[context || noContext] || [];
82+
return plurals[n];
8383
},
8484

8585
getString: function (string, scope, context) {
@@ -91,7 +91,10 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
9191
getPlural: function (n, string, stringPlural, scope, context) {
9292
var form = gettextPlurals(this.currentLanguage, n);
9393
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
94-
string = scope ? $interpolate(string)(scope) : string;
94+
if (scope) {
95+
scope.$count = n;
96+
string = $interpolate(string)(scope);
97+
}
9598
return addTranslatedMarkers(string);
9699
},
97100

dist/angular-gettext.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/catalog.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $http, $cacheFactory, $interpolate, $rootScope) {
22
var catalog;
3+
var noContext = '$$noContext';
34

45
var prefixDebug = function (string) {
56
if (catalog.debug && catalog.currentLanguage !== catalog.baseLanguage) {
@@ -44,30 +45,29 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
4445

4546
for (var key in strings) {
4647
var val = strings[key];
47-
if (!angular.isArray(val)) {
48-
this.strings[language][key] = [val];
49-
} else {
50-
this.strings[language][key] = val;
48+
if (angular.isString(val) || angular.isArray(val)) {
49+
// No context, wrap it in $$noContext.
50+
var obj = {};
51+
obj[noContext] = val;
52+
val = obj;
5153
}
54+
55+
// Expand single strings for each context.
56+
for (var context in val) {
57+
var str = val[context];
58+
val[context] = angular.isArray(str) ? str : [str];
59+
}
60+
this.strings[language][key] = val;
5261
}
5362

5463
broadcastUpdated();
5564
},
5665

5766
getStringForm: function (string, n, context) {
5867
var stringTable = this.strings[this.currentLanguage] || {};
59-
var plurals = stringTable[string] || [];
60-
var translation;
61-
62-
// Translation is an object with context bound translations for the string
63-
if (angular.isObject(plurals[0])){
64-
plurals = (plurals[0][context] || []);
65-
if (!angular.isArray(plurals)){
66-
throw new Error('Context bound translations must be wrapped in a array');
67-
}
68-
}
69-
translation = plurals[n];
70-
return translation;
68+
var contexts = stringTable[string] || {};
69+
var plurals = contexts[context || noContext] || [];
70+
return plurals[n];
7171
},
7272

7373
getString: function (string, scope, context) {
@@ -79,7 +79,10 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
7979
getPlural: function (n, string, stringPlural, scope, context) {
8080
var form = gettextPlurals(this.currentLanguage, n);
8181
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
82-
string = scope ? $interpolate(string)(scope) : string;
82+
if (scope) {
83+
scope.$count = n;
84+
string = $interpolate(string)(scope);
85+
}
8386
return addTranslatedMarkers(string);
8487
},
8588

0 commit comments

Comments
 (0)