clear locale on each spec run, fix indentation

This commit is contained in:
Florian Staudacher 2014-02-23 17:13:01 +01:00
parent 1f98e1c639
commit a2aff720a2
2 changed files with 86 additions and 63 deletions

View file

@ -2,20 +2,21 @@
* licensed under the Affero General Public License version 3 or later. See * licensed under the Affero General Public License version 3 or later. See
* the COPYRIGHT file. * the COPYRIGHT file.
*/ */
Diaspora.I18n = {
language: "en",
locale: {},
loadLocale: function(locale, language) { Diaspora.I18n = {
this.locale = $.extend(this.locale, locale); language: "en",
this.language = language; locale: {},
rule = this.t('pluralization_rule');
if (rule === "")
rule = 'function (n) { return n == 1 ? "one" : "other" }';
eval("this.pluralizationKey = "+rule);
},
t: function(item, views) { loadLocale: function(locale, language) {
this.locale = $.extend(this.locale, locale);
this.language = language;
rule = this.t('pluralization_rule');
if (rule === "")
rule = 'function (n) { return n == 1 ? "one" : "other" }';
eval("this.pluralizationKey = "+rule);
},
t: function(item, views) {
var items = item.split("."), var items = item.split("."),
translatedMessage, translatedMessage,
nextNamespace; nextNamespace;
@ -35,5 +36,12 @@
} }
return _.template(translatedMessage, views || {}); return _.template(translatedMessage, views || {});
} },
};
reset: function() {
this.locale = {};
if( arguments.length > 0 && !(_.isEmpty(arguments[0])) )
this.locale = arguments[0];
}
};

View file

@ -3,69 +3,84 @@
* the COPYRIGHT file. * the COPYRIGHT file.
*/ */
describe("Diaspora", function() { describe("Diaspora.I18n", function() {
describe("widgets", function() { var locale = {namespace: {
describe("i18n", function() { message: "hey",
var locale = {namespace: { template: "<%= myVar %>",
message: "hey", otherNamespace: {
template: "<%= myVar %>", message: "hello from another namespace",
otherNamespace: { otherMessage: {
message: "hello from another namespace", zero: "none",
otherMessage: { one: "just one",
zero: "none", few: "just a few",
one: "just one", many: "way too many",
few: "just a few", other: "what?"
many: "way too many",
other: "what?"
}
}
} }
}; }
}
};
describe("loadLocale", function() { beforeEach(function(){
it("sets the class's locale variable", function() { Diaspora.I18n.reset(); // run tests with clean locale
Diaspora.I18n.loadLocale(locale); });
expect(Diaspora.I18n.locale).toEqual(locale); describe("::loadLocale", function() {
}); it("sets the class's locale variable", function() {
Diaspora.I18n.loadLocale(locale);
it("extends the class's locale variable on multiple calls", function() { expect(Diaspora.I18n.locale).toEqual(locale);
var data = {another: 'section'}, });
extended = $.extend(locale, data);
Diaspora.I18n.loadLocale(locale); it("extends the class's locale variable on multiple calls", function() {
Diaspora.I18n.loadLocale(data); var data = {another: 'section'},
extended = $.extend(locale, data);
expect(Diaspora.I18n.locale).toEqual(extended); Diaspora.I18n.loadLocale(locale);
}); Diaspora.I18n.loadLocale(data);
});
describe("t", function() { expect(Diaspora.I18n.locale).toEqual(extended);
var translation; });
beforeEach(function() { Diaspora.I18n.loadLocale(locale); }); });
it("returns the specified translation", function() { describe("::t", function() {
translation = Diaspora.I18n.t("namespace.message"); var translation;
beforeEach(function() { Diaspora.I18n.loadLocale(locale); });
expect(translation).toEqual("hey"); it("returns the specified translation", function() {
}); translation = Diaspora.I18n.t("namespace.message");
it("will go through a infinitely deep object", function() { expect(translation).toEqual("hey");
translation = Diaspora.I18n.t("namespace.otherNamespace.message"); });
expect(translation).toEqual("hello from another namespace"); it("will go through a infinitely deep object", function() {
}); translation = Diaspora.I18n.t("namespace.otherNamespace.message");
it("can render a mustache template", function() { expect(translation).toEqual("hello from another namespace");
translation = Diaspora.I18n.t("namespace.template", { myVar: "it works!" }); });
expect(translation).toEqual("it works!"); it("can render a mustache template", function() {
}); translation = Diaspora.I18n.t("namespace.template", { myVar: "it works!" });
it("returns an empty string if the translation is not found", function() { expect(translation).toEqual("it works!");
expect(Diaspora.I18n.t("missing.locale")).toEqual(""); });
});
}); it("returns an empty string if the translation is not found", function() {
expect(Diaspora.I18n.t("missing.locale")).toEqual("");
});
});
describe("::reset", function(){
it("clears the current locale", function() {
Diaspora.I18n.loadLocale(locale);
Diaspora.I18n.reset()
expect(Diaspora.I18n.locale).toEqual({});
});
it("sets the locale to only a specific value", function() {
var data = { some: 'value' };
Diaspora.I18n.loadLocale(locale);
Diaspora.I18n.reset(data);
expect(Diaspora.I18n.locale).toEqual(data);
}); });
}); });
}); });