Phaser3 i18n is a plugin for Phaser 3 that allows you to have seamless translations in your game. It uses i18next as it's source for translations management, which is widely adopted by the JS community in other projects as well.
Key features:
- Support for translations namespaces
- Simple key/value JSON
- Seamless switching of languages
- No extra function calls for translating strings, directly build into Phaser's Text object
$ npm i -g npm
$ npm i --save @koreez/phaser3-i18n
import { I18nPlugin } from "@koreez/phaser3-i18n";
You need to load the plugin in your game. This is done just like any other plugin in Phaser 3. So, to load the plugin, include it in plugins config.
const config = {
type: Phaser.AUTO,
parent: "phaser-example",
width: 800,
height: 600,
plugins: {
scene: [
{
key: "i18nPlugin",
plugin: I18nPlugin,
mapping: "i18n"
}
]
},
scene: {
create: create
}
};
After plugin has been loaded you need to initialize it.
create () {
this.i18n.initialize(
{
fallbackLng: 'en',
loadPath: 'assets/i18n/{{lng}}/{{ns}}.json',
debug: false,
},
function () {
console.log('I18nPlugin initialized!')
},
)
}
The plugin will patch add.text, add.bitmapText and add.dynamicBitmapText / make.text, make.bitmapText and make.dynamicBitmapText methods with additional functionality.
// x - any
// y - any
// font - bitmap font
// text - should be translation key
// size - font size
// interpolations - interpolation for translation (for example { 0: "value0", 1: "value1" }), note this is not required parameter
this.add.bitmapText(x, y, font, text, size, interpolations);
var config = {
x: 100,
y: 100,
text: "translationKey",
font: "atari-classic",
size: 64,
interpolations: { 0: "value0", 1: "value1" }
};
this.make.dynamicBitmapText(config);
When you're all done and set up for translations and have your key value file, next up is starting to translate them. For smaller games this is fairly simple and still manageable by editing your translations in a text file. But when you start with bigger games and more translations (and translation namespaces) you want to manage your translations better, and maybe even use external translator services.
Two of those service you could use are locize and/or Poeditor. Both these services allow you to online edit key value JSON translations that are used in i18next. The advantage of using such tools is that it's easier to allow external people work on your translations and they aggregate the statistics of your projects (translation completion, languages managed, etc.)
Big thanks to this great repo: