public/ITranslator.h
author Nicholas Hastings <skamonkey@gmail.com>
Wed Aug 14 14:20:57 2013 -0400 (2013-08-14)
changeset 4008 490b01aa0683
parent 3532 88a0d2c30bc3
child 4306 f8a08c7f0c92
permissions -rw-r--r--
Updated some CS:GO gamedata.
     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	3
    39 
    40 #define MAX_TRANSLATE_PARAMS		32
    41 #define CORELANG_ENGLISH			0
    42 
    43 /**
    44  * @file ITranslator.h
    45  * @brief Defines interfaces related to translation files.
    46  */
    47 
    48 namespace SourceMod
    49 {
    50 	/**
    51 	 * @brief SourceMod hardcodes the English language (default) to ID 0.
    52 	 * This cannot be changed and languages.cfg should never have it as anything 
    53 	 * other than the first index.
    54 	 */
    55 	#define SOURCEMOD_LANGUAGE_ENGLISH			0
    56 
    57 	/**
    58 	 * @brief For %T formats, specifies that the language should be that of the 
    59 	 * server and not a specific client.
    60 	 */
    61 	#define SOURCEMOD_SERVER_LANGUAGE			0
    62 
    63 	/**
    64 	 * @brief Translation error codes.
    65 	 */
    66 	enum TransError
    67 	{
    68 		Trans_Okay = 0,					/**< Translation succeeded. */
    69 		Trans_BadLanguage = 1,			/**< Bad language ID. */
    70 		Trans_BadPhrase = 2,			/**< Phrase not found. */
    71 		Trans_BadPhraseLanguage = 3,	/**< Phrase not found in the given language. */
    72 		Trans_BadPhraseFile = 4,		/**< Phrase file was unreadable. */
    73 	};
    74 
    75 	/**
    76 	 * @brief Contains information about a translation phrase.
    77 	 */
    78 	struct Translation
    79 	{
    80 		const char *szPhrase;		/**< Translated phrase. */
    81 		unsigned int fmt_count;		/**< Number of format parameters. */
    82 		int *fmt_order;				/**< Array of size fmt_count where each 
    83 										 element is the numerical order of 
    84 										 parameter insertion, starting from 
    85 										 0.
    86 										 */
    87 	};
    88 
    89 	/**
    90 	 * @brief Represents a phrase file from SourceMod's "translations" folder.
    91 	 */
    92 	class IPhraseFile
    93 	{
    94 	public:
    95 		/**
    96 		 * @brief Attempts to find a translation phrase in a phrase file.
    97 		 *
    98 		 * @param szPhrase			String containing the phrase name.
    99 		 * @param lang_id			Language ID.
   100 		 * @param pTrans			Buffer to store translation info.
   101 		 * @return					Translation error code indicating success 
   102 		 *							(pTrans is filled) or failure (pTrans 
   103 		 *							contents is undefined).
   104 		 */
   105 		virtual TransError GetTranslation(
   106 			const char *szPhrase, 
   107 			unsigned int lang_id, 
   108 			Translation *pTrans) =0;
   109 
   110 		/**
   111 		 * @brief Returns the file name of this translation file.
   112 		 *
   113 		 * @return					File name.
   114 		 */
   115 		virtual const char *GetFilename() =0;
   116 	};
   117 
   118 	/**
   119 	 * Represents a collection of phrase files.
   120 	 */
   121 	class IPhraseCollection
   122 	{
   123 	public:
   124 		/**
   125 		 * @brief Adds a phrase file to the collection, using a cached one 
   126 		 * if already found.  The return value is provided for informational 
   127 		 * purposes and does not need to be saved.  The life time of the 
   128 		 * return pointer is equal to the life time of the collection.
   129 		 *
   130 		 * This function will internally ignore dupliate additions but still 
   131 		 * return a valid pointer.
   132 		 *
   133 		 * @param filename			File name, without the ".txt" extension, of 
   134 		 *							the phrase file in the translations folder.
   135 		 * @return					An IPhraseFile pointer, even if the file does 
   136 		 *							not exist.
   137 		 */
   138 		virtual IPhraseFile *AddPhraseFile(const char *filename) =0;
   139 
   140 		/**
   141 		 * @brief Returns the number of contained phrase files.
   142 		 *
   143 		 * @return					Number of contained phrase files.
   144 		 */
   145 		virtual unsigned int GetFileCount() =0;
   146 		
   147 		/**
   148 		 * @brief Returns the pointer to a contained phrase file.
   149 		 *
   150 		 * @param file				File index, from 0 to GetFileCount()-1.
   151 		 * @return					IPhraseFile pointer, or NULL if out of 
   152 		 *							range.
   153 		 */
   154 		virtual IPhraseFile *GetFile(unsigned int file) =0;
   155 
   156 		/**
   157 		 * @brief Destroys the phrase collection, freeing all internal 
   158 		 * resources and invalidating the object.
   159 		 */
   160 		virtual void Destroy() =0;
   161 
   162 		/**
   163 		 * @brief Attempts a translation across a given language.  All 
   164 		 * contained files are searched for an appropriate match; the 
   165 		 * first valid match is returned.
   166 		 *
   167 		 * @param key				String containing the phrase name.
   168 		 * @param langid			Language ID to translate to.
   169 		 * @param pTrans			Translation buffer.
   170 		 * @return					Translation error code; on success, 
   171 		 *							pTrans is valid.  On failure, the 
   172 		 *							contents of pTrans is undefined.
   173 		 */
   174 		virtual TransError FindTranslation(
   175 			const char *key,
   176 			unsigned int langid,
   177 			Translation *pTrans) =0;
   178 
   179 		/**
   180 		 * @brief Formats a phrase given a parameter stack.  The parameter 
   181 		 * stack size must exactly match the expected parameter count.  If 
   182 		 * this count is too small or too large, the format fails.
   183 		 *
   184 		 * @param buffer			Buffer to store formatted text.
   185 		 * @param maxlength			Maximum length of the buffer.
   186 		 * @param format			String containing format information.  
   187 		 *							This is equivalent to SourceMod's Format() 
   188 		 *							native, and sub-translations are acceptable. 
   189 		 * @param params			An array of pointers to each parameter. 
   190 		 *							Integer parameters must have a pointer to the integer.
   191 		 *							Float parameters must have a pointer to a float.
   192 		 *							String parameters must be a string pointer.
   193 		 *							Char parameters must be a pointer to a char.
   194 		 *							Translation parameters fill multiple indexes in the 
   195 		 *							array.  For %T translations, the expected stack is:
   196 		 *							[phrase string pointer] [int target id pointer] [...] 
   197 		 *							Where [...] is the required parameters for the translation,  
   198 		 *							in the order expected by the phrase, not the phrase's 
   199 		 *							translation.  For example, say the format is:
   200 		 *							"%d %T" and the phrase's format is {1:s,2:f}, then the 
   201 		 *							parameter stack should be:
   202 		 *							int *, const char *, int *, const char *, float *
   203 		 *							The %t modifier is the same except the target id pointer 
   204 		 *							would be removed:
   205 		 *							int *, const char *, const char *, float *
   206 		 * @param numparams			Number of parameters in the params array.
   207 		 * @param pOutLength		Optional pointer filled with output length on success.
   208 		 * @param pFailPhrase		Optional pointer; on failure, is filled with NULL if the 
   209 		 *							failure was not due to a failed translation phrase.  
   210 		 *							Otherwise, it is filled with the given phrase name pointer 
   211 		 *							from the parameter stack.  Undefined on success.
   212 		 * @return					True on success.  False if the parameter stack was not 
   213 		 *							exactly the right length, or if a translation phrase 
   214 		 *							could not be found.
   215 		 */
   216 		virtual bool FormatString(
   217 			char *buffer,
   218 			size_t maxlength,
   219 			const char *format,
   220 			void **params,
   221 			unsigned int numparams,
   222 			size_t *pOutLength,
   223 			const char **pFailPhrase) =0;
   224 	};
   225 
   226 	/**
   227 	 * @brief Provides functions for translation.
   228 	 */
   229 	 class ITranslator : public SMInterface
   230 	{
   231 	public:
   232 		virtual const char *GetInterfaceName() =0;
   233 		virtual unsigned int GetInterfaceVersion() =0;
   234 	public:
   235 		/**
   236 		 * @brief Creates a new phrase collection object.
   237 		 *
   238 		 * @return			A new phrase collection object, which must be 
   239 		 *					destroyed via IPhraseCollection::Destroy() when 
   240 		 *					no longer needed.
   241 		 */
   242 		virtual IPhraseCollection *CreatePhraseCollection() =0;
   243 
   244 		/**
   245 		 * @brief Returns the server language.
   246 		 *
   247 		 * @return			Server language index.
   248 		 */
   249 		virtual unsigned int GetServerLanguage() =0;
   250 
   251 		/**
   252 		 * @brief Returns a client's language.
   253 		 *
   254 		 * @param client	Client index.
   255 		 * @return			Client language index, or server's if client's is 
   256 		 *					not known.
   257 		 */
   258 		virtual unsigned int GetClientLanguage(int client) =0;
   259 
   260 		/**
   261 		 * @brief Sets the global client SourceMod will use for assisted 
   262 		 * translations (that is, %t).
   263 		 *
   264 		 * @param index		Client index (0 for server).
   265 		 * @return			Old global client value.
   266 		 */
   267 		virtual int SetGlobalTarget(int index) =0;
   268 
   269 		/**
   270 		 * @brief Returns the global client SourceMod is currently using 
   271 		 * for assisted translations (that is, %t).
   272 		 *
   273 		 * @return			Global client index (0 for server).
   274 		 */
   275 		virtual int GetGlobalTarget() const =0;
   276 
   277 		/**
   278 		 * @brief Formats a phrase given a parameter stack.  The parameter 
   279 		 * stack size must exactly match the expected parameter count.  If 
   280 		 * this count is too small or too large, the format fails.
   281 		 *
   282 		 * Note: This is the same as IPhraseCollection::FormatString(), except 
   283 		 * that the IPhraseCollection parameter is explicit instead of implicit.
   284 		 *
   285 		 * @param buffer			Buffer to store formatted text.
   286 		 * @param maxlength			Maximum length of the buffer.
   287 		 * @param format			String containing format information.  
   288 		 *							This is equivalent to SourceMod's Format() 
   289 		 *							native, and sub-translations are acceptable. 
   290 		 * @param pPhrases			Optional phrase collection pointer to search for 
   291 		 *							phrases.
   292 		 * @param params			An array of pointers to each parameter. 
   293 		 *							Integer parameters must have a pointer to the integer.
   294 		 *							Float parameters must have a pointer to a float.
   295 		 *							String parameters must be a string pointer.
   296 		 *							Char parameters must be a pointer to a char.
   297 		 *							Translation parameters fill multiple indexes in the 
   298 		 *							array.  For %T translations, the expected stack is:
   299 		 *							[phrase string pointer] [int target id pointer] [...] 
   300 		 *							Where [...] is the required parameters for the translation,  
   301 		 *							in the order expected by the phrase, not the phrase's 
   302 		 *							translation.  For example, say the format is:
   303 		 *							"%d %T" and the phrase's format is {1:s,2:f}, then the 
   304 		 *							parameter stack should be:
   305 		 *							int *, const char *, int *, const char *, float *
   306 		 *							The %t modifier is the same except the target id pointer 
   307 		 *							would be removed:
   308 		 *							int *, const char *, const char *, float *
   309 		 * @param numparams			Number of parameters in the params array.
   310 		 * @param pOutLength		Optional pointer filled with output length on success.
   311 		 * @param pFailPhrase		Optional pointer; on failure, is filled with NULL if the 
   312 		 *							failure was not due to a failed translation phrase.  
   313 		 *							Otherwise, it is filled with the given phrase name pointer 
   314 		 *							from the parameter stack.  Undefined on success.
   315 		 * @return					True on success.  False if the parameter stack was not 
   316 		 *							exactly the right length, or if a translation phrase 
   317 		 *							could not be found.
   318 		 */
   319 		virtual bool FormatString(
   320 			char *buffer,
   321 			size_t maxlength,
   322 			const char *format,
   323 			IPhraseCollection *pPhrases,
   324 			void **params,
   325 			unsigned int numparams,
   326 			size_t *pOutLength,
   327 			const char **pFailPhrase) =0;
   328 
   329 		/**
   330 		 * @brief Get number of languages.
   331 		 *
   332 		 * @return                  Number of languages.
   333 		 */
   334 		virtual unsigned int GetLanguageCount() =0;
   335 
   336 		/**
   337 		 * @brief Find a language number by name.
   338 		 *
   339 		 * @param name              Language name.
   340 		 * @param index             Optional pointer to store language index.
   341 		 * @return                  True if found, false otherwise.
   342 		 */
   343 		virtual bool GetLanguageByName(const char *name, unsigned int *index) =0;
   344 
   345 		/**
   346 		 * @brief Retrieves info about a given language number.
   347 		 *
   348 		 * @param number            Language number.
   349 		 * @param code              Pointer to store the language code.
   350 		 * @param name              Pointer to store language name.
   351 		 * @return                  True if language number is valid, false otherwise.
   352 		 */
   353 		virtual bool GetLanguageInfo(unsigned int number, const char **code, const char **name) =0;
   354 	};
   355 }
   356 
   357 #endif //_INCLUDE_SOURCEMOD_TRANSLATOR_INTERFACE_H_
   358