Case-insensitive lang search for i8n, along with support for match just lang, without region, ie. 'en' == 'en-US'

This commit is contained in:
simojenki
2021-08-16 10:50:56 +10:00
parent c67f74bf08
commit 2cfd52415c
5 changed files with 114 additions and 68 deletions

View File

@@ -54,79 +54,129 @@ describe("i8n", () => {
describe("fetching translations", () => {
describe("with a single lang", () => {
describe("and there is no templating", () => {
it("should return the value", () => {
expect(i8n("foo")("en-US")("artists")).toEqual("Artists");
expect(i8n("foo")("nl-NL")("artists")).toEqual("Artiesten");
describe("and the lang is not represented", () => {
describe("and there is no templating", () => {
it("should return the en-US value", () => {
expect(i8n("foo")("en-AU" as LANG)("artists")).toEqual("Artists");
});
});
describe("and there is templating of the service name", () => {
it("should return the en-US value templated", () => {
expect(i8n("service123")("en-AU" as LANG)("AppLinkMessage")).toEqual(
"Linking sonos with service123"
);
});
});
});
describe("and there is templating of the service name", () => {
it("should return the value", () => {
expect(i8n("service123")("en-US")("AppLinkMessage")).toEqual(
"Linking sonos with service123"
);
expect(i8n("service456")("nl-NL")("AppLinkMessage")).toEqual(
"Sonos koppelen aan service456"
);
describe("and the lang is represented", () => {
describe("and there is no templating", () => {
it("should return the value", () => {
expect(i8n("foo")("en-US")("artists")).toEqual("Artists");
expect(i8n("foo")("nl-NL")("artists")).toEqual("Artiesten");
});
});
describe("and there is templating of the service name", () => {
it("should return the value", () => {
expect(i8n("service123")("en-US")("AppLinkMessage")).toEqual(
"Linking sonos with service123"
);
expect(i8n("service456")("nl-NL")("AppLinkMessage")).toEqual(
"Sonos koppelen aan service456"
);
});
});
});
});
describe("with multiple langs", () => {
describe("and the first lang is a match", () => {
function itShouldReturn(serviceName: string, langs: string[], key: KEY, expected: string) {
it(`should return '${expected}' for the serviceName=${serviceName}, langs=${langs}`, () => {
expect(i8n(serviceName)(...langs)(key)).toEqual(expected);
});
};
describe("and the first lang is an exact match", () => {
describe("and there is no templating", () => {
it("should return the value for the first lang", () => {
expect(i8n("foo")("en-US", "nl-NL")("artists")).toEqual("Artists");
expect(i8n("foo")("nl-NL", "en-US")("artists")).toEqual("Artiesten");
});
itShouldReturn("foo", ["en-US", "nl-NL"], "artists", "Artists");
itShouldReturn("foo", ["nl-NL", "en-US"], "artists", "Artiesten");
});
describe("and there is templating of the service name", () => {
it("should return the value for the firt lang", () => {
expect(i8n("service123")("en-US", "nl-NL")("AppLinkMessage")).toEqual(
"Linking sonos with service123"
);
expect(i8n("service456")("nl-NL", "en-US")("AppLinkMessage")).toEqual(
"Sonos koppelen aan service456"
);
});
itShouldReturn("service123", ["en-US", "nl-NL"], "AppLinkMessage", "Linking sonos with service123");
itShouldReturn("service456", ["nl-NL", "en-US"], "AppLinkMessage", "Sonos koppelen aan service456");
});
});
describe("and the first lang is not a match, however there is a match in the provided langs", () => {
describe("and the first lang is a case insensitive match", () => {
describe("and there is no templating", () => {
it("should return the value for the first lang", () => {
expect(i8n("foo")("something", "en-US", "nl-NL")("artists")).toEqual("Artists");
expect(i8n("foo")("something", "nl-NL", "en-US")("artists")).toEqual("Artiesten");
});
itShouldReturn("foo", ["en-us", "nl-NL"], "artists", "Artists");
itShouldReturn("foo", ["nl-nl", "en-US"], "artists", "Artiesten");
});
describe("and there is templating of the service name", () => {
it("should return the value for the firt lang", () => {
expect(i8n("service123")("something", "en-US", "nl-NL")("AppLinkMessage")).toEqual(
"Linking sonos with service123"
);
expect(i8n("service456")("something", "nl-NL", "en-US")("AppLinkMessage")).toEqual(
"Sonos koppelen aan service456"
);
});
itShouldReturn("service123", ["en-us", "nl-NL"], "AppLinkMessage", "Linking sonos with service123");
itShouldReturn("service456", ["nl-nl", "en-US"], "AppLinkMessage", "Sonos koppelen aan service456");
});
});
describe("and the first lang is a lang match without region", () => {
describe("and there is no templating", () => {
itShouldReturn("foo", ["en", "nl-NL"], "artists", "Artists");
itShouldReturn("foo", ["nl", "en-US"], "artists", "Artiesten");
});
describe("and there is templating of the service name", () => {
itShouldReturn("service123", ["en", "nl-NL"], "AppLinkMessage", "Linking sonos with service123");
itShouldReturn("service456", ["nl", "en-US"], "AppLinkMessage", "Sonos koppelen aan service456");
});
});
describe("and the first lang is not a match, however there is an exact match in the provided langs", () => {
describe("and there is no templating", () => {
itShouldReturn("foo", ["something", "en-US", "nl-NL"], "artists", "Artists")
itShouldReturn("foo", ["something", "nl-NL", "en-US"], "artists", "Artiesten")
});
describe("and there is templating of the service name", () => {
itShouldReturn("service123", ["something", "en-US", "nl-NL"], "AppLinkMessage", "Linking sonos with service123")
itShouldReturn("service456", ["something", "nl-NL", "en-US"], "AppLinkMessage", "Sonos koppelen aan service456")
});
});
describe("and the first lang is not a match, however there is a case insensitive match in the provided langs", () => {
describe("and there is no templating", () => {
itShouldReturn("foo", ["something", "en-us", "nl-nl"], "artists", "Artists")
itShouldReturn("foo", ["something", "nl-nl", "en-us"], "artists", "Artiesten")
});
describe("and there is templating of the service name", () => {
itShouldReturn("service123", ["something", "en-us", "nl-nl"], "AppLinkMessage", "Linking sonos with service123")
itShouldReturn("service456", ["something", "nl-nl", "en-us"], "AppLinkMessage", "Sonos koppelen aan service456")
});
});
describe("and the first lang is not a match, however there is a lang match without region", () => {
describe("and there is no templating", () => {
itShouldReturn("foo", ["something", "en", "nl-nl"], "artists", "Artists")
itShouldReturn("foo", ["something", "nl", "en-us"], "artists", "Artiesten")
});
describe("and there is templating of the service name", () => {
itShouldReturn("service123", ["something", "en", "nl-nl"], "AppLinkMessage", "Linking sonos with service123")
itShouldReturn("service456", ["something", "nl", "en-us"], "AppLinkMessage", "Sonos koppelen aan service456")
});
});
describe("and no lang is a match", () => {
describe("and there is no templating", () => {
it("should return the value for the first lang", () => {
expect(i8n("foo")("something", "something2")("artists")).toEqual("Artists");
});
itShouldReturn("foo", ["something", "something2"], "artists", "Artists")
});
describe("and there is templating of the service name", () => {
it("should return the value for the firt lang", () => {
expect(i8n("service123")("something", "something2")("AppLinkMessage")).toEqual(
"Linking sonos with service123"
);
});
itShouldReturn("service123", ["something", "something2"], "AppLinkMessage", "Linking sonos with service123")
});
});
});
@@ -139,20 +189,5 @@ describe("i8n", () => {
});
});
describe("when the lang is not represented", () => {
describe("and there is no templating", () => {
it("should return the en-US value", () => {
expect(i8n("foo")("en-AU" as LANG)("artists")).toEqual("Artists");
});
});
describe("and there is templating of the service name", () => {
it("should return the en-US value templated", () => {
expect(i8n("service123")("en-AU" as LANG)("AppLinkMessage")).toEqual(
"Linking sonos with service123"
);
});
});
});
});
});