crowdfree
Version:
A crowdin compatible tool for translation and localisation of websites and applications
91 lines (88 loc) • 4.08 kB
JavaScript
const assert = require("assert")
const fs = require("fs/promises")
const Crowdfree = require("..")
beforeEach(async () => {
// Set up testdata
await fs.mkdir("./testdata/locale/en", {
recursive: true,
})
await fs.mkdir("./testdata/locale/no", {
recursive: true,
})
await fs.writeFile("./testdata/locale/no/locale.json", JSON.stringify({
description: "Enkel oversetting uten rot",
name: "Gruppefri",
with_template: "Norsk tekst med %{template} :)",
template_pluralized: "%{smart_count} epple |||| %{smart_count} eppler"
}, null, 4))
await fs.writeFile("./testdata/locale/en/locale.json", JSON.stringify({
description: "Simple localisations without all the fuss",
name: "Crowdfree",
with_template: "English text with %{template} :)",
template_pluralized: "%{smart_count} apple |||| %{smart_count} apples"
}, null, 4))
})
afterEach(async () => {
// Clean up testdata
await fs.rm("./testdata", {
recursive: true,
})
})
describe("Nodejs library", () => {
it("Exports a class", () => {
assert.deepStrictEqual(typeof Crowdfree, "function")
})
describe("Loading locale", () => {
it("loads files from a locale directory", async () => {
let cf = new Crowdfree()
await cf.load("./", "testdata")
assert.deepStrictEqual(cf.translations.length, 4)
})
it("returns strings of the chosen locale", async () => {
let cf = new Crowdfree()
await cf.load("./", "testdata")
cf.setLanguage("no")
assert.deepStrictEqual(cf.t("description"), "Enkel oversetting uten rot")
cf.setLanguage("en")
assert.deepStrictEqual(cf.t("description"), "Simple localisations without all the fuss")
})
it("defaults to returning strings in english", async () => {
let cf = new Crowdfree()
await cf.load("./", "testdata")
assert.deepStrictEqual(cf.t("description"), "Simple localisations without all the fuss")
})
it("returns strings in the first language it can find if there is no english locale", async () => {
await fs.rm("./testdata/locale/en", { recursive: true })
let cf = new Crowdfree()
await cf.load("./", "testdata")
assert.deepStrictEqual(cf.t("description"), "Enkel oversetting uten rot")
})
})
describe("Translates strings", () => {
var cf
beforeEach(async () => {
cf = new Crowdfree()
await cf.load("./", "testdata")
})
describe("Using templating", () => {
it("replaces template with value", async () => {
let text = cf.t("with_template", {
template: "THIS IS THE TEMPLATED TEXT"
})
assert.deepStrictEqual(text, "English text with THIS IS THE TEMPLATED TEXT :)")
})
it("allows it to handle pluralization", async () => {
cf.setLanguage("en")
assert.deepStrictEqual(cf.t("template_pluralized", { smart_count: 0 }), "0 apples")
assert.deepStrictEqual(cf.t("template_pluralized", { smart_count: 1 }), "1 apple")
assert.deepStrictEqual(cf.t("template_pluralized", { smart_count: 2 }), "2 apples")
assert.deepStrictEqual(cf.t("template_pluralized", { smart_count: 100 }), "100 apples")
cf.setLanguage("no")
assert.deepStrictEqual(cf.t("template_pluralized", { smart_count: 0 }), "0 eppler")
assert.deepStrictEqual(cf.t("template_pluralized", { smart_count: 1 }), "1 epple")
assert.deepStrictEqual(cf.t("template_pluralized", { smart_count: 2 }), "2 eppler")
assert.deepStrictEqual(cf.t("template_pluralized", { smart_count: 100 }), "100 eppler")
})
})
})
})