changed around translation API a bit

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40388
This commit is contained in:
David Anderson 2007-01-26 22:37:54 +00:00
parent b489378b9c
commit 326ac67c68
2 changed files with 31 additions and 36 deletions

View File

@ -538,33 +538,35 @@ void CPhraseFile::ReadSMC_ParseEnd(bool halted, bool failed)
} }
} }
const char *CPhraseFile::GetTranslation(const Translation *pTrans, int **fmt_order, unsigned int *fmt_count) TransError CPhraseFile::GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans)
{ {
if (pTrans->lang_id >= m_LangCount) if (lang_id >= m_LangCount)
{ {
return NULL; return Trans_BadLanguage;
} }
void *object; void *object;
if (!sm_trie_retrieve(m_pPhraseLookup, pTrans->szPhrase, &object)) if (!sm_trie_retrieve(m_pPhraseLookup, szPhrase, &object))
{ {
return NULL; return Trans_BadPhrase;
} }
phrase_t *pPhrase = (phrase_t *)m_pMemory->GetAddress(reinterpret_cast<int>(object)); phrase_t *pPhrase = (phrase_t *)m_pMemory->GetAddress(reinterpret_cast<int>(object));
trans_t *trans = (trans_t *)m_pMemory->GetAddress(pPhrase->trans_tbl); trans_t *trans = (trans_t *)m_pMemory->GetAddress(pPhrase->trans_tbl);
trans = &trans[pTrans->lang_id]; trans = &trans[lang_id];
if (trans->stridx == -1) if (trans->stridx == -1)
{ {
return NULL; return Trans_BadPhraseLanguage;
} }
*fmt_order = (int *)m_pMemory->GetAddress(trans->fmt_order); pTrans->fmt_order = (int *)m_pMemory->GetAddress(trans->fmt_order);
*fmt_count = pPhrase->fmt_count; pTrans->fmt_count = pPhrase->fmt_count;
return m_pStringTab->GetString(trans->stridx); pTrans->szPhrase = m_pStringTab->GetString(trans->stridx);
return Trans_Okay;
} }
const char *CPhraseFile::GetFilename() const char *CPhraseFile::GetFilename()
@ -735,30 +737,15 @@ SMCParseResult CTranslator::ReadSMC_KeyValue(const char *key, const char *value,
return SMCParse_Continue; return SMCParse_Continue;
} }
size_t CTranslator::Translate(char *buffer, size_t maxlength, const Translation *pTrans) size_t CTranslator::Translate(char *buffer, size_t maxlength, void **params, const Translation *pTrans)
{ {
if (pTrans->file_id >= m_Files.size()) void *new_params[MAX_TRANSLATE_PARAMS];
{
return 0;
}
CPhraseFile *pFile = m_Files[pTrans->file_id];
unsigned int count;
int *order_table;
const char *str;
if ((str=pFile->GetTranslation(pTrans, &order_table, &count)) == NULL)
{
return 0;
}
void *params[MAX_TRANSLATE_PARAMS];
/* Rewrite the parameter order */ /* Rewrite the parameter order */
for (unsigned int i=0; i<count; i++) for (unsigned int i=0; i<pTrans->fmt_count; i++)
{ {
params[i] = pTrans->params[order_table[i]]; new_params[i] = params[pTrans->fmt_order[i]];
} }
return gnprintf(buffer, maxlength, str, params); return gnprintf(buffer, maxlength, pTrans->szPhrase, new_params);
} }

View File

@ -43,10 +43,17 @@ struct Language
struct Translation struct Translation
{ {
const char *szPhrase; const char *szPhrase; /**< Translated phrase. */
unsigned int lang_id; unsigned int fmt_count; /**< Number of format parameters. */
unsigned int file_id; int *fmt_order; /**< Format phrase order. */
void **params; };
enum TransError
{
Trans_Okay = 0,
Trans_BadLanguage = 1,
Trans_BadPhrase = 2,
Trans_BadPhraseLanguage = 3
}; };
class CPhraseFile : public ITextListener_SMC class CPhraseFile : public ITextListener_SMC
@ -57,7 +64,7 @@ public:
public: public:
void ReparseFile(); void ReparseFile();
const char *GetFilename(); const char *GetFilename();
const char *GetTranslation(const Translation *pTrans, int **fmt_order, unsigned int *fmt_count); TransError GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans);
public: //ITextListener_SMC public: //ITextListener_SMC
void ReadSMC_ParseStart(); void ReadSMC_ParseStart();
void ReadSMC_ParseEnd(bool halted, bool failed); void ReadSMC_ParseEnd(bool halted, bool failed);
@ -99,7 +106,8 @@ public:
BaseStringTable *GetStringTable(); BaseStringTable *GetStringTable();
unsigned int GetLanguageCount(); unsigned int GetLanguageCount();
bool GetLanguageByCode(const char *code, unsigned int *index); bool GetLanguageByCode(const char *code, unsigned int *index);
size_t Translate(char *buffer, size_t maxlength, const Translation *pTrans); size_t Translate(char *buffer, size_t maxlength, void **params, const Translation *pTrans);
CPhraseFile *GetFileByIndex(unsigned int index);
private: private:
CVector<Language *> m_Languages; CVector<Language *> m_Languages;
CVector<CPhraseFile *> m_Files; CVector<CPhraseFile *> m_Files;