From fb9ac6fd6ea650fe0c5e641d9ad1d2344b0ad024 Mon Sep 17 00:00:00 2001 From: neverblde Date: Thu, 11 Apr 2024 20:21:35 -0700 Subject: [PATCH] Update with comments and type safety --- src/locales/en/menu.ts | 9 +++++++-- src/locales/it/menu.ts | 8 ++++++++ src/phases.ts | 4 ++-- src/plugins/i18n.ts | 35 ++++++++++++++++++++++++++++++----- 4 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 src/locales/it/menu.ts diff --git a/src/locales/en/menu.ts b/src/locales/en/menu.ts index 1525f2f9068..1cdafac5d71 100644 --- a/src/locales/en/menu.ts +++ b/src/locales/en/menu.ts @@ -1,8 +1,13 @@ +/** + * The menu namespace holds most miscellaneous text that isn't directly part of the game's + * contents or directly related to Pokemon. This includes menu navigation, settings, + * account interactions, etc. + */ export const menu = { "cancel": "Cancel", "continue": "Continue", - "newGame": "New Game", - "loadGame": "Load Game", "dailyRun": "Daily Run (Beta)", + "loadGame": "Load Game", + "newGame": "New Game", "selectGameMode": "Select a game mode." } as const; \ No newline at end of file diff --git a/src/locales/it/menu.ts b/src/locales/it/menu.ts new file mode 100644 index 00000000000..6c6b6ba468c --- /dev/null +++ b/src/locales/it/menu.ts @@ -0,0 +1,8 @@ +export const menu = { + "cancel": "Annulla", + "continue": "Continua", + "newGame": "Nuova Partita", + "loadGame": "Carica Partita", + "dailyRun": "Corsa Giornaliera (Beta)", + "selectGameMode": "Seleziona una modalità di gioco." +} as const; \ No newline at end of file diff --git a/src/phases.ts b/src/phases.ts index eb30c04d216..09d0de7f686 100644 --- a/src/phases.ts +++ b/src/phases.ts @@ -57,7 +57,7 @@ import { SaveSlotUiMode } from "./ui/save-slot-select-ui-handler"; import { fetchDailyRunSeed, getDailyRunStarters } from "./data/daily-run"; import { GameModes, gameModes } from "./game-mode"; import { getPokemonSpecies, speciesStarters } from "./data/pokemon-species"; -import { default as i18next, menuNS }from './plugins/i18n'; +import i18next from './plugins/i18n'; export class LoginPhase extends Phase { private showText: boolean; @@ -174,7 +174,7 @@ export class TitlePhase extends Phase { const options: OptionSelectItem[] = []; if (loggedInUser.lastSessionSlot > -1) { options.push({ - label: i18next.t('continue', {ns: menuNS}), + label: i18next.t('menu:continue'), handler: () => this.loadSaveSlot(this.lastSessionData ? -1 : loggedInUser.lastSessionSlot) }); } diff --git a/src/plugins/i18n.ts b/src/plugins/i18n.ts index 3d2f6da45d3..95ba54007e1 100644 --- a/src/plugins/i18n.ts +++ b/src/plugins/i18n.ts @@ -1,17 +1,42 @@ import i18next from 'i18next'; import { menu as enMenu } from '../locales/en/menu'; +import { menu as itMenu } from '../locales/it/menu'; -export const menuNS = 'menu'; +const DEFAULT_LANGUAGE_OVERRIDE = ''; + +/** + * i18next is a localization library for maintaining and using translation resources. + * + * Q: How do I add a new language? + * A: To add a new language, create a new folder in the locales directory with the language code. + * Each language folder should contain a file for each namespace (ex. menu.ts) with the translations. + * + * Q: How do I add a new namespace? + * A: To add a new namespace, create a new file in each language folder with the translations. + * Then update the `resources` field in the init() call and the CustomTypeOptions interface. + */ i18next.init({ - lng: 'en', // Default language - fallbackLng: 'en', // Fallback language - debug: true, // Enable debug mode (optional) + lng: DEFAULT_LANGUAGE_OVERRIDE ? DEFAULT_LANGUAGE_OVERRIDE : 'en', + fallbackLng: 'en', + debug: true, resources: { en: { menu: enMenu, }, + it: { + menu: itMenu, + } }, }); -export default i18next; \ No newline at end of file +// Module declared to make referencing keys in the localization files type-safe. +declare module 'i18next' { + interface CustomTypeOptions { + resources: { + menu: typeof enMenu; + }; + } +} + +export default i18next;