diff --git a/core/Translator.cpp b/core/Translator.cpp index 9b40eac8..18dd1b9c 100644 --- a/core/Translator.cpp +++ b/core/Translator.cpp @@ -909,3 +909,28 @@ unsigned int Translator::GetServerLanguage() { return m_ServerLang; } + +unsigned int Translator::GetClientLanguage(int client) +{ + return GetServerLanguage(); +} + +bool Translator::GetLanguageInfo(unsigned int number, const char **code, const char **name) +{ + if (number >= GetLanguageCount()) + { + return false; + } + + Language *l = m_Languages[number]; + if (code) + { + *code = l->m_code2; + } + if (name) + { + *name = m_pStringTab->GetString(l->m_FullName); + } + + return true; +} diff --git a/core/Translator.h b/core/Translator.h index 065b48cb..e24d96c4 100644 --- a/core/Translator.h +++ b/core/Translator.h @@ -120,6 +120,7 @@ public: unsigned int FindOrAddPhraseFile(const char *phrase_file); BaseStringTable *GetStringTable(); unsigned int GetLanguageCount(); + bool GetLanguageInfo(unsigned int number, const char **code, const char **name); bool GetLanguageByCode(const char *code, unsigned int *index); size_t Translate(char *buffer, size_t maxlength, void **params, const Translation *pTrans); CPhraseFile *GetFileByIndex(unsigned int index); @@ -130,6 +131,7 @@ public: void **params, size_t *outlen=NULL); unsigned int GetServerLanguage(); + unsigned int GetClientLanguage(int client); private: bool AddLanguage(const char *langcode, const char *description); private: diff --git a/core/sm_stringutil.cpp b/core/sm_stringutil.cpp index dde2f493..d7041af9 100644 --- a/core/sm_stringutil.cpp +++ b/core/sm_stringutil.cpp @@ -81,7 +81,7 @@ try_serverlang: { langid = g_Translator.GetServerLanguage(); } else if ((target >= 1) && (target <= g_Players.GetMaxClients())) { - langid = g_Translator.GetServerLanguage(); + langid = g_Translator.GetClientLanguage(target); } else { pCtx->ThrowNativeErrorEx(SP_ERROR_PARAM, "Translation failed: invalid client index %d", target); goto error_out; diff --git a/core/smn_lang.cpp b/core/smn_lang.cpp index 54248abe..561285d8 100644 --- a/core/smn_lang.cpp +++ b/core/smn_lang.cpp @@ -16,6 +16,7 @@ #include "Translator.h" #include "LibrarySys.h" #include "sm_stringutil.h" +#include "PlayerManager.h" static cell_t sm_LoadTranslations(IPluginContext *pCtx, const cell_t *params) { @@ -48,9 +49,50 @@ static cell_t sm_SetGlobalTransTarget(IPluginContext *pContext, const cell_t *pa return 1; } -REGISTER_NATIVES(langNativeS) +static cell_t sm_GetClientLanguage(IPluginContext *pContext, const cell_t *params) +{ + CPlayer *player = g_Players.GetPlayerByIndex(params[1]); + if (!player || !player->IsConnected()) + { + return pContext->ThrowNativeError("Invalid client index %d", params[1]); + } + + return g_Translator.GetClientLanguage(params[1]); +} + +static cell_t sm_GetServerLanguage(IPluginContext *pContext, const cell_t *params) +{ + return g_Translator.GetServerLanguage(); +} + +static cell_t sm_GetLanguageCount(IPluginContext *pContext, const cell_t *params) +{ + return g_Translator.GetLanguageCount(); +} + +static cell_t sm_GetLanguageInfo(IPluginContext *pContext, const cell_t *params) +{ + const char *code; + const char *name; + + if (!g_Translator.GetLanguageInfo(params[1], &code, &name)) + { + return pContext->ThrowNativeError("Invalid language number %d", params[1]); + } + + pContext->StringToLocalUTF8(params[2], params[3], code, NULL); + pContext->StringToLocalUTF8(params[4], params[5], name, NULL); + + return 1; +} + +REGISTER_NATIVES(langNatives) { {"LoadTranslations", sm_LoadTranslations}, {"SetGlobalTransTarget", sm_SetGlobalTransTarget}, + {"GetClientLanguage", sm_GetClientLanguage}, + {"GetServerLanguage", sm_GetServerLanguage}, + {"GetLanguageCount", sm_GetLanguageCount}, + {"GetLanguageInfo", sm_GetLanguageInfo}, {NULL, NULL}, }; diff --git a/plugins/include/lang.inc b/plugins/include/lang.inc index 09f82e5b..678dc8b6 100644 --- a/plugins/include/lang.inc +++ b/plugins/include/lang.inc @@ -39,3 +39,41 @@ native LoadTranslations(const String:file[]); * @noreturn */ native SetGlobalTransTarget(client); + +/** + * Retrieves the language number of a client. + * Currently this simply returns the server language index. + * + * @param client Client index. + * @return Language number client is using. + * @error Invalid client index or client not in game. + */ +native GetClientLanguage(client); + +/** + * Retrieves the server's language. + * + * @return Language number server is using. + */ +native GetServerLanguage(client); + +/** + * Returns the number of languages known in languages.cfg. + * + * @return Language count. + */ +native GetLanguageCount(); + +/** + * Retrieves info about a given language number. + * + * @param language Language number. + * @param code Language code buffer (2-3 characters usually). + * @param codeLen Maximum length of the language code buffer. + * @param name Language name buffer. + * @param nameLen Maximum length of the language name buffer. + * @noreturn + * @error Invalid language number. + */ +native GetLanguageInfo(language, String:code[]="", codeLen=0, String:name[]="", nameLen=0); +