core/Translator.h
changeset 591 86e97f3eda1f
child 616 d5b1dcf38c07
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/Translator.h	Sat Mar 10 21:18:07 2007 +0000
     1.3 @@ -0,0 +1,136 @@
     1.4 +/**
     1.5 + * ===============================================================
     1.6 + * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     1.7 + * ===============================================================
     1.8 + *
     1.9 + * This file is not open source and may not be copied without explicit
    1.10 + * written permission of AlliedModders LLC.  This file may not be redistributed 
    1.11 + * in whole or significant part.
    1.12 + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    1.13 + *
    1.14 + * Version: $Id$
    1.15 + */
    1.16 +
    1.17 +#ifndef _INCLUDE_SOURCEMOD_TRANSLATOR_H_
    1.18 +#define _INCLUDE_SOURCEMOD_TRANSLATOR_H_
    1.19 +
    1.20 +#include "sm_trie.h"
    1.21 +#include <sh_string.h>
    1.22 +#include <sh_vector.h>
    1.23 +#include "sm_globals.h"
    1.24 +#include "sm_memtable.h"
    1.25 +#include "ITextParsers.h"
    1.26 +
    1.27 +#define MAX_TRANSLATE_PARAMS		32
    1.28 +
    1.29 +/* :TODO: write a templatized version of tries? */
    1.30 +
    1.31 +using namespace SourceHook;
    1.32 +class CTranslator;
    1.33 +
    1.34 +enum PhraseParseState
    1.35 +{
    1.36 +	PPS_None = 0,
    1.37 +	PPS_Phrases,
    1.38 +	PPS_InPhrase,
    1.39 +};
    1.40 +
    1.41 +struct Language
    1.42 +{
    1.43 +	char m_code2[3];
    1.44 +	int m_FullName;
    1.45 +};
    1.46 +
    1.47 +struct Translation
    1.48 +{
    1.49 +	const char *szPhrase;		/**< Translated phrase. */
    1.50 +	unsigned int fmt_count;		/**< Number of format parameters. */
    1.51 +	int *fmt_order;				/**< Format phrase order. */
    1.52 +};
    1.53 +
    1.54 +enum TransError
    1.55 +{
    1.56 +	Trans_Okay = 0,
    1.57 +	Trans_BadLanguage = 1,
    1.58 +	Trans_BadPhrase = 2,
    1.59 +	Trans_BadPhraseLanguage = 3,
    1.60 +	Trans_BadPhraseFile = 4,
    1.61 +};
    1.62 +
    1.63 +class CPhraseFile : public ITextListener_SMC
    1.64 +{
    1.65 +public:
    1.66 +	CPhraseFile(CTranslator *pTranslator, const char *file);
    1.67 +	~CPhraseFile();
    1.68 +public:
    1.69 +	void ReparseFile();
    1.70 +	const char *GetFilename();
    1.71 +	TransError GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans);
    1.72 +public: //ITextListener_SMC
    1.73 +	void ReadSMC_ParseStart();
    1.74 +	void ReadSMC_ParseEnd(bool halted, bool failed);
    1.75 +	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
    1.76 +	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
    1.77 +	SMCParseResult ReadSMC_LeavingSection();
    1.78 +	SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline);
    1.79 +private:
    1.80 +	void ParseError(const char *message, ...);
    1.81 +	void ParseWarning(const char *message, ...);
    1.82 +private:
    1.83 +	Trie *m_pPhraseLookup;
    1.84 +	String m_File;
    1.85 +	CTranslator *m_pTranslator;
    1.86 +	PhraseParseState m_ParseState;
    1.87 +	int m_CurPhrase;
    1.88 +	BaseMemTable *m_pMemory;
    1.89 +	BaseStringTable *m_pStringTab;
    1.90 +	unsigned int m_LangCount;
    1.91 +	String m_ParseError;
    1.92 +	String m_LastPhraseString;
    1.93 +	unsigned int m_CurLine;
    1.94 +	bool m_FileLogged;
    1.95 +};
    1.96 +
    1.97 +class CTranslator : 
    1.98 +	public ITextListener_SMC,
    1.99 +	public SMGlobalClass
   1.100 +{
   1.101 +public:
   1.102 +	CTranslator();
   1.103 +	~CTranslator();
   1.104 +public: //SMGlobalClass
   1.105 +	void OnSourceModAllInitialized();
   1.106 +public: //ITextListener_SMC
   1.107 +	void ReadSMC_ParseStart();
   1.108 +	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
   1.109 +	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
   1.110 +	SMCParseResult ReadSMC_LeavingSection();
   1.111 +public:
   1.112 +	void RebuildLanguageDatabase(const char *lang_header_file);
   1.113 +	unsigned int FindOrAddPhraseFile(const char *phrase_file);
   1.114 +	BaseStringTable *GetStringTable();
   1.115 +	unsigned int GetLanguageCount();
   1.116 +	bool GetLanguageByCode(const char *code, unsigned int *index);
   1.117 +	size_t Translate(char *buffer, size_t maxlength, void **params, const Translation *pTrans);
   1.118 +	CPhraseFile *GetFileByIndex(unsigned int index);
   1.119 +	TransError CoreTrans(int client, 
   1.120 +						 char *buffer, 
   1.121 +						 size_t maxlength, 
   1.122 +						 const char *phrase, 
   1.123 +						 void **params, 
   1.124 +						 size_t *outlen=NULL);
   1.125 +private:
   1.126 +	bool AddLanguage(const char *langcode, const char *description);
   1.127 +private:
   1.128 +	CVector<Language *> m_Languages;
   1.129 +	CVector<CPhraseFile *> m_Files;
   1.130 +	BaseStringTable *m_pStringTab;
   1.131 +	Trie *m_pLCodeLookup;
   1.132 +	bool m_InLanguageSection;
   1.133 +	String m_CustomError;
   1.134 +};
   1.135 +
   1.136 +extern CPhraseFile *g_pCorePhrases;
   1.137 +extern CTranslator g_Translator;
   1.138 +
   1.139 +#endif //_INCLUDE_SOURCEMOD_TRANSLATOR_H_