Add Translation Natives (#669)

* Add Translation Natives

See #626

* Fix vocab errors
* Better description
* Bump ITranslator Version
* Implement KyleS' Review Requests
* Improve documentation
This commit is contained in:
Michael Flaherty 2017-11-20 21:24:01 -08:00 committed by Kyle Sanderson
parent 43cdf20fd3
commit d685f30847
7 changed files with 85 additions and 1 deletions

View File

@ -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;

View File

@ -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,

View File

@ -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();

View File

@ -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);

View File

@ -36,6 +36,31 @@
#include <ISourceMod.h>
#include <am-string.h>
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},

View File

@ -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);

View File

@ -35,7 +35,7 @@
#include <IShareSys.h>
#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;
};
/**