One object with key/label for languages, i18next

This commit is contained in:
Adrian 2024-08-26 00:07:56 -04:00
parent 5faf048f25
commit d75c43218a
2 changed files with 42 additions and 74 deletions

View File

@ -79,6 +79,24 @@ async function initFonts(language: string | undefined) {
} }
} }
type LanguageType = {
[key:string]: string
};
export const languages: LanguageType = {
"en": "English",
"es": "Español",
"it": "Italiano",
"fr": "Français",
"de": "Deutsch",
"pt-BR": "Português (BR)",
"zh-CN": "简体中文",
"zh-TW": "繁體中文",
"ko": "한국어",
"ja": "日本語",
// "ca-ES": "Català",
};
export async function initI18n(): Promise<void> { export async function initI18n(): Promise<void> {
// Prevent reinitialization // Prevent reinitialization
if (isInitialized) { if (isInitialized) {
@ -92,7 +110,7 @@ export async function initI18n(): Promise<void> {
* Q: How do I add a new language? * 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. * 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. * 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 * Don't forgot declare new language in `languages` const with language code as key and language label as value.
* *
* Q: How do I add a new namespace? * 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. * A: To add a new namespace, create a new file in each language folder with the translations.
@ -109,7 +127,9 @@ export async function initI18n(): Promise<void> {
await i18next.init({ await i18next.init({
nonExplicitSupportedLngs: true, nonExplicitSupportedLngs: true,
fallbackLng: "en", fallbackLng: "en",
supportedLngs: ["en", "es", "fr", "it", "de", "zh", "pt", "ko", "ja", "ca"], supportedLngs: Object.keys(languages).map((langKey)=>langKey.split("-")[0]).filter((lang,i,a) => {
return (a.findIndex((t) => t === lang) === i);
}),
defaultNS: "menu", defaultNS: "menu",
ns: Object.keys(enConfig), ns: Object.keys(enConfig),
detection: { detection: {

View File

@ -8,7 +8,7 @@ import SettingsUiHandler from "#app/ui/settings/settings-ui-handler";
import { EaseType } from "#enums/ease-type"; import { EaseType } from "#enums/ease-type";
import { MoneyFormat } from "#enums/money-format"; import { MoneyFormat } from "#enums/money-format";
import { PlayerGender } from "#enums/player-gender"; import { PlayerGender } from "#enums/player-gender";
import { getIsInitialized, initI18n } from "#app/plugins/i18n.js"; import { getIsInitialized, initI18n, languages } from "#app/plugins/i18n.js";
function getTranslation(key: string): string { function getTranslation(key: string): string {
if (!getIsInitialized()) { if (!getIsInitialized()) {
@ -79,66 +79,8 @@ export interface Setting {
isHidden?: () => boolean isHidden?: () => boolean
} }
type LanguageType = {
key: string;
label: string;
handler?: () => {};
value?: string;
};
const languages: Array<LanguageType> = [
{
key: "en",
label: "English",
},
{
key: "es",
label: "Español",
},
{
key: "it",
label: "Italiano",
},
{
key: "fr",
label: "Français",
},
{
key: "de",
label: "Deutsch",
},
{
key: "pt-BR",
label: "Português (BR)",
},
{
key: "zh-CN",
label: "简体中文",
},
{
key: "zh-TW",
label: "繁體中文",
},
{
key: "ko",
label: "한국어",
},
{
key: "ko-KR",
label: "한국어",
},
{
key: "ja",
label: "日本語",
},
// {
// key:"ca-ES",
// label: "Català",
// },
];
// default is English // default is English
const currentLanguage = languages.find((locale) => i18next.resolvedLanguage! === locale.key)?.label ?? "English"; export const currentLanguage = languages[i18next.resolvedLanguage!] ?? "English";
/** /**
* Setting Keys for existing settings * Setting Keys for existing settings
@ -828,18 +770,24 @@ export function setSetting(scene: BattleScene, setting: string, value: integer):
} }
}; };
scene.ui.setOverlayMode(Mode.OPTION_SELECT, { scene.ui.setOverlayMode(Mode.OPTION_SELECT, {
options: [...languages.filter((locale,i,a) => { options: [
// Remove language active and sames labels ...Object.values(languages)
if (currentLanguage !== locale.label && a.findIndex((t) => t.label === locale.label) === i) { .map((lang, index) => {
locale.handler = () => changeLocaleHandler(locale.key); return {
return locale; label: lang,
} handler: () => changeLocaleHandler(Object.keys(languages)[index]),
}), };
{ })
label: i18next.t("settings:back"), .filter((lang) => {
handler: () => cancelHandler(), // Remove language active
}], return currentLanguage !== lang.label;
maxOptions: 7 }),
{
label: i18next.t("settings:back"),
handler: () => cancelHandler(),
},
],
maxOptions: 7,
}); });
return false; return false;
} }