ISource2ModTranslations

Translated text: phrases per plugin, each player's language.

A phrase is a name ("Welcome") with a text per language and, optionally, a format that says what its {1}, {2}, ... placeholders are ("{1:s},{2:d}"). Phrases come from wherever the plugin has them: a phrase file (translations/<file>.txt, SourceMod's layout), JSON, or one call per text at runtime -- a plugin that downloads its texts (HTTP, SQL) pushes them when they arrive and can change or remove them later. Files are only one source.

Lookups never fail: a phrase without the asked language falls back to the server's language, then English, then the phrase name itself.

auto* tr = g_pSource2ModTranslations;
tr->AddPhrase(g_PluginID, "Welcome", "en", "Welcome, {1}!", "{1:s}");
tr->AddPhrase(g_PluginID, "Welcome", "cze", "Vítej, {1}!");

const char* args[] = { player->GetPlayerName() };
char text[256];
tr->FormatPhrase(g_PluginID, "Welcome", player->GetPlayerSlot(), args, 1, text, sizeof(text));

Interface id ISource2ModTranslations001 (SOURCE2MOD_TRANSLATIONS_INTERFACE) · Global g_pSource2ModTranslations · Header source2mod/ISource2ModTranslations.h

Methods

GetLanguageCount

virtual int GetLanguageCount() = 0;

Languages known: configs/languages.cfg plus those added at runtime.

GetLanguageInfo

virtual bool GetLanguageInfo(int language, const char** code, const char** name) = 0;

A language's code ("en", "cze") and name ("English").

ParameterType
languageint
codeconst char**
nameconst char**

Returns bool — false for an index out of range.

FindLanguage

virtual int FindLanguage(const char* codeOrName) = 0;

A language by its code or its name (either, case-insensitive); -1 when unknown.

ParameterType
codeOrNameconst char*

AddLanguage

virtual int AddLanguage(const char* code, const char* name) = 0;

Adds a language at runtime (or finds it when the code is known).

ParameterType
codeconst char*
nameconst char*

Returns int — Its index.

GetServerLanguage

virtual int GetServerLanguage() = 0;

The server's language (ServerLang in core.json).

GetPlayerLanguage

virtual int GetPlayerLanguage(CPlayerSlot slot) = 0;

A player's language: one set with SetPlayerLanguage, else the client's own (its cl_language), else the server's.

ParameterType
slotCPlayerSlot

SetPlayerLanguage

virtual void SetPlayerLanguage(CPlayerSlot slot, int language) = 0;

Overrides a player's language until they disconnect; -1 goes back to theirs.

ParameterType
slotCPlayerSlot
languageint

LoadPhraseFile

virtual bool LoadPhraseFile(PluginId owner, const char* file, char* error, size_t maxlen) = 0;

Loads translations/<file>.txt and translations/<code>/<file>.txt for every language (SourceMod's phrase files) into @p owner's phrases.

ParameterType
ownerPluginId
fileconst char*
errorchar*
maxlensize_t

Returns bool — false (and why in @p error) when the base file is missing or broken.

LoadPhrasesJson

virtual bool LoadPhrasesJson(PluginId owner, const char* json, char* error, size_t maxlen) = 0;

Loads phrases from JSON: {"<phrase>": {"#format": "{1:s}", "<code>": "<text>", ...}, ...}. Unknown language codes are added.

ParameterType
ownerPluginId
jsonconst char*
errorchar*
maxlensize_t

Returns bool — false (and why in @p error) for text that is not such an object.

AddPhrase

virtual void AddPhrase(PluginId owner, const char* phrase, const char* language, const char* text, const char* format = nullptr) = 0;

Adds or replaces one text of a phrase.

ParameterTypeDefaultDescription
ownerPluginId
phraseconst char*
languageconst char*A language code; one not known yet is added.
textconst char*
formatconst char*nullptrThe phrase's placeholder format ("{1:s},{2:d}"); nullptr keeps the one it has.

RemovePhrase

virtual bool RemovePhrase(PluginId owner, const char* phrase, const char* language = nullptr) = 0;

Removes a phrase's text in one language, or the whole phrase for a null / empty @p language.

ParameterTypeDefault
ownerPluginId
phraseconst char*
languageconst char*nullptr

Returns bool — false when there was nothing to remove.

ClearPhrases

virtual void ClearPhrases(PluginId owner) = 0;

Drops every phrase of @p owner (a plugin's go when it unloads anyway).

ParameterType
ownerPluginId

PhraseExists

virtual bool PhraseExists(PluginId owner, const char* phrase) = 0;
ParameterType
ownerPluginId
phraseconst char*

IsTranslatedFor

virtual bool IsTranslatedFor(PluginId owner, const char* phrase, int language) = 0;
ParameterType
ownerPluginId
phraseconst char*
languageint

Translate

virtual size_t Translate(PluginId owner, const char* phrase, CPlayerSlot target, char* buffer, size_t maxlen) = 0;

The raw text of a phrase for a target ({n} placeholders left in).

ParameterTypeDescription
ownerPluginId
phraseconst char*
targetCPlayerSlotA player, or SOURCE2MOD_LANG_SERVER.
bufferchar*
maxlensize_t

Returns size_t — Bytes written; the fallbacks (server language, English, the phrase name) mean there always is a text.

FormatPhrase

virtual size_t FormatPhrase(PluginId owner, const char* phrase, CPlayerSlot target, const char* const* args, int numArgs, char* buffer, size_t maxlen) = 0;

The text for a target with its {1}..{n} replaced by @p args. The values are text already; the phrase's format is not applied.

ParameterType
ownerPluginId
phraseconst char*
targetCPlayerSlot
argsconst char* const*
numArgsint
bufferchar*
maxlensize_t

Returns size_t — Bytes written.

Macros

SOURCE2MOD_LANG_SERVER

#define SOURCE2MOD_LANG_SERVER CPlayerSlot(-1)

The server, as a translation target (FormatPhrase / Translate).

On this page