samp.directory
← Scripts and libraries

Tiaansu/JSONLang

Provides a simple API for multi-language systems.

Library★ 1updated Nov 2022MITPawnArchived
Open on GitHub

The author has archived this repository, so it is no longer maintained.

Try it in the Pawn compiler →

Adds its include files, with the libraries it depends on, to the Pawn compiler in your browser, ready to include in a script.

README.md

JSONLang

Dependencies

Installation

Simply install to your project:

sampctl package install Tiaansu/JSONLang

Include in your code and begin using the library:

#include <JSONLang>

Usage

This package works by loading json files from I18n folder in the root directory.

These file should have the same keys, see the I18n directory in the repo for an example.

Loading JSON Languages

To load a language from a json file, use InitJsonLanguageFromFile with just the language filename (including extension), not the full file path. For example InitJsonLanguageFromFile("en-US.lang.json"); loads the I18n/en-US.lang.json file.

You could also combine this with the fsutil plugin and iterate through the I18n directory.

new
    Directory:dir = OpenDir("I18n"),
    entry[256],
    ENTRY_TYPE:type,
    name[64]
;

while (DirNext(dir, type, entry))
{
    if (type == E_REGULAR)
    {
        PathBase(entry, name);

        InitJsonLanguageFromFile(name);
    }
}

CloseDir(dir);

or just by simply using InitJsonLanguages() in OnGameModeInit.

Using JSON Language Strings

Now you've loaded json languages, you can use strings from each json language with the GetJsonLanguageString(json_lang_id, const key[], bool:encode = false) function. The first parameter is the language ID, which you can obtain via name with GetJsonLanguageID.

For Example:

SendClientMessage(playerid, -1, GetJsonLanguageString(GetJsonLanguageID("en-US"), "greet"));

Would send the string from en-US keyed by greet.

Per-Player Language

You can store a language ID for each player with SetPlayerJsonLanguage and retrieve it with GetPlayerJsonLanguage. For example, your server could display a list of languages in a dialog with GetJsonLanguageList and when the player selects a language, call SetPlayerJsonLanguage with their selection and then in future you can use GetPlayerJsonLanguage to obtain the language ID that player selected.

There is also a useful macro @L(playerid, key[]) which you can use to quickly get a string for a player using their assigned language ID.

SendClientMessage(playerid, -1, @L(playerid, "greet"));

Testing

To test, simply run the package:

sampctl package run

languagemulti-language-systemsopen-mppawn-librarypawn-packagesa-mpsa-mp-library

This page is built from what GitHub publishes about the repository. samp.directory does not host the code. Report a problem with this listing

Tiaansu/JSONLang: SA-MP Library | samp.directory