diff --git a/src/web/l10n.mjs b/src/web/l10n.mjs index f286ff0..954ea5a 100644 --- a/src/web/l10n.mjs +++ b/src/web/l10n.mjs @@ -33,7 +33,7 @@ export class L10n { } constructor(language) { - this.language = language || "en-US"; + this.language = language || "en"; this.ready = this.load().then(() => true); } @@ -42,7 +42,7 @@ export class L10n { const [locale, fallbackLocale, defaultLocale] = await Promise.all([ L10n.loadLocale(this.language), L10n.loadLocale(this.language.split("-")[0]), - L10n.loadLocale("en-US"), + L10n.loadLocale("en"), ]); this.locale = locale; this.fallbackLocale = fallbackLocale; diff --git a/tests/fixtures/locales/en.json b/tests/fixtures/locales/en.json new file mode 100644 index 0000000..36373c4 --- /dev/null +++ b/tests/fixtures/locales/en.json @@ -0,0 +1,7 @@ +{ + "effectiveMessage": "Message with effective content", + "blankMessage": "Blank message", + "messageWithPlaceholders": "Message with placeholders: ${one}, ${two}", + "missingFallbackMessage": "Message defined in fallback locales", + "missingMessage": "Message not defined in non-default locales" +} diff --git a/tests/fixtures/locales/ja-JP.json b/tests/fixtures/locales/ja-JP.json new file mode 100644 index 0000000..b790273 --- /dev/null +++ b/tests/fixtures/locales/ja-JP.json @@ -0,0 +1,7 @@ +{ + "effectiveMessage": "JP:意味ある内容を含むメッセージ", + "blankMessage": "", + "messageWithPlaceholders": "JP:プレースホルダーを含むメッセージ:${one}, ${two}, ${three}", + "_missingFallbackMessage": "JP:フォールバック先で定義されているメッセージ", + "_missingMessage": "JP:未定義のメッセージ" +} diff --git a/tests/fixtures/locales/ja.json b/tests/fixtures/locales/ja.json new file mode 100644 index 0000000..a5f9cbc --- /dev/null +++ b/tests/fixtures/locales/ja.json @@ -0,0 +1,7 @@ +{ + "effectiveMessage": "意味ある内容を含むメッセージ", + "blankMessage": "空のメッセージ", + "messageWithPlaceholders": "プレースホルダーを含むメッセージ:${one}, ${two}", + "missingFallbackMessage": "フォールバック先で定義されているメッセージ", + "_missingMessage": "未定義のメッセージ" +} diff --git a/tests/unit/test-l10n.mjs b/tests/unit/test-l10n.mjs new file mode 100644 index 0000000..4fb0bb6 --- /dev/null +++ b/tests/unit/test-l10n.mjs @@ -0,0 +1,50 @@ +/* +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ +'use strict'; + +import "./l10n.mjs"; +import { L10n } from "../../src/web/l10n.mjs"; +import { assert } from "tiny-esm-test-runner"; +const { is } = assert; + +let l10n; + +export async function setUp() { + L10n.clearCache(); + L10n.baseUrl = (new URL(`${import.meta.url}/../../fixtures/`)).toString(); + l10n = new L10n("ja-JP"); + await l10n.ready; +} + +test_get.parameters = { + effective: { + key: "effectiveMessage", + expected: "JP:意味ある内容を含むメッセージ", + }, + blank: { + key: "blankMessage", + expected: "", + }, + withPlaceholders: { + key: "messageWithPlaceholders", + params: { + one: "One", + two: "Two", + }, + expected: "JP:プレースホルダーを含むメッセージ:One, Two, ${three}", + }, + fallbackToGeneralLocale: { + key: "missingFallbackMessage", + expected: "フォールバック先で定義されているメッセージ", + }, + fallbackToDefaultLocale: { + key: "missingMessage", + expected: "Message not defined in non-default locales", + }, +}; +export function test_get({ key, params, expected }) { + is(expected, l10n.get(key, params || null)); +}