pokerogue/src/plugins/i18n.ts
Jannik Tappert ac2f7755c2
Add the option to localize the Dialogue (#1254)
* Started Dialogue Loc. This is very much WIP

* Done with the trainer dialogue except the rival(s). And the special dialogue...

* Added Rival Dialogue

* Added final boss and ending dialogue

* Added Dialogue to all languages (just copied the english file). And updates the config.ts

* Added chinese splash

* Lint

* Added the new dialogue for the galar elite 4

* The dobule dialogue is now also localizable (also added dialogue localization files at all to chinese TW

* Added german dialouge for the named doubles

* Added Endboss translation for german

* Added rival dialogue in german (and yes i checked that every of them fits)

* Dialogue for trainer classes (As good as possible since the english text at some parts doesnt make a lot of sense)

* Start Gym Leaders

* Finished Kanto Gym Leaders
(Added missing dialogue to all other languages)

* Hoenn Gym Leaders

* Some more

* The Rest

* Marshal was missing something

* Partial French translation to dialogue.ts

* Added gender specific titles for elite 4, gym leaders and champs

* Readded import that was removed by a merge

* The dialogue can now be localized based on the players gender (male or female). unset uses the male dialogue. Can be easily adopted when we add non binary options later

---------

Co-authored-by: Lugiad <adrien.grivel@hotmail.fr>
2024-05-27 06:14:37 -05:00

202 lines
5.6 KiB
TypeScript

import i18next from "i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import { deConfig } from "#app/locales/de/config.js";
import { enConfig } from "#app/locales/en/config.js";
import { esConfig } from "#app/locales/es/config.js";
import { frConfig } from "#app/locales/fr/config.js";
import { itConfig } from "#app/locales/it/config.js";
import { ptBrConfig } from "#app/locales/pt_BR/config.js";
import { zhCnConfig } from "#app/locales/zh_CN/config.js";
import { zhTWConfig } from "#app/locales/zh_TW/config.js";
export interface SimpleTranslationEntries {
[key: string]: string
}
export interface MoveTranslationEntry {
name: string,
effect: string
}
export interface MoveTranslationEntries {
[key: string]: MoveTranslationEntry
}
export interface AbilityTranslationEntry {
name: string,
description: string
}
export interface AbilityTranslationEntries {
[key: string]: AbilityTranslationEntry
}
export interface ModifierTypeTranslationEntry {
name?: string,
description?: string,
extra?: SimpleTranslationEntries
}
export interface ModifierTypeTranslationEntries {
ModifierType: { [key: string]: ModifierTypeTranslationEntry },
AttackTypeBoosterItem: SimpleTranslationEntries,
TempBattleStatBoosterItem: SimpleTranslationEntries,
BaseStatBoosterItem: SimpleTranslationEntries,
EvolutionItem: SimpleTranslationEntries,
FormChangeItem: SimpleTranslationEntries,
}
export interface PokemonInfoTranslationEntries {
Stat: SimpleTranslationEntries,
Type: SimpleTranslationEntries,
}
export interface BerryTranslationEntry {
name: string,
effect: string
}
export interface BerryTranslationEntries {
[key: string]: BerryTranslationEntry
}
export interface DialogueTranslationEntry {
[key: number]: string;
}
export interface DialogueTranslationCategory {
encounter: DialogueTranslationEntry;
victory: DialogueTranslationEntry;
defeat?: DialogueTranslationEntry;
}
export interface DialogueTranslationTrainerClass {
[key: string]: DialogueTranslationCategory;
}
export interface DialogueTranslationEntries {
[key: string]: DialogueTranslationTrainerClass;
}
export interface Localizable {
localize(): void;
}
export function initI18n(): void {
// Prevent reinitialization
if (isInitialized) {
return;
}
isInitialized = true;
let lang = "";
if (localStorage.getItem("prLang")) {
lang = localStorage.getItem("prLang");
}
/**
* 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.
* Don't forget to declare new language in `supportedLngs` i18next initializer
*
* 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.
*
* Q: How do I make a language selectable in the settings?
* A: In src/system/settings.ts, add a new case to the Setting.Language switch statement.
*/
i18next.use(LanguageDetector).init({
lng: lang,
nonExplicitSupportedLngs: true,
fallbackLng: "en",
supportedLngs: ["en", "es", "fr", "it", "de", "zh", "pt"],
debug: true,
interpolation: {
escapeValue: false,
},
resources: {
en: {
...enConfig
},
es: {
...esConfig
},
fr: {
...frConfig
},
it: {
...itConfig
},
de: {
...deConfig
},
pt_BR: {
...ptBrConfig
},
zh_CN: {
...zhCnConfig
},
zh_TW: {
...zhTWConfig
}
},
});
}
// Module declared to make referencing keys in the localization files type-safe.
declare module "i18next" {
interface CustomTypeOptions {
resources: {
menu: SimpleTranslationEntries;
menuUiHandler: SimpleTranslationEntries;
move: MoveTranslationEntries;
battle: SimpleTranslationEntries;
abilityTriggers: SimpleTranslationEntries;
ability: AbilityTranslationEntries;
pokeball: SimpleTranslationEntries;
pokemon: SimpleTranslationEntries;
pokemonInfo: PokemonInfoTranslationEntries;
commandUiHandler: SimpleTranslationEntries;
fightUiHandler: SimpleTranslationEntries;
titles: SimpleTranslationEntries;
trainerClasses: SimpleTranslationEntries;
trainerNames: SimpleTranslationEntries;
tutorial: SimpleTranslationEntries;
starterSelectUiHandler: SimpleTranslationEntries;
splashMessages: SimpleTranslationEntries;
nature: SimpleTranslationEntries;
growth: SimpleTranslationEntries;
egg: SimpleTranslationEntries;
weather: SimpleTranslationEntries;
modifierType: ModifierTypeTranslationEntries;
battleMessageUiHandler: SimpleTranslationEntries;
berry: BerryTranslationEntries;
voucher: SimpleTranslationEntries;
biome: SimpleTranslationEntries;
PGMdialogue: DialogueTranslationEntries;
PGMbattleSpecDialogue: SimpleTranslationEntries;
PGMmiscDialogue: SimpleTranslationEntries;
PGMdoubleBattleDialogue: DialogueTranslationEntries;
PGFdialogue: DialogueTranslationEntries;
PGFbattleSpecDialogue: SimpleTranslationEntries;
PGFmiscDialogue: SimpleTranslationEntries;
PGFdoubleBattleDialogue: DialogueTranslationEntries;
};
}
}
export default i18next;
export function getIsInitialized(): boolean {
return isInitialized;
}
let isInitialized = false;