diff --git a/core/logic/PhraseCollection.cpp b/core/logic/PhraseCollection.cpp index 124f0094..66987820 100644 --- a/core/logic/PhraseCollection.cpp +++ b/core/logic/PhraseCollection.cpp @@ -89,6 +89,19 @@ IPhraseFile *CPhraseCollection::GetFile(unsigned int file) return m_Files[file]; } +bool CPhraseCollection::TranslationPhraseExists(const char *key) +{ + for (size_t i = 0; i < m_Files.size(); i++) + { + if (m_Files[i]->TranslationPhraseExists(key)) + { + return true; + } + } + + return false; +} + TransError CPhraseCollection::FindTranslation(const char *key, unsigned int langid, Translation *pTrans) { size_t i; diff --git a/core/logic/PhraseCollection.h b/core/logic/PhraseCollection.h index a43cf602..bf14e190 100644 --- a/core/logic/PhraseCollection.h +++ b/core/logic/PhraseCollection.h @@ -49,6 +49,7 @@ public: unsigned int GetFileCount(); IPhraseFile *GetFile(unsigned int file); void Destroy(); + bool TranslationPhraseExists(const char *key); TransError FindTranslation(const char *key, unsigned int langid, Translation *pTrans); bool FormatString( char *buffer, diff --git a/core/logic/Translator.cpp b/core/logic/Translator.cpp index 8a294517..36a76b21 100644 --- a/core/logic/Translator.cpp +++ b/core/logic/Translator.cpp @@ -672,6 +672,12 @@ TransError CPhraseFile::GetTranslation(const char *szPhrase, unsigned int lang_i return Trans_Okay; } +bool CPhraseFile::TranslationPhraseExists(const char *phrase) +{ + int address; + return m_PhraseLookup.retrieve(phrase, &address); +} + const char *CPhraseFile::GetFilename() { return m_File.c_str(); diff --git a/core/logic/Translator.h b/core/logic/Translator.h index cd88728f..53c91fdc 100644 --- a/core/logic/Translator.h +++ b/core/logic/Translator.h @@ -71,6 +71,7 @@ public: void ReparseFile(); const char *GetFilename(); TransError GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans); + bool TranslationPhraseExists(const char *phrase); public: //ITextListener_SMC void ReadSMC_ParseStart(); SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name); diff --git a/core/logic/smn_lang.cpp b/core/logic/smn_lang.cpp index 1f8e24fe..9371f586 100644 --- a/core/logic/smn_lang.cpp +++ b/core/logic/smn_lang.cpp @@ -36,6 +36,31 @@ #include #include +static cell_t sm_TranslationPhraseExists(IPluginContext *pCtx, const cell_t *params) +{ + IPlugin *pl = pluginsys->FindPluginByContext(pCtx->GetContext()); + IPhraseCollection *collection = pl->GetPhrases(); + + char *phrase; + pCtx->LocalToString(params[1], &phrase); + + return collection->TranslationPhraseExists(phrase); +} + +static cell_t sm_IsTranslatedForLanguage(IPluginContext *pCtx, const cell_t *params) +{ + IPlugin *pl = pluginsys->FindPluginByContext(pCtx->GetContext()); + IPhraseCollection *collection = pl->GetPhrases(); + + char *phrase; + pCtx->LocalToString(params[1], &phrase); + + int langid = params[2]; + + Translation trans; + return (collection->FindTranslation(phrase, langid, &trans) == Trans_Okay); +} + static cell_t sm_LoadTranslations(IPluginContext *pCtx, const cell_t *params) { char *filename, *ext; @@ -147,6 +172,8 @@ static cell_t sm_GetLanguageByName(IPluginContext *pContext, const cell_t *param REGISTER_NATIVES(langNatives) { + {"IsTranslatedForLanguage", sm_IsTranslatedForLanguage}, + {"TranslationPhraseExists", sm_TranslationPhraseExists}, {"LoadTranslations", sm_LoadTranslations}, {"SetGlobalTransTarget", sm_SetGlobalTransTarget}, {"GetClientLanguage", sm_GetClientLanguage}, diff --git a/plugins/include/lang.inc b/plugins/include/lang.inc index c46c974a..e1f9a14a 100644 --- a/plugins/include/lang.inc +++ b/plugins/include/lang.inc @@ -114,3 +114,21 @@ native int GetLanguageByCode(const char[] code); * @return Language number. -1 if not found. */ native int GetLanguageByName(const char[] name); + +/** + * Determines if the specified phrase exists within the plugin's + * translation cache. + * + * @param phrase Phrase to look for. + * @return True if phrase exists. + */ +native bool TranslationPhraseExists(const char[] phrase); + +/** + * Determines if a there is a translation for the speicifed language. + * + * @param phrase Phrase to check. + * @param language Language number. + * @return True if translation exists. + */ +native bool IsTranslatedForLanguage(const char[] phrase, int language); diff --git a/public/ITranslator.h b/public/ITranslator.h index d6f5c9d1..fc719a08 100644 --- a/public/ITranslator.h +++ b/public/ITranslator.h @@ -35,7 +35,7 @@ #include #define SMINTERFACE_TRANSLATOR_NAME "ITranslator" -#define SMINTERFACE_TRANSLATOR_VERSION 4 +#define SMINTERFACE_TRANSLATOR_VERSION 5 #define MAX_TRANSLATE_PARAMS 32 #define CORELANG_ENGLISH 0 @@ -113,6 +113,15 @@ namespace SourceMod * @return File name. */ virtual const char *GetFilename() =0; + + /** + * @brief Determines if a translation phrase exists within + * the file. + * + * @param phrase Phrase to search for. + * @return True if the phrase was found. + */ + virtual bool TranslationPhraseExists(const char* phrase) =0; }; /** @@ -221,6 +230,15 @@ namespace SourceMod unsigned int numparams, size_t *pOutLength, const char **pFailPhrase) =0; + + /** + * @brief Determines if a translation phrase exists within + * the collection. + * + * @param phrase Phrase to search for. + * @return True if the phrase was found. + */ + virtual bool TranslationPhraseExists(const char *phrase) =0; }; /**