js i18n now accepts a count
This commit is contained in:
parent
1a521a792a
commit
091db3fd66
2 changed files with 87 additions and 25 deletions
|
|
@ -12,8 +12,9 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
t: function(item, views) {
|
t: function(item, views) {
|
||||||
var translatedMessage,
|
var items = item.split("."),
|
||||||
items = item.split(".");
|
translatedMessage,
|
||||||
|
nextNamespace;
|
||||||
|
|
||||||
while(nextNamespace = items.shift()) {
|
while(nextNamespace = items.shift()) {
|
||||||
translatedMessage = (translatedMessage)
|
translatedMessage = (translatedMessage)
|
||||||
|
|
@ -25,6 +26,16 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 || {});
|
return $.mustache(translatedMessage, views || {});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -6,40 +6,91 @@
|
||||||
describe("Diaspora", function() {
|
describe("Diaspora", function() {
|
||||||
describe("widgets", function() {
|
describe("widgets", function() {
|
||||||
describe("i18n", 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() {
|
describe("loadLocale", function() {
|
||||||
it("sets the class's locale variable", function() {
|
it("sets the class's locale variable", function() {
|
||||||
Diaspora.I18n.loadLocale({sup: "hi"});
|
Diaspora.I18n.loadLocale(locale);
|
||||||
expect(Diaspora.I18n.locale).toEqual({sup: "hi"});
|
|
||||||
|
expect(Diaspora.I18n.locale).toEqual(locale);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("t", function() {
|
describe("t", function() {
|
||||||
|
var translation;
|
||||||
|
beforeEach(function() { Diaspora.I18n.loadLocale(locale); });
|
||||||
|
|
||||||
it("returns the specified translation", function() {
|
it("returns the specified translation", function() {
|
||||||
Diaspora.I18n.loadLocale({yo: "sup"}, "en");
|
translation = Diaspora.I18n.t("namespace.message");
|
||||||
var translation = Diaspora.I18n.t("yo");
|
|
||||||
expect(translation).toEqual("sup");
|
expect(translation).toEqual("hey");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("will go through a infinitely deep object", function() {
|
it("will go through a infinitely deep object", function() {
|
||||||
Diaspora.I18n.loadLocale({
|
translation = Diaspora.I18n.t("namespace.otherNamespace.message");
|
||||||
yo: {
|
|
||||||
hi: {
|
expect(translation).toEqual("hello from another namespace");
|
||||||
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");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("can render a mustache template", function() {
|
it("can render a mustache template", function() {
|
||||||
Diaspora.I18n.loadLocale({yo: "{{yo}}"}, "en");
|
translation = Diaspora.I18n.t("namespace.template", { myVar: "it works!" });
|
||||||
expect(Diaspora.I18n.t("yo", {yo: "it works!"})).toEqual("it works!");
|
|
||||||
|
expect(translation).toEqual("it works!");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns an empty string if the translation is not found", function() {
|
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");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue