add: i18n backend support

the backend is being supported by using msw which will import the correct file from the local locales folder
This commit is contained in:
flx-sta 2024-10-02 20:11:35 -07:00
parent f634b7c044
commit 109e36c6fb
2 changed files with 51 additions and 21 deletions

14
global.d.ts vendored Normal file
View File

@ -0,0 +1,14 @@
import type { SetupServerApi } from "msw/node";
export {};
declare global {
/**
* Only used in testing.
* Can technically be undefined/null but for ease of use we are going to assume it is always defined.
* Used to looad i18n files exclusively.
*
* To set up your own server in a test see `game_data.test.ts`
*/
var i18nServer: SetupServerApi;
}

View File

@ -4,16 +4,17 @@ import { initLoggedInUser } from "#app/account";
import { initAbilities } from "#app/data/ability"; import { initAbilities } from "#app/data/ability";
import { initBiomes } from "#app/data/balance/biomes"; import { initBiomes } from "#app/data/balance/biomes";
import { initEggMoves } from "#app/data/balance/egg-moves"; import { initEggMoves } from "#app/data/balance/egg-moves";
import { initPokemonPrevolutions } from "#app/data/balance/pokemon-evolutions";
import { initMoves } from "#app/data/move"; import { initMoves } from "#app/data/move";
import { initMysteryEncounters } from "#app/data/mystery-encounters/mystery-encounters"; import { initMysteryEncounters } from "#app/data/mystery-encounters/mystery-encounters";
import { initPokemonPrevolutions } from "#app/data/balance/pokemon-evolutions";
import { initPokemonForms } from "#app/data/pokemon-forms"; import { initPokemonForms } from "#app/data/pokemon-forms";
import { initSpecies } from "#app/data/pokemon-species"; import { initSpecies } from "#app/data/pokemon-species";
import { initAchievements } from "#app/system/achv"; import { initAchievements } from "#app/system/achv";
import { initVouchers } from "#app/system/voucher"; import { initVouchers } from "#app/system/voucher";
import { initStatsKeys } from "#app/ui/game-stats-ui-handler"; import { initStatsKeys } from "#app/ui/game-stats-ui-handler";
import { beforeAll, vi } from "vitest"; import { afterAll, beforeAll, vi } from "vitest";
/** Set the timezone to UTC for tests. */
process.env.TZ = "UTC"; process.env.TZ = "UTC";
/** Mock the override import to always return default values, ignoring any custom overrides. */ /** Mock the override import to always return default values, ignoring any custom overrides. */
@ -26,26 +27,36 @@ vi.mock("#app/overrides", async (importOriginal) => {
} satisfies typeof import("#app/overrides"); } satisfies typeof import("#app/overrides");
}); });
vi.mock("i18next", () => ({ /**
default: { * This is a hacky way to mock the i18n backend requests (with the help of {@link https://mswjs.io/ | msw}).
use: () => {}, * The reason to put it inside of a mock is to elevate it.
t: (key: string) => key, * This is necessary because how our code is structured.
changeLanguage: () => Promise.resolve(), * Do NOT try to put any of this code into external functions, it won't work as it's elevated during runtime.
init: () => Promise.resolve(), */
resolvedLanguage: "en", vi.mock("i18next", async (importOriginal) => {
exists: () => true, console.log("Mocking i18next");
getDataByLanguage:() => ({ const { setupServer } = await import("msw/node");
en: { const { http, HttpResponse } = await import("msw");
keys: ["foo"]
}, global.i18nServer = setupServer(
}), http.get("/locales/en/*", async (req) => {
services: { const filename = req.params[0];
formatter: {
add: () => {}, try {
const json = await import(`../../public/locales/en/${req.params[0]}`);
console.log("Loaded locale", filename);
return HttpResponse.json(json);
} catch (err) {
console.log(`Failed to load locale ${filename}!`, err);
return HttpResponse.json({});
} }
}, })
}, );
})); global.i18nServer.listen({ onUnhandledRequest: "error" });
console.log("i18n MSW server listening!");
return await importOriginal();
});
initVouchers(); initVouchers();
initAchievements(); initAchievements();
@ -70,3 +81,8 @@ beforeAll(() => {
}, },
}); });
}); });
afterAll(() => {
global.i18nServer.close();
console.log("Closing i18n MSW server!");
});