Skip to content

Instantly share code, notes, and snippets.

@IS4Code
Created October 7, 2017 12:34
Show Gist options
  • Save IS4Code/21122249e58e473ea8fc2fff85f3d2e1 to your computer and use it in GitHub Desktop.
Save IS4Code/21122249e58e473ea8fc2fff85f3d2e1 to your computer and use it in GitHub Desktop.
/*
The MIT License (MIT)
Copyright (c) 2017 IllidanS4
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//Documentation: http://forum.sa-mp.com/showthread.php?t=642789
//Version 1.0
/*
native RegisterTranslation(language, const text[], const replacement[LOC_MAX_TEXT_LENGTH]);
native SetPlayerLanguage(playerid, language);
native GetPlayerLanguage(playerid);
native GetLocText(playerid, const text[], rettext[], bool:packed=LOC_PACKED, maxlength=sizeof(rettext));
native LocText(playerid, const text[], bool:packed=LOC_PACKED);
native GetTextReplacement(language, const text[], rettext[], bool:packed=LOC_PACKED, maxlength=sizeof(rettext));
native TextReplacement(language, const text[], bool:packed=LOC_PACKED);
native SendClientMessageLoc(playerid, color, const message[]);
native SendClientMessageToAllLoc(color, const message[]);
native SendClientMessageFormatLoc(playerid, color, const format[], {Float,_}:...);
native GameTextForPlayerLoc(playerid, const string[], time, style);
*/
//These definitions must be provided: LOC_MAX_TEXTS, LOC_NUM_LOCALIZATIONS
#assert defined LOC_MAX_TEXTS
#assert defined LOC_NUM_LOCALIZATIONS
//These definitions are optional: LOC_MAX_TEXT_LENGTH, LOC_LANG_FUNC, LOC_PACKED, LOC_LANG_PVAR, LOC_FILTER_FUNC
#if !defined LOC_MAX_TEXT_LENGTH
#define LOC_MAX_TEXT_LENGTH 144
#endif
#if defined LOC_PACKED
#undef LOC_PACKED
#define LOC_PACKED true
#else
#define LOC_PACKED false
#endif
//callback LOC_LANG_FUNC(playerid);
//callback LOC_FILTER_FUNC(language, const text[], rettext[LOC_MAX_TEXT_LENGTH]);
#if !defined LOC_LANG_FUNC
#if defined LOC_LANG_PVAR
static stock _PVAR_LOC_LANGUAGE[] = LOC_LANG_PVAR;
#else
static stock _PVAR_LOC_LANGUAGE[] = "i_loc_lang";
#endif
stock SetPlayerLanguage(playerid, language)
{
return SetPVarInt(playerid, _PVAR_LOC_LANGUAGE, language);
}
#define LOC_LANG_FUNC(%0) GetPVarInt(%0,_PVAR_LOC_LANGUAGE)
#endif
stock GetPlayerLanguage(playerid)
{
return LOC_LANG_FUNC(playerid);
}
#if !defined LOC_FILTER_FUNC
#define LOC_FILTER_FUNC(%0) false
#endif
enum _LOC_LOCALIZATION_TEXT
{
_LOC_LOCALIZATION_TEXT_KEY,
_LOC_LOCALIZATION_TEXT_REP[LOC_MAX_TEXT_LENGTH char]
}
static stock _localizations[LOC_NUM_LOCALIZATIONS][LOC_MAX_TEXTS][_LOC_LOCALIZATION_TEXT];
static stock _regrep(loc[_LOC_LOCALIZATION_TEXT], address, const replacement[])
{
new addr = loc[_LOC_LOCALIZATION_TEXT_KEY];
if(addr == 0 || addr == address)
{
loc[_LOC_LOCALIZATION_TEXT_KEY] = address;
strpack(loc[_LOC_LOCALIZATION_TEXT_REP], replacement, sizeof(loc[_LOC_LOCALIZATION_TEXT_REP]));
return true;
}
return false;
}
static stock _regtext(table[LOC_MAX_TEXTS][_LOC_LOCALIZATION_TEXT], address, const replacement[LOC_MAX_TEXT_LENGTH])
{
new hash = (address/4)%sizeof(table);
new index = hash;
do{
if(_regrep(table[index++], address, replacement)) return index-1;
index %= sizeof(table);
}while(index != hash);
print("i_loc: The localization table is too small ("#LOC_MAX_TEXTS"). Increase LOC_MAX_TEXTS in your script.");
#emit halt 4
return -1;
}
static stock _addressof(const array[])
{
#pragma unused array
#emit load.s.pri 12
#emit retn
return 0;
}
//Registers a new translation for a text variable
stock RegisterTranslation(language, const text[], const replacement[LOC_MAX_TEXT_LENGTH])
{
assert(1 <= language <= LOC_NUM_LOCALIZATIONS);
language--;
_regtext(_localizations[language], _addressof(text), replacement);
}
//Gets the replacement of a text, for a specific language, if possible. On success, returns true and fills rettext.
stock GetTextReplacement(language, const text[], rettext[], bool:packed=LOC_PACKED, maxlength=sizeof(rettext))
{
if(!(1 <= language <= LOC_NUM_LOCALIZATIONS))
{
return false;
}
language--;
new address = _addressof(text);
new hash = (address/4)%sizeof(_localizations[]);
new index = hash;
do{
if(_localizations[language][index][_LOC_LOCALIZATION_TEXT_KEY] == address)
{
if(packed)
{
strpack(rettext, _localizations[language][index][_LOC_LOCALIZATION_TEXT_REP], maxlength);
}else{
strunpack(rettext, _localizations[language][index][_LOC_LOCALIZATION_TEXT_REP], maxlength);
}
return true;
}
index = (index+1)%sizeof(_localizations[]);
}while(index != hash);
return LOC_FILTER_FUNC(language+1, text, rettext);
}
static _rettext[LOC_MAX_TEXT_LENGTH];
//Returns the localized text, for a specific language, or the one passed.
stock TextReplacement(language, const text[], bool:packed=LOC_PACKED)
{
if(!GetTextReplacement(language, text, _rettext, packed))
{
if(packed)
{
strpack(_rettext, text);
}else{
strunpack(_rettext, text);
}
}
return _rettext;
}
//Gets the replacement of a text, for a specific player, if possible. On success, returns true and fills rettext.
stock GetLocText(playerid, const text[], rettext[], bool:packed=LOC_PACKED, maxlength=sizeof(rettext))
{
new language = LOC_LANG_FUNC(playerid);
return GetTextReplacement(language, text, rettext, packed, maxlength);
}
//Returns the localized text, for a specific player, or the one passed.
stock LocText(playerid, const text[], bool:packed=LOC_PACKED)
{
new language = LOC_LANG_FUNC(playerid);
if(!GetTextReplacement(language, text, _rettext, packed))
{
if(packed)
{
strpack(_rettext, text);
}else{
strunpack(_rettext, text);
}
}
return _rettext;
}
//Sends a localized message to a player.
stock SendClientMessageLoc(playerid, color, const message[])
{
new language = LOC_LANG_FUNC(playerid);
if(1 <= language <= LOC_NUM_LOCALIZATIONS)
{
if(GetTextReplacement(language, message, _rettext, true))
{
return SendClientMessage(playerid, color, _rettext);
}
}
return SendClientMessage(playerid, color, message);
}
//Sends a localized message to all players.
stock SendClientMessageToAllLoc(color, const message[])
{
new locs[LOC_NUM_LOCALIZATIONS][LOC_MAX_TEXT_LENGTH char];
for(new i = 0; i < LOC_NUM_LOCALIZATIONS; i++)
{
if(!GetTextReplacement(i+1, message, locs[i], true))
{
strpack(locs[i], message);
}
}
for(new playerid = 0, top = GetPlayerPoolSize(); playerid <= top; playerid++)
{
if(IsPlayerConnected(playerid))
{
new language = LOC_LANG_FUNC(playerid);
if(1 <= language <= LOC_NUM_LOCALIZATIONS)
{
SendClientMessage(playerid, color, locs[language-1]);
}else{
SendClientMessage(playerid, color, message);
}
}
}
return true;
}
//Displays a localized text for a player.
stock GameTextForPlayerLoc(playerid, const string[], time, style)
{
new language = LOC_LANG_FUNC(playerid);
if(GetTextReplacement(language, string, _rettext, true))
{
return GameTextForPlayer(playerid, _rettext, time, style);
}else{
return GameTextForPlayer(playerid, string, time, style);
}
}
//Sends a localized formatted message to a player.
#define SendClientMessageFormatLoc(%0,%1,%2,%3) SendClientMessageFormat(%0,%1,LocText(%0,%2),%3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment