Skip to content

Internationalization ​

Internationalization in Infernus is primarily about charset encoding conversion, not multi-language translation. While $t can serve translation needs, the core feature is ensuring correct data exchange between the server (UTF-8) and the player client (legacy charsets like ISO-8859-1 or GBK). Most servers only serve a single region, so you may not need multi-language at all — but you should at least understand player internationalization to avoid garbled text.

VSCode Extension ​

infernus-starter ships with a plugin recommendation file (.vscode/extensions.json). It is highly recommended to click install when prompted by VSCode, so you can view internationalization data easily during development.

i18n-ally-custom-framework.yml and settings.json are already configured for the plugin.

Example ​

Defining Language Packs ​

Language packs are .json files. Tree-structured keys are used to organize translation data.

Strings can contain %s placeholders for dynamic values.

Chinese Language Pack ​

json
{
  "server": {
    "running": "成功运行由 node.js 强力驱动的 omp 服务器",
    "connection": "连接信息: %s - %s : %s"
  }
}

English Language Pack ​

json
{
  "server": {
    "running": "Successfully running an omp server powered by node.js",
    "connection": "connection information: %s - %s : %s"
  }
}

Instance ​

Create an I18n instance with new I18n("default locale key", languagePack).

Typically you only need the $t function.

$t("translation key", [placeholder array], locale key) returns the translated text for the given locale.

When there are no placeholders, pass null, undefined, or an empty array.

Omitting the third parameter falls back to the default locale.

ts
import { I18n } from "@infernus/core";

// Internationalized JSON language packs
import zh_CN from "./locales/zh_CN.json";
import en_US from "./locales/en_US.json";

const locales = {
  zh_CN,
  en_US,
};

export const { $t } = new I18n("en_US", locales);

console.log($t("server.running"));
console.log($t("server.connection", ["127.0.0.1", "8080", "123456"]));
console.log($t("server.connection", ["127.0.0.1", "8080", "123456"], "zh_CN"));

Utility Functions ​

The I18n class also provides several useful static methods for character set conversions:

  • encodeToBuf
    • Converts a string to a byte array in the specified charset.
  • decodeFromBuf
    • Converts a byte array to a string in the specified charset.
  • getValidStr
    • Extracts a valid string from a byte array — the first 0 byte marks the end of the string.
  • convertSpecialChar
    • Remaps accented Latin characters onto the byte slots the stock SA font uses for their glyphs. See How It Works.

Player Internationalization ​

Each player instance has three internationalization properties: inputCharset, outputCharset and locale. Default values:

ts
class Player {
  inputCharset = "ISO-8859-1";
  outputCharset = "ISO-8859-1";
  locale = "en_US";
}

The two charsets answer different questions:

  • Input: what the player sends to the server. inputCharset is the encoding the server uses to decode this player's data — name (GetPlayerName), chat (OnText), commands (OnPlayerCommandText), dialog input (OnDialogResponse), and gpci.
  • Output: what the server sends to the player. The server encodes the text with outputCharset, and the player's client decodes it with the same encoding — messages (SendClientMessage), private messages (SendPlayerMessageToPlayer), dialogs (ShowPlayerDialog), and chat bubbles (SetPlayerChatBubble).

A wrong input charset garbles what the server reads from that player (names, chat, commands); a wrong output charset garbles what the player sees.

SetPlayerName also encodes with inputCharset, so a name survives a round trip through GetPlayerName.

Both default to ISO-8859-1, because a vanilla SA client speaks one legacy encoding in both directions. A client mod does not have to: it may redraw the font so the client expects UTF-8 on screen while its keyboard, IME or patched input still produces legacy bytes — or the other way round. A single charset forces you to pick one side and garble the other; two properties describe the client the player actually runs.

ts
PlayerEvent.onConnect(({ player, next }) => {
  // a font mod that draws UTF-8, on a client that still types GBK
  player.inputCharset = "gbk";
  player.outputCharset = "utf8";
  return next();
});

Assigning either property fires OnPlayerCharsetChange, which names the side that changed:

ts
PlayerEvent.onCharsetChange(({ player, newCharset, oldCharset, direction, next }) => {
  console.log(player.id, direction, oldCharset, "->", newCharset);
  return next();
});

