Add i18next framework to enable further contributions (#96)

* prototype

* Update with comments and type safety
This commit is contained in:
James Lin authored and GitHub committed 2024-04-11 23:47:03 -04:00
1 parent b44ee95610
commit 73c9625df1
6 files changed
+98 -10

No files matched your search

+13
View File
@@ -0,0 +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",
"dailyRun": "Daily Run (Beta)",
"loadGame": "Load Game",
"newGame": "New Game",
"selectGameMode": "Select a game mode."
} as const;
+8
View File
@@ -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;
+7 -6
View File
@@ -57,6 +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 i18next from './plugins/i18n';
export class LoginPhase extends Phase {
private showText: boolean;
@@ -173,12 +174,12 @@ export class TitlePhase extends Phase {
const options: OptionSelectItem[] = [];
if (loggedInUser.lastSessionSlot > -1) {
options.push({
label: 'Continue',
label: i18next.t('menu:continue'),
handler: () => this.loadSaveSlot(this.lastSessionData ? -1 : loggedInUser.lastSessionSlot)
});
}
options.push({
label: 'New Game',
label: i18next.t('menu:newGame'),
handler: () => {
const setModeAndEnd = (gameMode: GameModes) => {
this.gameMode = gameMode;
@@ -204,14 +205,14 @@ export class TitlePhase extends Phase {
});
}
options.push({
label: 'Cancel',
label: i18next.t('menu:cancel'),
handler: () => {
this.scene.clearPhaseQueue();
this.scene.pushPhase(new TitlePhase(this.scene));
super.end();
}
});
this.scene.ui.showText('Select a game mode.', null, () => this.scene.ui.setOverlayMode(Mode.OPTION_SELECT, { options: options }));
this.scene.ui.showText(i18next.t("menu:selectGameMode"), null, () => this.scene.ui.setOverlayMode(Mode.OPTION_SELECT, { options: options }));
} else {
this.gameMode = GameModes.CLASSIC;
this.scene.ui.setMode(Mode.MESSAGE);
@@ -221,7 +222,7 @@ export class TitlePhase extends Phase {
}
},
{
label: 'Load Game',
label: i18next.t('menu:loadGame'),
handler: () => this.scene.ui.setOverlayMode(Mode.SAVE_SLOT, SaveSlotUiMode.LOAD,
(slotId: integer) => {
if (slotId === -1)
@@ -231,7 +232,7 @@ export class TitlePhase extends Phase {
)
},
{
label: 'Daily Run (Beta)',
label: i18next.t('menu:dailyRun'),
handler: () => this.initDailyRun(),
keepOpen: true
});
+42
View File
@@ -0,0 +1,42 @@
import i18next from 'i18next';
import { menu as enMenu } from '../locales/en/menu';
import { menu as itMenu } from '../locales/it/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: DEFAULT_LANGUAGE_OVERRIDE ? DEFAULT_LANGUAGE_OVERRIDE : 'en',
fallbackLng: 'en',
debug: true,
resources: {
en: {
menu: enMenu,
},
it: {
menu: itMenu,
}
},
});
// Module declared to make referencing keys in the localization files type-safe.
declare module 'i18next' {
interface CustomTypeOptions {
resources: {
menu: typeof enMenu;
};
}
}
export default i18next;