|
| 1 | +# Phaser3 i18n Plugin |
| 2 | + |
| 3 | +[](https://github.com/koreezgames/phaser3-i18n-plugin) []() [](#status) |
| 4 | + |
| 5 | +Phaser3 i18n is a plugin for Phaser 3 that allows you to have seamless translations in your game. It uses **[i18next](https://github.com/i18next/i18next)** as it's source for translations management, which is widely adopted by the JS community in other projects as well. |
| 6 | + |
| 7 | +Key features: |
| 8 | + |
| 9 | +* Support for translations namespaces |
| 10 | +* Simple key/value JSON |
| 11 | +* Seamless switching of languages |
| 12 | +* No extra function calls for translating strings, directly build into Phaser's Text object |
| 13 | + |
| 14 | +## Getting Started |
| 15 | + |
| 16 | +### Installation |
| 17 | + |
| 18 | +#### **_Using script tag:_** |
| 19 | + |
| 20 | +[](https://www.jsdelivr.com/package/npm/@koreez/phaser3-i18n-plugin/dist/phaseri18n.min.js) |
| 21 | + |
| 22 | +```html |
| 23 | +<script src="//cdn.jsdelivr.net/npm/@koreez/phaser3-i18n-plugin/dist/phaseri18n.min.js"></script> |
| 24 | +``` |
| 25 | + |
| 26 | +#### **_Using npm:_** |
| 27 | + |
| 28 | +[](https://www.npmjs.com/package/@koreez/phaser3-i18n-plugin) |
| 29 | + |
| 30 | +```shell |
| 31 | +$ npm i -g npm |
| 32 | +$ npm i --save @koreez/phaser3-i18n-plugin |
| 33 | +``` |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +### Import the plugin |
| 38 | + |
| 39 | +##### **_CommonJS_** |
| 40 | + |
| 41 | +```javascript |
| 42 | +var I18nPlugin = require("@koreez/phaser3-i18n-plugin"); |
| 43 | +``` |
| 44 | + |
| 45 | +##### **_ES2015_** |
| 46 | + |
| 47 | +```javascript |
| 48 | +import I18nPlugin from "@koreez/phaser3-i18n-plugin"; |
| 49 | +``` |
| 50 | + |
| 51 | +### Load the plugin |
| 52 | + |
| 53 | +You need to load the plugin in your game. This is done just like any other plugin in Phaser 3. |
| 54 | +So, to load the plugin, include it one of the Phaser Scenes _preload_ method. |
| 55 | + |
| 56 | +```javascript |
| 57 | +preload () { |
| 58 | + this.load.plugin('I18nPlugin', I18nPlugin) |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### Initialize the plugin |
| 63 | + |
| 64 | +Afther plugin has beeen loaded you need to initialize it. |
| 65 | + |
| 66 | +```javascript |
| 67 | +create () { |
| 68 | + this.sys.install('I18nPlugin') |
| 69 | + this.sys.i18n.init( |
| 70 | + { |
| 71 | + fallbackLng: 'en', |
| 72 | + loadPath: 'assets/i18n/{{lng}}/{{ns}}.json', |
| 73 | + debug: false, |
| 74 | + }, |
| 75 | + function () { |
| 76 | + console.log('I18nPlugin initialized!') |
| 77 | + }, |
| 78 | + ) |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +The plugin will patch _add.text, add.bitmapText and add.dynamicBitmapText / make.text, make.bitmapText and make.dynamicBitmapText_ methods with additional functionality. |
| 83 | + |
| 84 | +### Create localized texts |
| 85 | + |
| 86 | +```javascript |
| 87 | +// x - any |
| 88 | +// y - any |
| 89 | +// font - bitmap font |
| 90 | +// text - should be translation key |
| 91 | +// size - font size |
| 92 | +// interpolations - interpolation for transleation (for example { 0: "value0", 1: "value1" }), note this is not required parametr |
| 93 | +this.add.bitmapText(x, y, font, text, size, interpolations); |
| 94 | +``` |
| 95 | + |
| 96 | +##### **_or via config_** |
| 97 | + |
| 98 | +```javascript |
| 99 | +var config = { |
| 100 | + x: 100, |
| 101 | + y: 100, |
| 102 | + text: "translationKey", |
| 103 | + font: "atari-classic", |
| 104 | + size: 64, |
| 105 | + interpolations: { 0: "value0", 1: "value1" }, |
| 106 | +}; |
| 107 | +this.make.dynamicBitmapText(config); |
| 108 | +``` |
0 commit comments