Skip to content
46 changes: 30 additions & 16 deletions dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ angular.module('gettext').constant('gettext', function (str) {

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

var prefixDebug = function (string) {
if (catalog.debug && catalog.currentLanguage !== catalog.baseLanguage) {
Expand Down Expand Up @@ -56,32 +57,44 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",

for (var key in strings) {
var val = strings[key];
if (typeof val === 'string') {
this.strings[language][key] = [val];
} else {
this.strings[language][key] = val;
if (angular.isString(val) || angular.isArray(val)) {
// No context, wrap it in $$noContext.
var obj = {};
obj[noContext] = val;
val = obj;
}

// Expand single strings for each context.
for (var context in val) {
var str = val[context];
val[context] = angular.isArray(str) ? str : [str];
}
this.strings[language][key] = val;
}

broadcastUpdated();
},

getStringForm: function (string, n) {
getStringForm: function (string, n, context) {
var stringTable = this.strings[this.currentLanguage] || {};
var plurals = stringTable[string] || [];
var contexts = stringTable[string] || {};
var plurals = contexts[context || noContext] || [];
return plurals[n];
},

getString: function (string, context) {
string = this.getStringForm(string, 0) || prefixDebug(string);
string = context ? $interpolate(string)(context) : string;
getString: function (string, scope, context) {
string = this.getStringForm(string, 0, context) || prefixDebug(string);
string = scope ? $interpolate(string)(scope) : string;
return addTranslatedMarkers(string);
},

getPlural: function (n, string, stringPlural, context) {
getPlural: function (n, string, stringPlural, scope, context) {
var form = gettextPlurals(this.currentLanguage, n);
string = this.getStringForm(string, form) || prefixDebug(n === 1 ? string : stringPlural);
string = context ? $interpolate(string)(context) : string;
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
if (scope) {
scope.$count = n;
string = $interpolate(string)(scope);
}
return addTranslatedMarkers(string);
},

Expand Down Expand Up @@ -131,6 +144,7 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$

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

return {
post: function (scope, element, attrs) {
Expand All @@ -143,9 +157,9 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
if (translatePlural) {
scope = pluralScope || (pluralScope = scope.$new());
scope.$count = countFn(scope);
translated = gettextCatalog.getPlural(scope.$count, msgid, translatePlural);
translated = gettextCatalog.getPlural(scope.$count, msgid, translatePlural, null, translateContext);
} else {
translated = gettextCatalog.getString(msgid);
translated = gettextCatalog.getString(msgid, null, translateContext);
}

// Swap in the translation
Expand All @@ -171,8 +185,8 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
}]);

angular.module('gettext').filter('translate', ["gettextCatalog", function (gettextCatalog) {
function filter(input) {
return gettextCatalog.getString(input);
function filter(input, context) {
return gettextCatalog.getString(input, null, context);
}
filter.$stateful = true;
return filter;
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.

37 changes: 25 additions & 12 deletions src/catalog.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $http, $cacheFactory, $interpolate, $rootScope) {
var catalog;
var noContext = '$$noContext';

var prefixDebug = function (string) {
if (catalog.debug && catalog.currentLanguage !== catalog.baseLanguage) {
Expand Down Expand Up @@ -44,32 +45,44 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h

for (var key in strings) {
var val = strings[key];
if (typeof val === 'string') {
this.strings[language][key] = [val];
} else {
this.strings[language][key] = val;
if (angular.isString(val) || angular.isArray(val)) {
// No context, wrap it in $$noContext.
var obj = {};
obj[noContext] = val;
val = obj;
}

// Expand single strings for each context.
for (var context in val) {
var str = val[context];
val[context] = angular.isArray(str) ? str : [str];
}
this.strings[language][key] = val;
}

broadcastUpdated();
},

getStringForm: function (string, n) {
getStringForm: function (string, n, context) {
var stringTable = this.strings[this.currentLanguage] || {};
var plurals = stringTable[string] || [];
var contexts = stringTable[string] || {};
var plurals = contexts[context || noContext] || [];
return plurals[n];
},

getString: function (string, context) {
string = this.getStringForm(string, 0) || prefixDebug(string);
string = context ? $interpolate(string)(context) : string;
getString: function (string, scope, context) {
string = this.getStringForm(string, 0, context) || prefixDebug(string);
string = scope ? $interpolate(string)(scope) : string;
return addTranslatedMarkers(string);
},

getPlural: function (n, string, stringPlural, context) {
getPlural: function (n, string, stringPlural, scope, context) {
var form = gettextPlurals(this.currentLanguage, n);
string = this.getStringForm(string, form) || prefixDebug(n === 1 ? string : stringPlural);
string = context ? $interpolate(string)(context) : string;
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
if (scope) {
scope.$count = n;
string = $interpolate(string)(scope);
}
return addTranslatedMarkers(string);
},

Expand Down
5 changes: 3 additions & 2 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ angular.module('gettext').directive('translate', function (gettextCatalog, $pars

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

return {
post: function (scope, element, attrs) {
Expand All @@ -40,9 +41,9 @@ angular.module('gettext').directive('translate', function (gettextCatalog, $pars
if (translatePlural) {
scope = pluralScope || (pluralScope = scope.$new());
scope.$count = countFn(scope);
translated = gettextCatalog.getPlural(scope.$count, msgid, translatePlural);
translated = gettextCatalog.getPlural(scope.$count, msgid, translatePlural, null, translateContext);
} else {
translated = gettextCatalog.getString(msgid);
translated = gettextCatalog.getString(msgid, null, translateContext);
}

// Swap in the translation
Expand Down
4 changes: 2 additions & 2 deletions src/filter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
angular.module('gettext').filter('translate', function (gettextCatalog) {
function filter(input) {
return gettextCatalog.getString(input);
function filter(input, context) {
return gettextCatalog.getString(input, null, context);
}
filter.$stateful = true;
return filter;
Expand Down
Loading