Internalization
This page explains how to add multi-language support to your Pixi’VN game using i18next, including UI and narration translation strategies. How to translate the visual novel?
In visual novels and similar games, it is common to allow players to select their native language.
For your Pixi’VN project, you should use an external library to handle translations. This gives you the flexibility to choose the implementation that best fits your needs.
The most popular and widely compatible library is i18next.
i18next is an internationalization framework for JavaScript. To use it, you need to install and initialize it. Learn more on the i18next website.
Templates
In all templates, i18next is already included and configured. You can start translating your game right away.
Translations are stored in multiple JSON files (one per language), using key-value pairs. The key is a unique identifier for the text, and the value is the translated string.
import i18n, { type ReadCallback } from "i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import ChainedBackend from "i18next-chained-backend";
import { initReactI18next } from "react-i18next";
class Backend extends ChainedBackend {
override async read(lng: string, ns: string, callback: ReadCallback) {
try {
const object = await getLocalesResource(lng);
const result = object[ns];
callback(null, result);
} catch (error) {
callback(error as Error, { data: null });
}
}
}
export const useI18n = async () => {
if (!i18n.isInitialized) {
return await i18n
.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
debug: false,
fallbackLng: "en",
supportedLngs: ["en"],
interpolation: {
escapeValue: false,
},
load: "currentOnly",
detection: {
order: ["localStorage"],
caches: ["localStorage"],
},
missingInterpolationHandler(_text, value, _options) {
return value[1];
},
});
}
};
function getLocalesResource(
lng: string,
): Promise<Record<string, Record<string, string>>> {
return import(`./../locales/${lng}.json`);
}To translate the UI, use the t function provided by i18next. Pass the translation key to t to get the corresponding text in the player's language.
For example:
import { useTranslation } from "react-i18next";
export default function MyComponent() {
const { t } = useTranslation("ui");
return (
<div>
<Button>{t("text_speed")}</Button>
</div>
);
}Templates
In all templates, the StepLabelProps interface already includes a t function. See pixi-vn.d.ts.
It is recommended to use t inside the label rather than directly in the UI, so you can take advantage of i18n Interpolation.
In Pixi’VN projects, you often need to translate narration text in TypeScript/JavaScript labels. To enable this, the translation function must be in the StepLabelProps interface.
It is recommended to use the original string as the translation key.
export const startLabel = newLabel("start", [
({ t }) => {
narration.dialogue = {
character: liam,
text: t("Hello, my name is {{name}}", { name: "Liam" }),
};
},
]);Create
ink
If you use ink, you can automatically extract strings from ink text for translation. See more here.
Translation JSON files must be created manually. It is recommended to split translations into two sections (see es.json):
- UI texts: for screens, settings, quick buttons, etc.—everything not part of the narration.
- Narration texts: for dialogues, choice menus, etc.
{
"ui": {
"text_speed": "Velocidad del texto"
// ...
},
"narration": {
"Hello, my name is {{name}}": "Hola, mi nombre es {{name}}"
// ...
}
}