i18n.js: Add interface to retrieve entire sections

And make use of that in HelpView
This commit is contained in:
Jonne Haß 2014-08-18 21:05:47 +02:00
parent ec877e3784
commit b582e1f707
3 changed files with 28 additions and 11 deletions

View file

@ -115,7 +115,7 @@ app.views.Help = app.views.StaticContentView.extend({
renderStaticSection: function(section, template, subs) { renderStaticSection: function(section, template, subs) {
this.clearItems(); this.clearItems();
data = $.extend(Diaspora.I18n.locale[section], { className: section }); data = $.extend(Diaspora.I18n.resolve(section), { className: section });
help_section = new app.views.HelpSectionView({ help_section = new app.views.HelpSectionView({
template: template, template: template,
data: data, data: data,

View file

@ -18,7 +18,7 @@ Diaspora.I18n = {
updateLocale: function(locale, data) { updateLocale: function(locale, data) {
locale.data = $.extend(locale.data, data); locale.data = $.extend(locale.data, data);
rule = this.resolve(locale, ['pluralization_rule']); rule = this._resolve(locale, ['pluralization_rule']);
if (rule !== "") { if (rule !== "") {
eval("locale.pluralizationKey = "+rule); eval("locale.pluralizationKey = "+rule);
} }
@ -26,15 +26,15 @@ Diaspora.I18n = {
t: function(item, views) { t: function(item, views) {
var items = item.split("."); var items = item.split(".");
return this.resolve(this.locale, items, views); return this._render(this.locale, item.split("."), views);
}, },
resolve: function(locale, items, views) { resolve: function(item) {
var translatedMessage, nextNamespace, originalItems = items.slice(); return this._resolve(this.locale, item.split("."));
},
if(views && typeof views.count !== "undefined") { _resolve: function(locale, items) {
items.push(locale.pluralizationKey(views.count)); var translatedMessage, nextNamespace, originalItems = items.slice();
}
while(nextNamespace = items.shift()) { while(nextNamespace = items.shift()) {
translatedMessage = (translatedMessage) translatedMessage = (translatedMessage)
@ -45,18 +45,28 @@ Diaspora.I18n = {
if (typeof locale.fallback === "undefined") { if (typeof locale.fallback === "undefined") {
return ""; return "";
} else { } else {
return this.resolve(locale.fallback, originalItems, views); return this._resolve(locale.fallback, originalItems);
} }
} }
} }
return translatedMessage;
},
_render: function(locale, items, views) {
var originalItems = items.slice();
if(views && typeof views.count !== "undefined") {
items.push(locale.pluralizationKey(views.count));
}
try { try {
return _.template(translatedMessage, views || {}); return _.template(this._resolve(locale, items), views || {});
} catch (e) { } catch (e) {
if (typeof locale.fallback === "undefined") { if (typeof locale.fallback === "undefined") {
return ""; return "";
} else { } else {
return this.resolve(locale.fallback, originalItems, views); return this._render(locale.fallback, originalItems, views);
} }
} }
}, },

View file

@ -78,6 +78,13 @@ describe("Diaspora.I18n", function() {
}); });
}); });
describe("::resolve", function() {
it("allows to retrieve entire sections", function() {
Diaspora.I18n.load(locale, "en", {});
expect(Diaspora.I18n.resolve("namespace")).toEqual(locale["namespace"]);
});
});
describe("::reset", function(){ describe("::reset", function(){
it("clears the current locale", function() { it("clears the current locale", function() {
Diaspora.I18n.load(locale, "en", locale); Diaspora.I18n.load(locale, "en", locale);