public/ITranslator.h
author David Anderson <dvander@alliedmods.net>
Wed Feb 25 20:07:12 2009 -0500 (2009-02-25)
changeset 2607 f3b8bbe17f85
parent 2042 4a2faafca477
child 3004 e3ac0dc6d9a4
permissions -rw-r--r--
Backed out changeset fa286305bd3f
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_TRANSLATOR_INTERFACE_H_
    33 #define _INCLUDE_SOURCEMOD_TRANSLATOR_INTERFACE_H_
    34 
    35 #include <IShareSys.h>
    36 
    37 #define SMINTERFACE_TRANSLATOR_NAME		"ITranslator"
    38 #define SMINTERFACE_TRANSLATOR_VERSION	1
    39 
    40 /**
    41  * @file ITranslator.h
    42  * @brief Defines interfaces related to translation files.
    43  */
    44 
    45 namespace SourceMod
    46 {
    47 	/**
    48 	 * @brief SourceMod hardcodes the English language (default) to ID 0.
    49 	 * This cannot be changed and languages.cfg should never have it as anything 
    50 	 * other than the first index.
    51 	 */
    52 	#define SOURCEMOD_LANGUAGE_ENGLISH			0
    53 
    54 	/**
    55 	 * @brief For %T formats, specifies that the language should be that of the 
    56 	 * server and not a specific client.
    57 	 */
    58 	#define SOURCEMOD_SERVER_LANGUAGE			0
    59 
    60 	/**
    61 	 * @brief Translation error codes.
    62 	 */
    63 	enum TransError
    64 	{
    65 		Trans_Okay = 0,					/**< Translation succeeded. */
    66 		Trans_BadLanguage = 1,			/**< Bad language ID. */
    67 		Trans_BadPhrase = 2,			/**< Phrase not found. */
    68 		Trans_BadPhraseLanguage = 3,	/**< Phrase not found in the given language. */
    69 		Trans_BadPhraseFile = 4,		/**< Phrase file was unreadable. */
    70 	};
    71 
    72 	/**
    73 	 * @brief Contains information about a translation phrase.
    74 	 */
    75 	struct Translation
    76 	{
    77 		const char *szPhrase;		/**< Translated phrase. */
    78 		unsigned int fmt_count;		/**< Number of format parameters. */
    79 		int *fmt_order;				/**< Array of size fmt_count where each 
    80 										 element is the numerical order of 
    81 										 parameter insertion, starting from 
    82 										 0.
    83 										 */
    84 	};
    85 
    86 	/**
    87 	 * @brief Represents a phrase file from SourceMod's "translations" folder.
    88 	 */
    89 	class IPhraseFile
    90 	{
    91 	public:
    92 		/**
    93 		 * @brief Attempts to find a translation phrase in a phrase file.
    94 		 *
    95 		 * @param szPhrase			String containing the phrase name.
    96 		 * @param lang_id			Language ID.
    97 		 * @param pTrans			Buffer to store translation info.
    98 		 * @return					Translation error code indicating success 
    99 		 *							(pTrans is filled) or failure (pTrans 
   100 		 *							contents is undefined).
   101 		 */
   102 		virtual TransError GetTranslation(
   103 			const char *szPhrase, 
   104 			unsigned int lang_id, 
   105 			Translation *pTrans) =0;
   106 
   107 		/**
   108 		 * @brief Returns the file name of this translation file.
   109 		 *
   110 		 * @return					File name.
   111 		 */
   112 		virtual const char *GetFilename() =0;
   113 	};
   114 
   115 	/**
   116 	 * Represents a collection of phrase files.
   117 	 */
   118 	class IPhraseCollection
   119 	{
   120 	public:
   121 		/**
   122 		 * @brief Adds a phrase file to the collection, using a cached one 
   123 		 * if already found.  The return value is provided for informational 
   124 		 * purposes and does not need to be saved.  The life time of the 
   125 		 * return pointer is equal to the life time of the collection.
   126 		 *
   127 		 * This function will internally ignore dupliate additions but still 
   128 		 * return a valid pointer.
   129 		 *
   130 		 * @param filename			File name, without the ".txt" extension, of 
   131 		 *							the phrase file in the translations folder.
   132 		 * @return					An IPhraseFile pointer, even if the file does 
   133 		 *							not exist.
   134 		 */
   135 		virtual IPhraseFile *AddPhraseFile(const char *filename) =0;
   136 
   137 		/**
   138 		 * @brief Returns the number of contained phrase files.
   139 		 *
   140 		 * @return					Number of contained phrase files.
   141 		 */
   142 		virtual unsigned int GetFileCount() =0;
   143 		
   144 		/**
   145 		 * @brief Returns the pointer to a contained phrase file.
   146 		 *
   147 		 * @param file				File index, from 0 to GetFileCount()-1.
   148 		 * @return					IPhraseFile pointer, or NULL if out of 
   149 		 *							range.
   150 		 */
   151 		virtual IPhraseFile *GetFile(unsigned int file) =0;
   152 
   153 		/**
   154 		 * @brief Destroys the phrase collection, freeing all internal 
   155 		 * resources and invalidating the object.
   156 		 */
   157 		virtual void Destroy() =0;
   158 
   159 		/**
   160 		 * @brief Attempts a translation across a given language.  All 
   161 		 * contained files are searched for an appropriate match; the 
   162 		 * first valid match is returned.
   163 		 *
   164 		 * @param key				String containing the phrase name.
   165 		 * @param langid			Language ID to translate to.
   166 		 * @param pTrans			Translation buffer.
   167 		 * @return					Translation error code; on success, 
   168 		 *							pTrans is valid.  On failure, the 
   169 		 *							contents of pTrans is undefined.
   170 		 */
   171 		virtual TransError FindTranslation(
   172 			const char *key,
   173 			unsigned int langid,
   174 			Translation *pTrans) =0;
   175 
   176 		/**
   177 		 * @brief Formats a phrase given a parameter stack.  The parameter 
   178 		 * stack size must exactly match the expected parameter count.  If 
   179 		 * this count is too small or too large, the format fails.
   180 		 *
   181 		 * @param buffer			Buffer to store formatted text.
   182 		 * @param maxlength			Maximum length of the buffer.
   183 		 * @param format			String containing format information.  
   184 		 *							This is equivalent to SourceMod's Format() 
   185 		 *							native, and sub-translations are acceptable. 
   186 		 * @param params			An array of pointers to each parameter. 
   187 		 *							Integer parameters must have a pointer to the integer.
   188 		 *							Float parameters must have a pointer to a float.
   189 		 *							String parameters must be a string pointer.
   190 		 *							Char parameters must be a pointer to a char.
   191 		 *							Translation parameters fill multiple indexes in the 
   192 		 *							array.  For %T translations, the expected stack is:
   193 		 *							[phrase string pointer] [int target id pointer] [...] 
   194 		 *							Where [...] is the required parameters for the translation,  
   195 		 *							in the order expected by the phrase, not the phrase's 
   196 		 *							translation.  For example, say the format is:
   197 		 *							"%d %T" and the phrase's format is {1:s,2:f}, then the 
   198 		 *							parameter stack should be:
   199 		 *							int *, const char *, int *, const char *, float *
   200 		 *							The %t modifier is the same except the target id pointer 
   201 		 *							would be removed:
   202 		 *							int *, const char *, const char *, float *
   203 		 * @param numparams			Number of parameters in the params array.
   204 		 * @param pOutLength		Optional pointer filled with output length on success.
   205 		 * @param pFailPhrase		Optional pointer; on failure, is filled with NULL if the 
   206 		 *							failure was not due to a failed translation phrase.  
   207 		 *							Otherwise, it is filled with the given phrase name pointer 
   208 		 *							from the parameter stack.  Undefined on success.
   209 		 * @return					True on success.  False if the parameter stack was not 
   210 		 *							exactly the right length, or if a translation phrase 
   211 		 *							could not be found.
   212 		 */
   213 		virtual bool FormatString(
   214 			char *buffer,
   215 			size_t maxlength,
   216 			const char *format,
   217 			void **params,
   218 			unsigned int numparams,
   219 			size_t *pOutLength,
   220 			const char **pFailPhrase) =0;
   221 	};
   222 
   223 	/**
   224 	 * @brief Provides functions for translation.
   225 	 */
   226 	 class ITranslator : public SMInterface
   227 	{
   228 	public:
   229 		virtual const char *GetInterfaceName() =0;
   230 		virtual unsigned int GetInterfaceVersion() =0;
   231 	public:
   232 		/**
   233 		 * @brief Creates a new phrase collection object.
   234 		 *
   235 		 * @return			A new phrase collection object, which must be 
   236 		 *					destroyed via IPhraseCollection::Destroy() when 
   237 		 *					no longer needed.
   238 		 */
   239 		virtual IPhraseCollection *CreatePhraseCollection() =0;
   240 
   241 		/**
   242 		 * @brief Returns the server language.
   243 		 *
   244 		 * @return			Server language index.
   245 		 */
   246 		virtual unsigned int GetServerLanguage() =0;
   247 
   248 		/**
   249 		 * @brief Returns a client's language.
   250 		 *
   251 		 * @param client	Client index.
   252 		 * @return			Client language index, or server's if client's is 
   253 		 *					not known.
   254 		 */
   255 		virtual unsigned int GetClientLanguage(int client) =0;
   256 
   257 		/**
   258 		 * @brief Sets the global client SourceMod will use for assisted 
   259 		 * translations (that is, %t).
   260 		 *
   261 		 * @param index		Client index (0 for server).
   262 		 * @return			Old global client value.
   263 		 */
   264 		virtual int SetGlobalTarget(int index) =0;
   265 
   266 		/**
   267 		 * @brief Returns the global client SourceMod is currently using 
   268 		 * for assisted translations (that is, %t).
   269 		 *
   270 		 * @return			Global client index (0 for server).
   271 		 */
   272 		virtual int GetGlobalTarget() const =0;
   273 
   274 		/**
   275 		 * @brief Formats a phrase given a parameter stack.  The parameter 
   276 		 * stack size must exactly match the expected parameter count.  If 
   277 		 * this count is too small or too large, the format fails.
   278 		 *
   279 		 * Note: This is the same as IPhraseCollection::FormatString(), except 
   280 		 * that the IPhraseCollection parameter is explicit instead of implicit.
   281 		 *
   282 		 * @param buffer			Buffer to store formatted text.
   283 		 * @param maxlength			Maximum length of the buffer.
   284 		 * @param format			String containing format information.  
   285 		 *							This is equivalent to SourceMod's Format() 
   286 		 *							native, and sub-translations are acceptable. 
   287 		 * @param pPhrases			Optional phrase collection pointer to search for 
   288 		 *							phrases.
   289 		 * @param params			An array of pointers to each parameter. 
   290 		 *							Integer parameters must have a pointer to the integer.
   291 		 *							Float parameters must have a pointer to a float.
   292 		 *							String parameters must be a string pointer.
   293 		 *							Char parameters must be a pointer to a char.
   294 		 *							Translation parameters fill multiple indexes in the 
   295 		 *							array.  For %T translations, the expected stack is:
   296 		 *							[phrase string pointer] [int target id pointer] [...] 
   297 		 *							Where [...] is the required parameters for the translation,  
   298 		 *							in the order expected by the phrase, not the phrase's 
   299 		 *							translation.  For example, say the format is:
   300 		 *							"%d %T" and the phrase's format is {1:s,2:f}, then the 
   301 		 *							parameter stack should be:
   302 		 *							int *, const char *, int *, const char *, float *
   303 		 *							The %t modifier is the same except the target id pointer 
   304 		 *							would be removed:
   305 		 *							int *, const char *, const char *, float *
   306 		 * @param numparams			Number of parameters in the params array.
   307 		 * @param pOutLength		Optional pointer filled with output length on success.
   308 		 * @param pFailPhrase		Optional pointer; on failure, is filled with NULL if the 
   309 		 *							failure was not due to a failed translation phrase.  
   310 		 *							Otherwise, it is filled with the given phrase name pointer 
   311 		 *							from the parameter stack.  Undefined on success.
   312 		 * @return					True on success.  False if the parameter stack was not 
   313 		 *							exactly the right length, or if a translation phrase 
   314 		 *							could not be found.
   315 		 */
   316 		virtual bool FormatString(
   317 			char *buffer,
   318 			size_t maxlength,
   319 			const char *format,
   320 			IPhraseCollection *pPhrases,
   321 			void **params,
   322 			unsigned int numparams,
   323 			size_t *pOutLength,
   324 			const char **pFailPhrase) =0;
   325 	};
   326 }
   327 
   328 #endif //_INCLUDE_SOURCEMOD_TRANSLATOR_INTERFACE_H_
   329