discourse/test/javascripts/lib/i18n-test.js.es6

147 lines
3.8 KiB
Plaintext
Raw Normal View History

2017-06-14 13:57:58 -04:00
QUnit.module("lib:i18n", {
_locale: I18n.locale,
_fallbackLocale: I18n.fallbackLocale,
_translations: I18n.translations,
2017-06-14 13:57:58 -04:00
beforeEach() {
I18n.locale = "fr";
I18n.translations = {
2018-06-15 11:03:24 -04:00
fr_FOO: {
js: {
topic: {
reply: {
title: "Foo"
}
2018-06-15 11:03:24 -04:00
}
}
},
2018-06-15 11:03:24 -04:00
fr: {
js: {
hello: "Bonjour",
topic: {
reply: {
title: "Répondre"
},
2018-06-15 11:03:24 -04:00
share: {
title: "Partager"
}
},
2018-06-15 11:03:24 -04:00
character_count: {
zero: "{{count}} ZERO",
one: "{{count}} ONE",
two: "{{count}} TWO",
few: "{{count}} FEW",
many: "{{count}} MANY",
other: "{{count}} OTHER"
}
}
},
2018-06-15 11:03:24 -04:00
en: {
js: {
hello: {
world: "Hello World!",
universe: ""
},
2018-06-15 11:03:24 -04:00
topic: {
reply: {
help: "begin composing a reply to this topic"
}
},
2018-06-15 11:03:24 -04:00
word_count: {
one: "1 word",
other: "{{count}} words"
}
}
}
};
// fake pluralization rules
I18n.pluralizationRules.fr = function(n) {
2017-02-28 04:33:16 -05:00
if (n === 0) return "zero";
if (n === 1) return "one";
if (n === 2) return "two";
2018-06-15 11:03:24 -04:00
if (n >= 3 && n <= 9) return "few";
if (n >= 10 && n <= 99) return "many";
return "other";
2017-02-28 04:33:16 -05:00
};
},
2017-06-14 13:57:58 -04:00
afterEach() {
I18n.locale = this._locale;
I18n.fallbackLocale = this._fallbackLocale;
I18n.translations = this._translations;
}
});
2017-06-14 13:57:58 -04:00
QUnit.test("defaults", assert => {
assert.equal(I18n.defaultLocale, "en", "it has English as default locale");
assert.ok(I18n.pluralizationRules["en"], "it has English pluralizer");
});
2017-06-14 13:57:58 -04:00
QUnit.test("translations", assert => {
2018-06-15 11:03:24 -04:00
assert.equal(
I18n.t("topic.reply.title"),
"Répondre",
"uses locale translations when they exist"
);
assert.equal(
I18n.t("topic.reply.help"),
"begin composing a reply to this topic",
"fallbacks to English translations"
);
assert.equal(
I18n.t("hello.world"),
"Hello World!",
"doesn't break if a key is overriden in a locale"
);
2017-06-14 13:57:58 -04:00
assert.equal(I18n.t("hello.universe"), "", "allows empty strings");
});
2017-06-14 13:57:58 -04:00
QUnit.test("extra translations", assert => {
2018-06-15 11:03:24 -04:00
I18n.extras = [{ admin: { title: "Discourse Admin" } }];
2018-06-15 11:03:24 -04:00
assert.equal(
I18n.t("admin.title"),
"Discourse Admin",
"it check extra translations when they exists"
);
});
2017-06-14 13:57:58 -04:00
QUnit.test("pluralizations", assert => {
assert.equal(I18n.t("character_count", { count: 0 }), "0 ZERO");
assert.equal(I18n.t("character_count", { count: 1 }), "1 ONE");
assert.equal(I18n.t("character_count", { count: 2 }), "2 TWO");
assert.equal(I18n.t("character_count", { count: 3 }), "3 FEW");
assert.equal(I18n.t("character_count", { count: 10 }), "10 MANY");
assert.equal(I18n.t("character_count", { count: 100 }), "100 OTHER");
2017-06-14 13:57:58 -04:00
assert.equal(I18n.t("word_count", { count: 0 }), "0 words");
assert.equal(I18n.t("word_count", { count: 1 }), "1 word");
assert.equal(I18n.t("word_count", { count: 2 }), "2 words");
assert.equal(I18n.t("word_count", { count: 3 }), "3 words");
assert.equal(I18n.t("word_count", { count: 10 }), "10 words");
assert.equal(I18n.t("word_count", { count: 100 }), "100 words");
});
QUnit.test("fallback", assert => {
I18n.locale = "fr_FOO";
I18n.fallbackLocale = "fr";
2018-06-15 11:03:24 -04:00
assert.equal(
I18n.t("topic.reply.title"),
"Foo",
"uses locale translations when they exist"
);
assert.equal(
I18n.t("topic.share.title"),
"Partager",
"falls back to fallbackLocale translations when they exist"
);
assert.equal(
I18n.t("topic.reply.help"),
"begin composing a reply to this topic",
"falls back to English translations"
);
});