Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,7 @@ src/**/*.js*
dist/**/*.txt

# Local
example/pi/sdpi-components.js.map
example/pi/sdpi-components.js.map

# OS
.DS_Store
2 changes: 1 addition & 1 deletion assets/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion dist/sdpi-components.js

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions example/pi/sdpi-components.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 71 additions & 23 deletions src/core/__tests__/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,84 @@ describe("i18n", () => {

describe("default language", () => {
afterAll(() => jest.spyOn(window.navigator, "language", "get").mockReturnValue("en-GB"));
afterEach(() => jest.resetModules());

const testCases = [
{
name: "should read window.navigator.language when localized",
navigator: "es-ES",
language: "es",
},
{
name: "should read window.navigator.language when not localized",
navigator: "fr",
language: "fr",
},
{
name: "should fallback to en when empty",
navigator: "",
language: "en",
},
];

testCases.map((testCase) => {
it(testCase.name, async () => {
it("should read window.navigator.language when localized", async () => {
// given.
jest.spyOn(window.navigator, "language", "get").mockReturnValue("es-ES");

// when
const i18n = (await import("../i18n")).default;

// then.
expect(i18n.language).toBe("es");
});

it("should read window.navigator.language when not localized", async () => {
// given.
jest.spyOn(window.navigator, "language", "get").mockReturnValue("fr");

// when
const i18n = (await import("../i18n")).default;

// then.
expect(i18n.language).toBe("fr");
});

it("should fallback to en when empty", async () => {
// given.
jest.spyOn(window.navigator, "language", "get").mockReturnValue("");

// when
const i18n = (await import("../i18n")).default;

// then.
expect(i18n.language).toBe("en");
});

describe("Simplified/Traditional Chinese", () => {
it("should map traditional Chinese zh-TW to zh_TW", async () => {
// given.
jest.spyOn(window.navigator, "language", "get").mockReturnValue("zh-TW");

// when
const i18n = (await import("../i18n")).default;

// then.
expect(i18n.language).toBe("zh_TW");
});

it("should map traditional Chinese zh-Hant to zh_TW", async () => {
// given.
jest.spyOn(window.navigator, "language", "get").mockReturnValue("zh-Hant");

// when
const i18n = (await import("../i18n")).default;

// then.
expect(i18n.language).toBe("zh_TW");
});

it("should map simplified Chinese zh-CN to zh_CN", async () => {
// given.
jest.spyOn(window.navigator, "language", "get").mockReturnValue("zh-CN");

// when
const i18n = (await import("../i18n")).default;

// then.
expect(i18n.language).toBe("zh_CN");
});

it("should map simplified Chinese zh-Hans to zh_CN", async () => {
// given.
jest.spyOn(window.navigator, "language", "get").mockReturnValue(testCase.navigator);
jest.spyOn(window.navigator, "language", "get").mockReturnValue("zh-Hans");

// when
const i18n = (await import("../i18n")).default;
jest.resetModules();

// then.
expect(i18n.language).toBe(testCase.language);
expect(i18n.language).toBe("zh_CN");
});
});
});
Expand Down
12 changes: 12 additions & 0 deletions src/core/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export class Internationalization {
* {@link https://developer.chrome.com/docs/extensions/reference/i18n/#method-getUILanguage}
*/
public getUILanguage(): string {
const language = window.navigator.language;

// Chinese Traditional
if (language === "zh-Hant" || language === "zh-TW") {
return "zh_TW";
}

// Chinese Simplified
if (language === "zh-Hans" || language === "zh-CN") {
return "zh_CN";
}

return window.navigator.language ? window.navigator.language.split("-")[0] : "en";
}
}
Expand Down