Skip to content

koreezgames/phaser3-i18n-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Phaser3 i18n Plugin

GitHub license Build Status codebeat badge Test Coverage npm version Greenkeeper badge styled with prettier

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

Getting Started

Installation

npm

$ npm i -g npm
$ npm i --save  @koreez/phaser3-i18n

Usage

Import the plugin

import { I18nPlugin } from "@koreez/phaser3-i18n";

Load the plugin

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
    }
};

Initialize the plugin

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.

Create localized texts

// 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);
or via config
var config = {
    x: 100,
    y: 100,
    text: "translationKey",
    font: "atari-classic",
    size: 64,
    interpolations: { 0: "value0", 1: "value1" }
};
this.make.dynamicBitmapText(config);

Manage translations

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.)

Credits

Big thanks to this great repo:

https://github.com/orange-games/phaser-i18next

License

MIT