3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #ifndef _INCLUDE_SOURCEMOD_TRANSLATOR_INTERFACE_H_
33 #define _INCLUDE_SOURCEMOD_TRANSLATOR_INTERFACE_H_
35 #include <IShareSys.h>
37 #define SMINTERFACE_TRANSLATOR_NAME "ITranslator"
38 #define SMINTERFACE_TRANSLATOR_VERSION 1
42 * @brief Defines interfaces related to translation files.
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.
52 #define SOURCEMOD_LANGUAGE_ENGLISH 0
55 * @brief For %T formats, specifies that the language should be that of the
56 * server and not a specific client.
58 #define SOURCEMOD_SERVER_LANGUAGE 0
61 * @brief Translation error codes.
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. */
73 * @brief Contains information about a translation phrase.
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
87 * @brief Represents a phrase file from SourceMod's "translations" folder.
93 * @brief Attempts to find a translation phrase in a phrase file.
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).
102 virtual TransError GetTranslation(
103 const char *szPhrase,
104 unsigned int lang_id,
105 Translation *pTrans) =0;
108 * @brief Returns the file name of this translation file.
112 virtual const char *GetFilename() =0;
116 * Represents a collection of phrase files.
118 class IPhraseCollection
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.
127 * This function will internally ignore dupliate additions but still
128 * return a valid pointer.
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
135 virtual IPhraseFile *AddPhraseFile(const char *filename) =0;
138 * @brief Returns the number of contained phrase files.
140 * @return Number of contained phrase files.
142 virtual unsigned int GetFileCount() =0;
145 * @brief Returns the pointer to a contained phrase file.
147 * @param file File index, from 0 to GetFileCount()-1.
148 * @return IPhraseFile pointer, or NULL if out of
151 virtual IPhraseFile *GetFile(unsigned int file) =0;
154 * @brief Destroys the phrase collection, freeing all internal
155 * resources and invalidating the object.
157 virtual void Destroy() =0;
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.
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.
171 virtual TransError FindTranslation(
174 Translation *pTrans) =0;
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.
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
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.
213 virtual bool FormatString(
218 unsigned int numparams,
220 const char **pFailPhrase) =0;
224 * @brief Provides functions for translation.
226 class ITranslator : public SMInterface
229 virtual const char *GetInterfaceName() =0;
230 virtual unsigned int GetInterfaceVersion() =0;
233 * @brief Creates a new phrase collection object.
235 * @return A new phrase collection object, which must be
236 * destroyed via IPhraseCollection::Destroy() when
239 virtual IPhraseCollection *CreatePhraseCollection() =0;
242 * @brief Returns the server language.
244 * @return Server language index.
246 virtual unsigned int GetServerLanguage() =0;
249 * @brief Returns a client's language.
251 * @param client Client index.
252 * @return Client language index, or server's if client's is
255 virtual unsigned int GetClientLanguage(int client) =0;
258 * @brief Sets the global client SourceMod will use for assisted
259 * translations (that is, %t).
261 * @param index Client index (0 for server).
262 * @return Old global client value.
264 virtual int SetGlobalTarget(int index) =0;
267 * @brief Returns the global client SourceMod is currently using
268 * for assisted translations (that is, %t).
270 * @return Global client index (0 for server).
272 virtual int GetGlobalTarget() const =0;
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.
279 * Note: This is the same as IPhraseCollection::FormatString(), except
280 * that the IPhraseCollection parameter is explicit instead of implicit.
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
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
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.
316 virtual bool FormatString(
320 IPhraseCollection *pPhrases,
322 unsigned int numparams,
324 const char **pFailPhrase) =0;
328 #endif //_INCLUDE_SOURCEMOD_TRANSLATOR_INTERFACE_H_