Internationalization
Internationalization is a very important core function of Infernus
. Even if you don't consider it in the process of developing a script, you need to know at least the player internationalization section, otherwise you may encounter common situations such as garbled codes.
VSCode plugin
infernus-starter provides the plugin installation recommendation file .vscode/ extensions.json
. It is highly recommended that you click install in the prompt in the lower right corner of VSCode
, so that you can easily and intuitively view international data during development.
i18n-ally-custom-framework.yml
and settings.json
The configuration file related to the plugin has been set up for you.
Example
Define language pack
The language pack consists of the data of the .json
file. the tree structure is used as the language pack data key
.
The defined string data can contain %s
as a placeholder keyword to populate the data.
Chinese language pack
{
"server": {
"running": "成功运行由 node.js 强力驱动的 omp 服务器",
"connection": "连接信息: %s - %s : %s"
}
}
English language pack
{
"server": {
"running": "Successfully running an omp server powered by node.js",
"connection": "connection information: %s - %s : %s"
}
}
Instance
Get an internationalization example through new I18n("default region key", "language pack")
.
Usually we only need to use the $t
function.
$t("language pack data key", [placeholder array], region key)
can get the text in the corresponding language pack.
Instead of passing a placeholder array, you can replace it with null
or undefined
or an empty array when the string does not have a placeholder.
Not passing in the third parameter means fetching data from the default locale.
import { I18n } from "@infernus/core";
// Internationalized json language Pack
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"));
Practical functions
I18n
class also has several practical static functions that you can try when you encounter conversions between internationalization.
- encodeToBuf
- A byte array used to convert a substring to a specified charset.
- decodeFromBuf
- A string used to convert a byte array to a specified charset.
- getValidStr
- If you intercept a valid string in a byte array, the first
0
is encountered, which is regarded as the end of the string.
- If you intercept a valid string in a byte array, the first
Player internationalization
There are two attributes on the player instance to control internationalization, namely charset
and locale
, corresponding to the charset and region, respectively. The default values are as follows:
class Player {
charset = "ISO-8859-1";
locale = "en_US";
}
Usually $t
combines the player's region to get the corresponding language pack data.
// Suppose you have an instance of player
console.log(
$t("server.incoming", ["127.0.0.1", "8080", "123456"], player.locale),
);
If you want to modify the player's language or charset, you should modify the player's charset
and locale
attributes. Always make sure that the player's charset
is correct. You can use the ip
address to guess the player's area, and let the player enter a specific string and compare bytes to figure out what charset it is.
supportAllNickname
TIP
By default, the game server only allows letters, numbers and underlined usernames to connect to the game.
Because omp
provides some new api
, you can disable / enable certain characters (decimal bytes 0-255) for user names to connect to the server.
The GameMode.supportAllNickname()
method makes use of the new api to connect all international usernames, including any characters such as Chinese characters, to the game.
import { GameMode } from "@infernus/core";
GameMode.onInit(({ next }) => {
GameMode.supportAllNickname();
return next();
});
Principle
WARNING
Two-way charset data exchange is normal on the premise that the player's charset is correct.
If the wrong charset is set, both the server and the client will get garbled code.
Infernus
use iconv-lite
convert charset,use polyfill.amx
intercept gamemode
part of the callback is returned as a decimal byte array.
The server converts the charset to the client side functions such as SendClientMessage
, SendClientMessageToAll
, ShowPlayerDialog
and CreateDynamic3DTextLabel
, that is, the utf8
data is converted into the player's charset data to ensure that the player can display the data normally.
Charset conversion from client to server, such as OnText
, OnPlayerCommandText
, OnDialogResponse
, has been done to ensure that the data exchange between the server and the client is correct.
TIP
Although the charset has been converted, the characters of utf8
that are not included in the player's charset will still be displayed in the form of garbled codes, such as ?
, which is limited by the underlying code of the early sa
game. For example, only the charset of western fonts emoji
or Chinese characters will be garbled when received by emoji
.
For Infernus
, the underlying layer of Infernus
always exchanges data in the format of utf8
, because the implementation of the two is based on the existing algorithm of mapping files, which is similar to the concept of sprite image in the front end, rather than the real text data. Some client modules, such as Chinese patches, use utf8
at the bottom to display commonly used words, but all the utf8
fonts cannot be listed in one map. This is very unreasonable and the effect is certainly not as good as that of text data.
For sa
definitive edition, it must be implemented internally using utf8
. If omp
launches definitive edition online in the future, no additional processing should be required.
Terminal garbled characters
If you encounter garbled characters in the terminal when using logger such as pino.js
, you can try the following commands to resolve the issue.
For PowerShell
users, execute the command below (effective only for the current terminal session).
If you want this change to be permanent, add the command to your $PROFILE.
$OutputEncoding = [Console]::InputEncoding = [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding
For cmd
users, use the following command to run your omp server.
cmd /c "chcp 65001 > nul & omp-server"
The above command changes the charset of PowerShell
or cmd
to UTF-8
.
For other environments, please search for solutions on your own.