Skip to content

add namespace support for translated string #241

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

Open
wants to merge 2 commits 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
46 changes: 33 additions & 13 deletions dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
$rootScope.$broadcast('gettextLanguageChanged');
}

function getInternalNamespaceName(namepsace) {
return namepsace ? ('$$_ns_' + namepsace) : namepsace;
}

catalog = {
debug: false,
debugPrefix: '[MISSING]: ',
Expand All @@ -61,11 +65,17 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
return this.currentLanguage;
},

setStrings: function (language, strings) {
setStrings: function (language, strings, namepsace) {
if (!this.strings[language]) {
this.strings[language] = {};
}

namepsace = getInternalNamespaceName(namepsace);

if (namepsace && !this.strings[language][namepsace]) {
this.strings[language][namepsace] = {};
}

for (var key in strings) {
var val = strings[key];

Expand All @@ -86,44 +96,53 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
var str = val[context];
val[context] = angular.isArray(str) ? str : [str];
}
this.strings[language][key] = val;

if (namepsace) {
this.strings[language][namepsace][key] = val;
} else {
this.strings[language][key] = val;
}
}

broadcastUpdated();
},

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

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

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

loadRemote: function (url) {
loadRemote: function (url, namepsace) {
return $http({
method: 'GET',
url: url,
cache: catalog.cache
}).then(function (response) {
var data = response.data;
for (var lang in data) {
catalog.setStrings(lang, data[lang]);
catalog.setStrings(lang, data[lang], namepsace);
}
return response;
});
Expand Down Expand Up @@ -166,6 +185,7 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
var msgid = trim(element.html());
var translatePlural = attrs.translatePlural;
var translateContext = attrs.translateContext;
var namespace = attrs.translate;

if (msie <= 8) {
// Workaround fix relating to angular adding a comment node to
Expand All @@ -187,9 +207,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, null, translateContext);
translated = gettextCatalog.getPlural(scope.$count, msgid, translatePlural, null, translateContext, namespace);
} else {
translated = gettextCatalog.getString(msgid, null, translateContext);
translated = gettextCatalog.getString(msgid, null, translateContext, namespace);
}

var oldContents = element.contents();
Expand Down Expand Up @@ -231,8 +251,8 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
}]);

angular.module('gettext').filter('translate', ["gettextCatalog", function (gettextCatalog) {
function filter(input, context) {
return gettextCatalog.getString(input, null, context);
function filter(input, context, namespace) {
return gettextCatalog.getString(input, null, context, namespace);
}
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: 28 additions & 9 deletions src/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
$rootScope.$broadcast('gettextLanguageChanged');
}

function getInternalNamespaceName(namepsace) {
return namepsace ? ('$$_ns_' + namepsace) : namepsace;
}

catalog = {
debug: false,
debugPrefix: '[MISSING]: ',
Expand All @@ -49,11 +53,17 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
return this.currentLanguage;
},

setStrings: function (language, strings) {
setStrings: function (language, strings, namepsace) {
if (!this.strings[language]) {
this.strings[language] = {};
}

namepsace = getInternalNamespaceName(namepsace);

if (namepsace && !this.strings[language][namepsace]) {
this.strings[language][namepsace] = {};
}

for (var key in strings) {
var val = strings[key];

Expand All @@ -74,44 +84,53 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
var str = val[context];
val[context] = angular.isArray(str) ? str : [str];
}
this.strings[language][key] = val;

if (namepsace) {
this.strings[language][namepsace][key] = val;
} else {
this.strings[language][key] = val;
}
}

broadcastUpdated();
},

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

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

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

loadRemote: function (url) {
loadRemote: function (url, namepsace) {
return $http({
method: 'GET',
url: url,
cache: catalog.cache
}).then(function (response) {
var data = response.data;
for (var lang in data) {
catalog.setStrings(lang, data[lang]);
catalog.setStrings(lang, data[lang], namepsace);
}
return response;
});
Expand Down
5 changes: 3 additions & 2 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ angular.module('gettext').directive('translate', function (gettextCatalog, $pars
var msgid = trim(element.html());
var translatePlural = attrs.translatePlural;
var translateContext = attrs.translateContext;
var namespace = attrs.translate;

if (msie <= 8) {
// Workaround fix relating to angular adding a comment node to
Expand All @@ -52,9 +53,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, null, translateContext);
translated = gettextCatalog.getPlural(scope.$count, msgid, translatePlural, null, translateContext, namespace);
} else {
translated = gettextCatalog.getString(msgid, null, translateContext);
translated = gettextCatalog.getString(msgid, null, translateContext, namespace);
}

var oldContents = element.contents();
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, context) {
return gettextCatalog.getString(input, null, context);
function filter(input, context, namespace) {
return gettextCatalog.getString(input, null, context, namespace);
}
filter.$stateful = true;
return filter;
Expand Down