locale picks the language pack; the two charsets decide whether the text arrives intact. Set both before the player reads the first message — OnPlayerConnect is the earliest point, and if you probe the client, do it right after the probe. The region can be guessed from the IP address; inputCharset can be detected by asking the player to type a known string and trying candidate encodings until the decoded bytes match. That round trip measures input only: outputCharset depends on the client the player runs — vanilla, a font mod, or a russifier.

Migrating from player.charset

player.charset no longer exists. To reproduce the old behavior, assign both properties at once:

ts
player.inputCharset = player.outputCharset = "ISO-8859-1";

OnPlayerCharsetChange (exposed as PlayerEvent.onCharsetChange) gains a direction argument that names the side that changed.

supportAllNickname ​

TIP

By default, the game server only allows alphanumeric characters and underscores in usernames.

OMP provides new APIs to enable or disable specific byte values (0–255) in usernames.

GameMode.supportAllNickname() leverages these APIs to allow all international usernames — including Chinese characters and other non-ASCII names — to connect.

ts
import { GameMode } from "@infernus/core";

GameMode.onInit(({ next }) => {
  GameMode.supportAllNickname();
  return next();
});

How It Works ​

WARNING

Bidirectional charset exchange works only when inputCharset and outputCharset both match the player's client. A wrong value garbles its own direction and leaves the other one working.

Infernus uses iconv-lite for charset conversion and polyfill.amx to intercept gamemode callbacks, returning them as decimal byte arrays.

Server-to-client APIs encode UTF-8 into outputCharset — SendClientMessage, SendClientMessageToAll, SendPlayerMessageToPlayer, ShowPlayerDialog and SetPlayerChatBubble, plus everything built on them.

Client-to-server callbacks decode with inputCharset — OnText, OnPlayerCommandText and OnDialogResponse, together with GetPlayerName, SetPlayerName and gpci.

TIP

Conversion cannot invent characters: one that the target charset lacks still arrives as ?. That is a limitation of the original SA client. An ISO-8859-1 client receiving emoji or Chinese characters will show garbage no matter how the server is configured.

GameText and TextDraw take a different path. They ignore both player charsets and encode with the charset you pass — win1252 for GameText, ISO-8859-1 for TextDraw by default. The client then picks a glyph from its font texture by byte value (think of a CSS sprite) instead of decoding the bytes as text.

Per-player TextLabel instances default to player.outputCharset; global labels fall back to utf8. Menu is encoded once at creation with the menu's own charset, so it cannot follow a per-player output encoding.

Both APIs also remap accented Latin characters (é, ñ, ü, …) when needed: the stock SA font stores their glyphs in dedicated byte slots rather than at their Latin-1 positions, and convertSpecialChar moves them there. The convertSpecialChars option controls this remap. It defaults to true only for the stock-font charsets (win1252, cp1252 and ISO-8859-1) and to false for other encodings such as utf8 or cp1251, where the remap would corrupt the bytes. Pass it explicitly to override:

ts
new GameText("...", 3000, 3).forAll("cp1251", false);
new TextDraw({
  x: 10,
  y: 10,
  text: "Привет",
  charset: "cp1251",
  convertSpecialChars: false,
}).create();

Russian works the same way from the other side. A russifier is a Russian localization mod the player installs on their own client; it redraws the game's font, putting Cyrillic letters into the slots the original font reserves for accented Latin characters. @infernus/rustext supplies the server half, rewriting the outgoing GameText, TextDraw and Menu RPC text so that cp1251 bytes land on that mod's glyph slots. Pass charset: "cp1251" explicitly — the defaults above turn Cyrillic into ? before the package can convert anything — and set the RussifierType that matches the pack the player actually installed, since the packs lay the letters out differently (SanLtd is the default, alongside OneC, Rush, MG and others). The package ships no russifier assets. @infernus/rustext rewrites those RPCs, so with charset: "cp1251" the remap is already off by default. Text sent to all players (GameText.forAll) carries no player id and is not converted.

The SA Definitive Edition uses UTF-8 internally. If OMP adds online support for it in the future, no additional handling should be needed.

Terminal Garbled Characters ​

If you encounter garbled characters in the terminal when using logging libraries like pino.js, try one of the following solutions.

For PowerShell users, run the following command (effective for the current session only).

To make the change permanent, add the command to your $PROFILE.

powershell
$OutputEncoding = [Console]::InputEncoding = [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding

For cmd users, use the following command to start your OMP server:

sh
cmd /c "chcp 65001 > nul & omp-server"

These commands change the terminal charset to UTF-8.

For other environments, please search for solutions on your own.