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