js i18n now accepts a count

This commit is contained in:
Dan Hansen 2011-10-25 20:31:58 -05:00
parent 1a521a792a
commit 091db3fd66
2 changed files with 87 additions and 25 deletions

View file

@ -12,8 +12,9 @@
},
t: function(item, views) {
var translatedMessage,
items = item.split(".");
var items = item.split("."),
translatedMessage,
nextNamespace;
while(nextNamespace = items.shift()) {
translatedMessage = (translatedMessage)
@ -25,6 +26,16 @@
}
}
return $.mustache(translatedMessage, views || { });
if(views && typeof views.count !== "undefined") {
if(views.count == 0) { nextNamespace = "zero"; } else
if(views.count == 1) { nextNamespace = "one"; } else
if(views.count <= 3) { nextNamespace = "few"; } else
if(views.count > 3) { nextNamespace = "many"; }
else { nextNamespace = "other"; }
translatedMessage = translatedMessage[nextNamespace];
}
return $.mustache(translatedMessage, views || {});
}
};

View file

@ -6,40 +6,91 @@
describe("Diaspora", function() {
describe("widgets", function() {
describe("i18n", function() {
var locale = {
namespace: {
message: "hey",
template: "{{myVar}}",
otherNamespace: {
message: "hello from another namespace",
otherMessage: {
zero: "none",
one: "just one",
few: "just a few",
many: "way too many",
other: "what?"
}
}
}
};
describe("loadLocale", function() {
it("sets the class's locale variable", function() {
Diaspora.I18n.loadLocale({sup: "hi"});
expect(Diaspora.I18n.locale).toEqual({sup: "hi"});
Diaspora.I18n.loadLocale(locale);
expect(Diaspora.I18n.locale).toEqual(locale);
});
});
describe("t", function() {
var translation;
beforeEach(function() { Diaspora.I18n.loadLocale(locale); });
it("returns the specified translation", function() {
Diaspora.I18n.loadLocale({yo: "sup"}, "en");
var translation = Diaspora.I18n.t("yo");
expect(translation).toEqual("sup");
translation = Diaspora.I18n.t("namespace.message");
expect(translation).toEqual("hey");
});
it("will go through a infinitely deep object", function() {
Diaspora.I18n.loadLocale({
yo: {
hi: {
sup: {
test: "test"
}
}
},
more: {
another: "i hope this spec is green"
}
});
expect(Diaspora.I18n.t("yo.hi.sup.test")).toEqual("test");
expect(Diaspora.I18n.t("more.another")).toEqual("i hope this spec is green");
translation = Diaspora.I18n.t("namespace.otherNamespace.message");
expect(translation).toEqual("hello from another namespace");
});
it("can render a mustache template", function() {
Diaspora.I18n.loadLocale({yo: "{{yo}}"}, "en");
expect(Diaspora.I18n.t("yo", {yo: "it works!"})).toEqual("it works!");
translation = Diaspora.I18n.t("namespace.template", { myVar: "it works!" });
expect(translation).toEqual("it works!");
});
it("returns an empty string if the translation is not found", function() {
expect(Diaspora.I18n.t("thisstringdoesnotexist")).toEqual("");
expect(Diaspora.I18n.t("missing.locale")).toEqual("");
});
describe("count", function() {
function translateWith(count) {
translation = Diaspora.I18n.t("namespace.otherNamespace.otherMessage", {
count: count
})
}
it("returns the 'zero' namespace if the count is zero", function() {
translateWith(0);
expect(translation).toEqual("none");
});
it("returns the 'one' namespace if the count is one", function() {
translateWith(1);
expect(translation).toEqual("just one");
});
it("returns the 'few' namespace if the count is 2 or 3", function() {
translateWith(2);
expect(translation).toEqual("just a few");
translateWith(3);
expect(translation).toEqual("just a few");
});
it("returns the 'many' namespace for any number greater than 3", function() {
translateWith(50);
expect(translation).toEqual("way too many");
});
});
});
});