core/Translator.cpp
author David Anderson <dvander@alliedmods.net>
Sun Apr 13 06:06:27 2008 +0000 (2008-04-13)
changeset 1999 63aafc6ff212
parent 1952 b7aa2b626807
child 2042 4a2faafca477
permissions -rw-r--r--
added amb1538 - adt_stack
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include <stdarg.h>
ds@1952
    33
#include <stdlib.h>
ds@1952
    34
#include <ctype.h>
ds@1952
    35
#include "Translator.h"
ds@1952
    36
#include "Logger.h"
ds@1952
    37
#include "LibrarySys.h"
ds@1952
    38
#include "sm_stringutil.h"
ds@1952
    39
#include "sourcemod.h"
ds@1952
    40
#include "PlayerManager.h"
ds@1952
    41
ds@1952
    42
Translator g_Translator;
ds@1952
    43
CPhraseFile *g_pCorePhrases = NULL;
ds@1952
    44
unsigned int g_pCorePhraseID = 0;
ds@1952
    45
ds@1952
    46
struct trans_t
ds@1952
    47
{
ds@1952
    48
	int stridx;
ds@1952
    49
	int fmt_order;
ds@1952
    50
};
ds@1952
    51
ds@1952
    52
struct phrase_t
ds@1952
    53
{
ds@1952
    54
	int fmt_list;
ds@1952
    55
	unsigned int fmt_count;
ds@1952
    56
	unsigned int fmt_bytes;
ds@1952
    57
	int trans_tbl;
ds@1952
    58
	unsigned int translations;
ds@1952
    59
};
ds@1952
    60
ds@1952
    61
CPhraseFile::CPhraseFile(Translator *pTranslator, const char *file)
ds@1952
    62
{
ds@1952
    63
	m_pStringTab = pTranslator->GetStringTable();
ds@1952
    64
	m_pMemory = m_pStringTab->GetMemTable();
ds@1952
    65
	m_LangCount = pTranslator->GetLanguageCount();
ds@1952
    66
	m_File.assign(file);
ds@1952
    67
	m_pTranslator = pTranslator;
ds@1952
    68
	m_pPhraseLookup = NULL;
ds@1952
    69
}
ds@1952
    70
ds@1952
    71
CPhraseFile::~CPhraseFile()
ds@1952
    72
{
ds@1952
    73
	if (m_pPhraseLookup)
ds@1952
    74
	{
ds@1952
    75
		sm_trie_destroy(m_pPhraseLookup);
ds@1952
    76
	}
ds@1952
    77
}
ds@1952
    78
ds@1952
    79
void CPhraseFile::ParseError(const char *message, ...)
ds@1952
    80
{
ds@1952
    81
	char buffer[1024];
ds@1952
    82
	va_list ap;
ds@1952
    83
	va_start(ap, message);
ds@1952
    84
	vsnprintf(buffer, sizeof(buffer), message, ap);
ds@1952
    85
	va_end(ap);
ds@1952
    86
ds@1952
    87
	m_ParseError.assign(buffer);
ds@1952
    88
}
ds@1952
    89
ds@1952
    90
void CPhraseFile::ParseWarning(const char *message, ...)
ds@1952
    91
{
ds@1952
    92
	char buffer[1024];
ds@1952
    93
	va_list ap;
ds@1952
    94
	va_start(ap, message);
ds@1952
    95
	vsnprintf(buffer, sizeof(buffer), message, ap);
ds@1952
    96
	va_end(ap);
ds@1952
    97
ds@1952
    98
	if (!m_FileLogged)
ds@1952
    99
	{
ds@1952
   100
		g_Logger.LogError("[SM] Warning(s) encountered in translation file \"%s\"", m_File.c_str());
ds@1952
   101
		m_FileLogged = true;
ds@1952
   102
	}
ds@1952
   103
ds@1952
   104
	g_Logger.LogError("[SM] %s", message);
ds@1952
   105
}
ds@1952
   106
ds@1952
   107
void CPhraseFile::ReparseFile()
ds@1952
   108
{
ds@1952
   109
	if (m_pPhraseLookup)
ds@1952
   110
	{
ds@1952
   111
		sm_trie_destroy(m_pPhraseLookup);
ds@1952
   112
	}
ds@1952
   113
	m_pPhraseLookup = sm_trie_create();
ds@1952
   114
ds@1952
   115
	m_LangCount = m_pTranslator->GetLanguageCount();
ds@1952
   116
ds@1952
   117
	if (!m_LangCount)
ds@1952
   118
	{
ds@1952
   119
		return;
ds@1952
   120
	}
ds@1952
   121
ds@1952
   122
	SMCError err;
ds@1952
   123
	char path[PLATFORM_MAX_PATH];
ds@1952
   124
	g_SourceMod.BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "translations/%s", m_File.c_str());
ds@1952
   125
ds@1952
   126
	//backwards compatibility shim
ds@1952
   127
	/* :HACKHACK: Change .cfg/.txt and vice versa for compatibility */
ds@1952
   128
	if (!g_LibSys.PathExists(path))
ds@1952
   129
	{
ds@1952
   130
		if (m_File.compare("common.cfg") == 0)
ds@1952
   131
		{
ds@1952
   132
			UTIL_ReplaceAll(path, sizeof(path), "common.cfg", "common.phrases.txt");
ds@1952
   133
		} else if (strstr(path, ".cfg")) {
ds@1952
   134
			UTIL_ReplaceAll(path, sizeof(path), ".cfg", ".txt");
ds@1952
   135
		} else if (strstr(path, ".txt")) {
ds@1952
   136
			UTIL_ReplaceAll(path, sizeof(path), ".txt", ".cfg");
ds@1952
   137
		}
ds@1952
   138
	}
ds@1952
   139
ds@1952
   140
	SMCStates states;
ds@1952
   141
ds@1952
   142
	if ((err=textparsers->ParseFile_SMC(path, this, &states)) != SMCError_Okay)
ds@1952
   143
	{
ds@1952
   144
		const char *msg = textparsers->GetSMCErrorString(err);
ds@1952
   145
		if (!msg)
ds@1952
   146
		{
ds@1952
   147
			msg = m_ParseError.c_str();
ds@1952
   148
		}
ds@1952
   149
ds@1952
   150
		g_Logger.LogError("[SM] Fatal error encountered parsing translation file \"%s\"", m_File.c_str());
ds@1952
   151
		g_Logger.LogError("[SM] Error (line %d, column %d): %s", states.line, states.col, msg);
ds@1952
   152
	}
ds@1952
   153
}
ds@1952
   154
ds@1952
   155
void CPhraseFile::ReadSMC_ParseStart()
ds@1952
   156
{
ds@1952
   157
	m_CurPhrase = -1;
ds@1952
   158
	m_ParseState = PPS_None;
ds@1952
   159
	m_FileLogged = false;
ds@1952
   160
	m_LastPhraseString.clear();
ds@1952
   161
}
ds@1952
   162
ds@1952
   163
SMCResult CPhraseFile::ReadSMC_NewSection(const SMCStates *states, const char *name)
ds@1952
   164
{
ds@1952
   165
	bool recognized = false;
ds@1952
   166
	if (m_ParseState == PPS_None)
ds@1952
   167
	{
ds@1952
   168
		if (strcmp(name, "Phrases") == 0)
ds@1952
   169
		{
ds@1952
   170
			m_ParseState = PPS_Phrases;
ds@1952
   171
			recognized = true;
ds@1952
   172
		}
ds@1952
   173
	} 
ds@1952
   174
	else if (m_ParseState == PPS_Phrases) 
ds@1952
   175
	{
ds@1952
   176
		m_ParseState = PPS_InPhrase;
ds@1952
   177
		recognized = true;
ds@1952
   178
ds@1952
   179
		phrase_t *pPhrase;
ds@1952
   180
		m_CurPhrase = m_pMemory->CreateMem(sizeof(phrase_t), (void **)&pPhrase);
ds@1952
   181
ds@1952
   182
		/* Create the reverse lookup */
ds@1952
   183
		if (!sm_trie_insert(m_pPhraseLookup, name, reinterpret_cast<void *>(m_CurPhrase)))
ds@1952
   184
		{
ds@1952
   185
			ParseWarning("Skipping duplicate phrase \"%s\" on line %d.", name, states->line);
ds@1952
   186
			/* :IDEA: prevent memory waste by seeking backwards in memtable? */
ds@1952
   187
			m_CurPhrase = -1;
ds@1952
   188
		} 
ds@1952
   189
		else 
ds@1952
   190
		{
ds@1952
   191
			/* Initialize new phrase */
ds@1952
   192
			trans_t *pTrans;
ds@1952
   193
	
ds@1952
   194
			pPhrase->fmt_count = 0;
ds@1952
   195
			pPhrase->fmt_list = -1;
ds@1952
   196
ds@1952
   197
			int trans_tbl = m_pMemory->CreateMem(sizeof(trans_t) * m_LangCount, (void **)&pTrans);
ds@1952
   198
			pPhrase = (phrase_t *)m_pMemory->GetAddress(m_CurPhrase);
ds@1952
   199
			pPhrase->trans_tbl = trans_tbl;
ds@1952
   200
ds@1952
   201
			pPhrase->translations = 0;
ds@1952
   202
			pPhrase->fmt_bytes = 0;
ds@1952
   203
	
ds@1952
   204
			for (unsigned int i=0; i<m_LangCount; i++)
ds@1952
   205
			{
ds@1952
   206
				pTrans[i].stridx = -1;
ds@1952
   207
			}
ds@1952
   208
			m_LastPhraseString.assign(name);
ds@1952
   209
		}
ds@1952
   210
	} 
ds@1952
   211
	else if (m_ParseState == PPS_InPhrase) 
ds@1952
   212
	{
ds@1952
   213
		ParseError("Phrase sections may not have sub-sections");
ds@1952
   214
		return SMCResult_HaltFail;
ds@1952
   215
	}
ds@1952
   216
ds@1952
   217
	if (!recognized)
ds@1952
   218
	{
ds@1952
   219
		ParseWarning("Ignoring invalid section \"%s\" on line %d.", name, states->line);
ds@1952
   220
	}
ds@1952
   221
ds@1952
   222
	return SMCResult_Continue;
ds@1952
   223
}
ds@1952
   224
ds@1952
   225
SMCResult CPhraseFile::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
ds@1952
   226
{
ds@1952
   227
	/* See if we are ignoring a phrase */
ds@1952
   228
	if (m_CurPhrase == -1)
ds@1952
   229
	{
ds@1952
   230
		return SMCResult_Continue;
ds@1952
   231
	}
ds@1952
   232
ds@1952
   233
	phrase_t *pPhrase = (phrase_t *)m_pMemory->GetAddress(m_CurPhrase);
ds@1952
   234
	
ds@1952
   235
	if (key[0] == '#' && strcmp(key, "#format") == 0)
ds@1952
   236
	{
ds@1952
   237
		if (pPhrase->fmt_list != -1)
ds@1952
   238
		{
ds@1952
   239
			ParseWarning("Ignoring duplicated #format property on line %d", states->line);
ds@1952
   240
			return SMCResult_Continue;
ds@1952
   241
		}
ds@1952
   242
ds@1952
   243
		if (pPhrase->translations > 0)
ds@1952
   244
		{
ds@1952
   245
			ParseWarning("#format property should come before translations on line %d, ignoring", states->line);
ds@1952
   246
			return SMCResult_Continue;
ds@1952
   247
		}
ds@1952
   248
ds@1952
   249
		/* Do an initial browsing for error checking and what not */
ds@1952
   250
		enum ParseStates
ds@1952
   251
		{
ds@1952
   252
			Parse_None,
ds@1952
   253
			Parse_Index,
ds@1952
   254
			Parse_Format,
ds@1952
   255
		};
ds@1952
   256
ds@1952
   257
		ParseStates state = Parse_None;
ds@1952
   258
ds@1952
   259
		const char *old_value = value;
ds@1952
   260
		const char *last_value = value;
ds@1952
   261
		while (*value != '\0')
ds@1952
   262
		{
ds@1952
   263
			if (state == Parse_None)
ds@1952
   264
			{
ds@1952
   265
				if (*value == '{')
ds@1952
   266
				{
ds@1952
   267
					pPhrase->fmt_count++;
ds@1952
   268
					state = Parse_Index;
ds@1952
   269
				} 
ds@1952
   270
				else if (*value == ',') 
ds@1952
   271
				{
ds@1952
   272
					/* Do nothing */
ds@1952
   273
				} 
ds@1952
   274
				else 
ds@1952
   275
				{
ds@1952
   276
					unsigned int bytes = textparsers->GetUTF8CharBytes(value);
ds@1952
   277
					if (bytes != 1 || !isalpha(*value))
ds@1952
   278
					{
ds@1952
   279
						ParseWarning("Invalid token '%c' in #format property on line %d.", *value, states->line);
ds@1952
   280
					}
ds@1952
   281
				}
ds@1952
   282
			} 
ds@1952
   283
			else if (state == Parse_Index) 
ds@1952
   284
			{
ds@1952
   285
				if (*value == ':')
ds@1952
   286
				{
ds@1952
   287
					state = Parse_Format;
ds@1952
   288
					if (value - last_value >= 15)
ds@1952
   289
					{
ds@1952
   290
						ParseWarning("Too many digits in format index on line %d, phrase will be ignored.", states->line);
ds@1952
   291
						m_CurPhrase = -1;
ds@1952
   292
						return SMCResult_Continue;
ds@1952
   293
					}
ds@1952
   294
				} 
ds@1952
   295
				else 
ds@1952
   296
				{
ds@1952
   297
					unsigned int bytes = textparsers->GetUTF8CharBytes(value);
ds@1952
   298
					if (bytes != 1 || !isdigit(*value))
ds@1952
   299
					{
ds@1952
   300
						ParseWarning("Token '%c' in #format property on line %d is not a digit, phrase will be ignored.",
ds@1952
   301
									*value,
ds@1952
   302
									states->line);
ds@1952
   303
						m_CurPhrase = -1;
ds@1952
   304
						return SMCResult_Continue;
ds@1952
   305
					}
ds@1952
   306
				}
ds@1952
   307
			} 
ds@1952
   308
			else if (state == Parse_Format) 
ds@1952
   309
			{
ds@1952
   310
				if (*value == '}')
ds@1952
   311
				{
ds@1952
   312
					state = Parse_None;
ds@1952
   313
					last_value = value + 1;
ds@1952
   314
				}
ds@1952
   315
			}
ds@1952
   316
			value++;
ds@1952
   317
		}
ds@1952
   318
ds@1952
   319
		if (state != Parse_None)
ds@1952
   320
		{
ds@1952
   321
			/* Moose clam cow. */
ds@1952
   322
			ParseWarning("Unterminated format string on line %d, phrase will be ignored.", states->line);
ds@1952
   323
			m_CurPhrase = -1;
ds@1952
   324
			return SMCResult_Continue;
ds@1952
   325
		}
ds@1952
   326
		value = old_value;
ds@1952
   327
ds@1952
   328
		/* Allocate the format table */
ds@1952
   329
		char fmt_buf[16];
ds@1952
   330
		int *fmt_list;
ds@1952
   331
		int tmp = m_pMemory->CreateMem(sizeof(int) * pPhrase->fmt_count, (void **)&fmt_list);
ds@1952
   332
ds@1952
   333
		/* Update the phrase pointer in case it changed */
ds@1952
   334
		pPhrase = (phrase_t *)m_pMemory->GetAddress(m_CurPhrase);
ds@1952
   335
		pPhrase->fmt_list = tmp;
ds@1952
   336
ds@1952
   337
		/* Initialize */
ds@1952
   338
		for (size_t i=0; i<pPhrase->fmt_count; i++)
ds@1952
   339
		{
ds@1952
   340
			fmt_list[i] = -1;
ds@1952
   341
		}
ds@1952
   342
ds@1952
   343
		/* Now we need to read again... this time into the format buffer */
ds@1952
   344
		const char *in_ptr = value;
ds@1952
   345
		const char *idx_ptr = NULL;
ds@1952
   346
		char *out_ptr = NULL;
ds@1952
   347
		unsigned int cur_idx = 0;
ds@1952
   348
		state = Parse_None;
ds@1952
   349
		while (*in_ptr != '\0')
ds@1952
   350
		{
ds@1952
   351
			if (state == Parse_None)
ds@1952
   352
			{
ds@1952
   353
				if (*in_ptr == '{')
ds@1952
   354
				{
ds@1952
   355
					state = Parse_Index;
ds@1952
   356
					idx_ptr = NULL;
ds@1952
   357
				}
ds@1952
   358
			}
ds@1952
   359
			else if (state == Parse_Index) 
ds@1952
   360
			{
ds@1952
   361
				if (*in_ptr == ':')
ds@1952
   362
				{
ds@1952
   363
					/* Check the number! */
ds@1952
   364
					if (!idx_ptr)
ds@1952
   365
					{
ds@1952
   366
						ParseWarning("Format property contains unindexed format string on line %d, phrase will be ignored.", states->line);
ds@1952
   367
						m_CurPhrase = -1;
ds@1952
   368
						return SMCResult_Continue;
ds@1952
   369
					}
ds@1952
   370
					long idx = strtol(idx_ptr, NULL, 10);
ds@1952
   371
					if (idx < 1 || idx > (long)pPhrase->fmt_count)
ds@1952
   372
					{
ds@1952
   373
						ParseWarning("Format property contains invalid index '%d' on line %d, phrase will be ignored.", idx, states->line);
ds@1952
   374
						m_CurPhrase = -1;
ds@1952
   375
						return SMCResult_Continue;
ds@1952
   376
					}
ds@1952
   377
					else if (fmt_list[idx - 1] != -1) 
ds@1952
   378
					{
ds@1952
   379
						ParseWarning("Format property contains duplicated index '%d' on line %d, phrase will be ignored.", idx, states->line);
ds@1952
   380
						m_CurPhrase = -1;
ds@1952
   381
						return SMCResult_Continue;
ds@1952
   382
					}
ds@1952
   383
					cur_idx = (unsigned int)idx;
ds@1952
   384
					state = Parse_Format;
ds@1952
   385
					out_ptr = NULL;
ds@1952
   386
				}
ds@1952
   387
				else if (!idx_ptr) 
ds@1952
   388
				{
ds@1952
   389
					idx_ptr = in_ptr;
ds@1952
   390
				}
ds@1952
   391
			}
ds@1952
   392
			else if (state == Parse_Format) 
ds@1952
   393
			{
ds@1952
   394
				if (*in_ptr == '}')
ds@1952
   395
				{
ds@1952
   396
					if (!out_ptr)
ds@1952
   397
					{
ds@1952
   398
						ParseWarning("Format property contains empty format string on line %d, phrase will be ignored.", states->line);
ds@1952
   399
						m_CurPhrase = -1;
ds@1952
   400
						return SMCResult_Continue;
ds@1952
   401
					}
ds@1952
   402
					*out_ptr = '\0';
ds@1952
   403
					state = Parse_None;
ds@1952
   404
					/* Now, add this to our table */
ds@1952
   405
					int tmp_idx = m_pStringTab->AddString(fmt_buf);
ds@1952
   406
					/* Update pointers and update necessary variables */
ds@1952
   407
					pPhrase = (phrase_t *)m_pMemory->GetAddress(m_CurPhrase);
ds@1952
   408
					pPhrase->fmt_bytes += strlen(fmt_buf);
ds@1952
   409
					fmt_list = (int *)m_pMemory->GetAddress(pPhrase->fmt_list);
ds@1952
   410
					fmt_list[cur_idx - 1] = tmp_idx;
ds@1952
   411
				} 
ds@1952
   412
				else 
ds@1952
   413
				{
ds@1952
   414
					if (!out_ptr)
ds@1952
   415
					{
ds@1952
   416
						out_ptr = fmt_buf;
ds@1952
   417
						*out_ptr++ = '%';
ds@1952
   418
					}
ds@1952
   419
					/* Check length ... */
ds@1952
   420
					if ((unsigned)(out_ptr - fmt_buf) >= sizeof(fmt_buf) - 1)
ds@1952
   421
					{
ds@1952
   422
						ParseWarning("Format property contains format string that exceeds maximum length on line %d, phrase will be ignored.",
ds@1952
   423
									 states->line);
ds@1952
   424
						m_CurPhrase = -1;
ds@1952
   425
						return SMCResult_Continue;
ds@1952
   426
					}
ds@1952
   427
					*out_ptr++ = *in_ptr;
ds@1952
   428
				}
ds@1952
   429
			}
ds@1952
   430
			in_ptr++;
ds@1952
   431
		}
ds@1952
   432
ds@1952
   433
		/* If we've gotten here, we only accepted unique indexes in a range.
ds@1952
   434
		 * Therefore, the list has to be completely filled.  Double check anyway.
ds@1952
   435
		 */
ds@1952
   436
		for (size_t i=0; i<pPhrase->fmt_count; i++)
ds@1952
   437
		{
ds@1952
   438
			if (fmt_list[i] == -1)
ds@1952
   439
			{
ds@1952
   440
				ParseWarning("Format property contains no string for index %d on line %d, phrase will be ignored.",
ds@1952
   441
							 i + 1,
ds@1952
   442
							 states->line);
ds@1952
   443
				m_CurPhrase = -1;
ds@1952
   444
				return SMCResult_Continue;
ds@1952
   445
			}
ds@1952
   446
		}
ds@1952
   447
	} 
ds@1952
   448
	else 
ds@1952
   449
	{
ds@1952
   450
		size_t len = strlen(key);
ds@1952
   451
		if (len != 2)
ds@1952
   452
		{
ds@1952
   453
			ParseWarning("Ignoring translation to invalid language \"%s\" on line %d.", key, states->line);
ds@1952
   454
			return SMCResult_Continue;
ds@1952
   455
		}
ds@1952
   456
ds@1952
   457
		unsigned int lang;
ds@1952
   458
		if (!m_pTranslator->GetLanguageByCode(key, &lang))
ds@1952
   459
		{
ds@1952
   460
			/* Ignore if we don't have a language.
ds@1952
   461
			 * :IDEA: issue a one-time alert?
ds@1952
   462
			 */
ds@1952
   463
			return SMCResult_Continue;
ds@1952
   464
		}
ds@1952
   465
ds@1952
   466
		/* See how many bytes we need for this string, then allocate.
ds@1952
   467
		 * NOTE: THIS SHOULD GUARANTEE THAT WE DO NOT NEED TO NEED TO SIZE CHECK 
ds@1952
   468
		 */
ds@1952
   469
		len = strlen(value) + pPhrase->fmt_bytes + 1;
ds@1952
   470
		char *out_buf;
ds@1952
   471
		int out_idx;
ds@1952
   472
ds@1952
   473
		out_idx = m_pMemory->CreateMem(len, (void **)&out_buf);
ds@1952
   474
ds@1952
   475
		/* Update pointer */
ds@1952
   476
		pPhrase = (phrase_t *)m_pMemory->GetAddress(m_CurPhrase);
ds@1952
   477
ds@1952
   478
		int *fmt_order;
ds@1952
   479
		int *fmt_list = (int *)m_pMemory->GetAddress(pPhrase->fmt_list);
ds@1952
   480
		trans_t *pTrans = (trans_t *)m_pMemory->GetAddress(pPhrase->trans_tbl);
ds@1952
   481
ds@1952
   482
		pTrans = &pTrans[lang];
ds@1952
   483
		pTrans->stridx = out_idx;
ds@1952
   484
ds@1952
   485
		bool params[MAX_TRANSLATE_PARAMS];
ds@1952
   486
ds@1952
   487
		/* Build the format order list, if necessary */
ds@1952
   488
		if (fmt_list)
ds@1952
   489
		{
ds@1952
   490
			int tmp = m_pMemory->CreateMem(pPhrase->fmt_count * sizeof(int), (void **)&fmt_order);
ds@1952
   491
			
ds@1952
   492
			/* Update pointers */
ds@1952
   493
			pPhrase = (phrase_t *)m_pMemory->GetAddress(m_CurPhrase);
ds@1952
   494
			pTrans = (trans_t *)m_pMemory->GetAddress(pPhrase->trans_tbl);
ds@1952
   495
			fmt_list = (int *)m_pMemory->GetAddress(pPhrase->fmt_list);
ds@1952
   496
			out_buf = (char *)m_pMemory->GetAddress(out_idx);
ds@1952
   497
			pTrans = &pTrans[lang];
ds@1952
   498
ds@1952
   499
			/* Now it's safe to save the index */
ds@1952
   500
			pTrans->fmt_order = tmp;
ds@1952
   501
ds@1952
   502
			for (unsigned int i=0; i<pPhrase->fmt_count; i++)
ds@1952
   503
			{
ds@1952
   504
				fmt_order[i] = -1;
ds@1952
   505
			}
ds@1952
   506
			memset(&params[0], 0, sizeof(params));
ds@1952
   507
		}
ds@1952
   508
ds@1952
   509
		const char *in_ptr = value;
ds@1952
   510
		char *out_ptr = out_buf;
ds@1952
   511
		unsigned int order_idx = 0;
ds@1952
   512
		while (*in_ptr != '\0')
ds@1952
   513
		{
ds@1952
   514
			if (*in_ptr == '\\')
ds@1952
   515
			{
ds@1952
   516
				switch (*(in_ptr + 1))
ds@1952
   517
				{
ds@1952
   518
				case '\n':
ds@1952
   519
					*out_ptr++ = '\n';
ds@1952
   520
					break;
ds@1952
   521
				case '\t':
ds@1952
   522
					*out_ptr++ = '\t';
ds@1952
   523
					break;
ds@1952
   524
				case '\r':
ds@1952
   525
					*out_ptr++ = '\r';
ds@1952
   526
					break;
ds@1952
   527
				case '{':
ds@1952
   528
					*out_ptr++ = '{';
ds@1952
   529
					break;
ds@1952
   530
				default:
ds@1952
   531
					/* Copy both bytes since we don't know what's going on */
ds@1952
   532
					*out_ptr++ = *in_ptr++;
ds@1952
   533
					*out_ptr++ = *in_ptr;
ds@1952
   534
					break;
ds@1952
   535
				}
ds@1952
   536
				/* Skip past the last byte read */
ds@1952
   537
				in_ptr++;
ds@1952
   538
			} 
ds@1952
   539
			else if (*in_ptr == '{' && fmt_list != NULL) 
ds@1952
   540
			{
ds@1952
   541
				/* Search for parameters if this is a formatted string */
ds@1952
   542
				const char *scrap_in_point = in_ptr;
ds@1952
   543
				const char *digit_start = ++in_ptr;
ds@1952
   544
				unsigned int bytes;
ds@1952
   545
				while (*in_ptr != '\0')
ds@1952
   546
				{
ds@1952
   547
					bytes = textparsers->GetUTF8CharBytes(in_ptr);
ds@1952
   548
					if (bytes != 1)
ds@1952
   549
					{
ds@1952
   550
						goto scrap_point;
ds@1952
   551
					}
ds@1952
   552
					if (*in_ptr == '}')
ds@1952
   553
					{
ds@1952
   554
						/* Did we get an index? */
ds@1952
   555
						if (in_ptr == digit_start)
ds@1952
   556
						{
ds@1952
   557
							goto scrap_point;
ds@1952
   558
						}
ds@1952
   559
						/* Is it valid? */
ds@1952
   560
						long idx = strtol(digit_start, NULL, 10);
ds@1952
   561
						if (idx < 1 || idx > (int)pPhrase->fmt_count)
ds@1952
   562
						{
ds@1952
   563
							goto scrap_point;
ds@1952
   564
						}
ds@1952
   565
						if (params[idx-1])
ds@1952
   566
						{
ds@1952
   567
							goto scrap_point;
ds@1952
   568
						}
ds@1952
   569
ds@1952
   570
						/* We're safe to insert the string.  First mark order. */
ds@1952
   571
						fmt_order[order_idx++] = (int)idx - 1;
ds@1952
   572
						/* Now concatenate */
ds@1952
   573
						out_ptr += sprintf(out_ptr, "%s", m_pStringTab->GetString(fmt_list[idx-1]));
ds@1952
   574
						/* Mark as used */
ds@1952
   575
						params[idx-1] = true;
ds@1952
   576
ds@1952
   577
						goto cont_loop;
ds@1952
   578
					}
ds@1952
   579
					in_ptr++;
ds@1952
   580
				}
ds@1952
   581
scrap_point:
ds@1952
   582
				/* Pretend none of this ever happened.  Move along now! */
ds@1952
   583
				in_ptr = scrap_in_point;
ds@1952
   584
				*out_ptr++ = *in_ptr;
ds@1952
   585
			} else {
ds@1952
   586
				*out_ptr++ = *in_ptr;
ds@1952
   587
			}
ds@1952
   588
cont_loop:
ds@1952
   589
			in_ptr++;
ds@1952
   590
		}
ds@1952
   591
		*out_ptr = '\0';
ds@1952
   592
		pPhrase->translations++;
ds@1952
   593
	}
ds@1952
   594
ds@1952
   595
	return SMCResult_Continue;
ds@1952
   596
}
ds@1952
   597
ds@1952
   598
SMCResult CPhraseFile::ReadSMC_LeavingSection(const SMCStates *states)
ds@1952
   599
{
ds@1952
   600
	if (m_ParseState == PPS_InPhrase)
ds@1952
   601
	{
ds@1952
   602
		if (m_CurPhrase == -1 && m_LastPhraseString.size())
ds@1952
   603
		{
ds@1952
   604
			sm_trie_delete(m_pPhraseLookup, m_LastPhraseString.c_str());
ds@1952
   605
		}
ds@1952
   606
		m_CurPhrase = -1;
ds@1952
   607
		m_ParseState = PPS_Phrases;
ds@1952
   608
		m_LastPhraseString.assign("");
ds@1952
   609
	} 
ds@1952
   610
	else if (m_ParseState == PPS_Phrases) 
ds@1952
   611
	{
ds@1952
   612
		m_ParseState = PPS_None;
ds@1952
   613
	}
ds@1952
   614
ds@1952
   615
	return SMCResult_Continue;
ds@1952
   616
}
ds@1952
   617
ds@1952
   618
void CPhraseFile::ReadSMC_ParseEnd(bool halted, bool failed)
ds@1952
   619
{
ds@1952
   620
	/* Check to see if we have any dangling phrases that weren't completed, and scrap them */
ds@1952
   621
	if ((halted || failed) && m_LastPhraseString.size())
ds@1952
   622
	{
ds@1952
   623
		sm_trie_delete(m_pPhraseLookup, m_LastPhraseString.c_str());
ds@1952
   624
	}
ds@1952
   625
}
ds@1952
   626
ds@1952
   627
TransError CPhraseFile::GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans)
ds@1952
   628
{
ds@1952
   629
	if (lang_id >= m_LangCount)
ds@1952
   630
	{
ds@1952
   631
		return Trans_BadLanguage;
ds@1952
   632
	}
ds@1952
   633
ds@1952
   634
	void *object;
ds@1952
   635
	if (!sm_trie_retrieve(m_pPhraseLookup, szPhrase, &object))
ds@1952
   636
	{
ds@1952
   637
		return Trans_BadPhrase;
ds@1952
   638
	}
ds@1952
   639
ds@1952
   640
	phrase_t *pPhrase = (phrase_t *)m_pMemory->GetAddress(reinterpret_cast<int>(object));
ds@1952
   641
	trans_t *trans = (trans_t *)m_pMemory->GetAddress(pPhrase->trans_tbl);
ds@1952
   642
ds@1952
   643
	trans = &trans[lang_id];
ds@1952
   644
ds@1952
   645
	if (trans->stridx == -1)
ds@1952
   646
	{
ds@1952
   647
		return Trans_BadPhraseLanguage;
ds@1952
   648
	}
ds@1952
   649
ds@1952
   650
	pTrans->fmt_count = pPhrase->fmt_count;
ds@1952
   651
	pTrans->fmt_order = pTrans->fmt_count > 0 ? (int *)m_pMemory->GetAddress(trans->fmt_order) : NULL;
ds@1952
   652
	pTrans->szPhrase = m_pStringTab->GetString(trans->stridx);
ds@1952
   653
ds@1952
   654
	return Trans_Okay;
ds@1952
   655
}
ds@1952
   656
ds@1952
   657
const char *CPhraseFile::GetFilename()
ds@1952
   658
{
ds@1952
   659
	return m_File.c_str();
ds@1952
   660
}
ds@1952
   661
ds@1952
   662
/**************************
ds@1952
   663
 ** MAIN TRANSLATOR CODE **
ds@1952
   664
 **************************/
ds@1952
   665
ds@1952
   666
Translator::Translator() : m_ServerLang(LANGUAGE_ENGLISH)
ds@1952
   667
{
ds@1952
   668
	m_pStringTab = new BaseStringTable(2048);
ds@1952
   669
	m_pLCodeLookup = sm_trie_create();
ds@1952
   670
	strncopy(m_InitialLang, "en", sizeof(m_InitialLang));
ds@1952
   671
}
ds@1952
   672
ds@1952
   673
Translator::~Translator()
ds@1952
   674
{
ds@1952
   675
	for (size_t i=0; i<m_Files.size(); i++)
ds@1952
   676
	{
ds@1952
   677
		delete m_Files[i];
ds@1952
   678
	}
ds@1952
   679
ds@1952
   680
	for (size_t i=0; i<m_Languages.size(); i++)
ds@1952
   681
	{
ds@1952
   682
		delete m_Languages[i];
ds@1952
   683
	}
ds@1952
   684
ds@1952
   685
	sm_trie_destroy(m_pLCodeLookup);
ds@1952
   686
ds@1952
   687
	delete m_pStringTab;
ds@1952
   688
}
ds@1952
   689
ds@1952
   690
ConfigResult Translator::OnSourceModConfigChanged(const char *key, 
ds@1952
   691
									  const char *value, 
ds@1952
   692
									  ConfigSource source, 
ds@1952
   693
									  char *error, 
ds@1952
   694
									  size_t maxlength)
ds@1952
   695
{
ds@1952
   696
	if (strcasecmp(key, "ServerLang") == 0)
ds@1952
   697
	{
ds@1952
   698
		if (source == ConfigSource_Console)
ds@1952
   699
		{
ds@1952
   700
			unsigned int index;
ds@1952
   701
			if (!GetLanguageByCode(value, &index))
ds@1952
   702
			{
ds@1952
   703
				UTIL_Format(error, maxlength, "Language code \"%s\" is not registered", value);
ds@1952
   704
				return ConfigResult_Reject;
ds@1952
   705
			}
ds@1952
   706
ds@1952
   707
			m_ServerLang = index;
ds@1952
   708
		} else {
ds@1952
   709
			strncopy(m_InitialLang, value, sizeof(m_InitialLang));
ds@1952
   710
		}
ds@1952
   711
ds@1952
   712
		return ConfigResult_Accept;
ds@1952
   713
	}
ds@1952
   714
ds@1952
   715
	return ConfigResult_Ignore;
ds@1952
   716
}
ds@1952
   717
ds@1952
   718
void Translator::OnSourceModLevelChange(const char *mapName)
ds@1952
   719
{
ds@1952
   720
	/* Refresh language stuff */
ds@1952
   721
	char path[PLATFORM_MAX_PATH];
ds@1952
   722
	g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "configs/languages.cfg");
ds@1952
   723
	RebuildLanguageDatabase(path);
ds@1952
   724
}
ds@1952
   725
ds@1952
   726
void Translator::OnSourceModAllInitialized()
ds@1952
   727
{
ds@1952
   728
	AddLanguage("en", "English");
ds@1952
   729
ds@1952
   730
	unsigned int id;
ds@1952
   731
ds@1952
   732
	id = FindOrAddPhraseFile("core.phrases.txt");
ds@1952
   733
	g_pCorePhraseID = id;
ds@1952
   734
	g_pCorePhrases = GetFileByIndex(id);
ds@1952
   735
}
ds@1952
   736
ds@1952
   737
bool Translator::GetLanguageByCode(const char *code, unsigned int *index)
ds@1952
   738
{
ds@1952
   739
	void *_index;
ds@1952
   740
ds@1952
   741
	if (!sm_trie_retrieve(m_pLCodeLookup, code, &_index))
ds@1952
   742
	{
ds@1952
   743
		return false;
ds@1952
   744
	}
ds@1952
   745
ds@1952
   746
	if (index)
ds@1952
   747
	{
ds@1952
   748
		*index = reinterpret_cast<unsigned int>(_index);
ds@1952
   749
	}
ds@1952
   750
ds@1952
   751
	return true;
ds@1952
   752
}
ds@1952
   753
ds@1952
   754
bool Translator::GetLanguageByName(const char *name, unsigned int *index)
ds@1952
   755
{
ds@1952
   756
	CVector<Language *>::iterator iter;
ds@1952
   757
	unsigned int id = 0;
ds@1952
   758
ds@1952
   759
	for (iter=m_Languages.begin(); iter!=m_Languages.end(); iter++, id++)
ds@1952
   760
	{
ds@1952
   761
		if (strcasecmp(m_pStringTab->GetString((*iter)->m_FullName), name) == 0)
ds@1952
   762
		{
ds@1952
   763
			break;
ds@1952
   764
		}
ds@1952
   765
	}
ds@1952
   766
ds@1952
   767
	if (iter == m_Languages.end())
ds@1952
   768
	{
ds@1952
   769
		return false;
ds@1952
   770
	}
ds@1952
   771
ds@1952
   772
	if (index)
ds@1952
   773
	{
ds@1952
   774
		*index = id;
ds@1952
   775
	}
ds@1952
   776
ds@1952
   777
	return true;
ds@1952
   778
}
ds@1952
   779
ds@1952
   780
unsigned int Translator::GetLanguageCount()
ds@1952
   781
{
ds@1952
   782
	return (unsigned int)m_Languages.size();
ds@1952
   783
}
ds@1952
   784
ds@1952
   785
BaseStringTable *Translator::GetStringTable()
ds@1952
   786
{
ds@1952
   787
	return m_pStringTab;
ds@1952
   788
}
ds@1952
   789
ds@1952
   790
unsigned int Translator::FindOrAddPhraseFile(const char *phrase_file)
ds@1952
   791
{
ds@1952
   792
	for (size_t i=0; i<m_Files.size(); i++)
ds@1952
   793
	{
ds@1952
   794
		if (strcmp(m_Files[i]->GetFilename(), phrase_file) == 0)
ds@1952
   795
		{
ds@1952
   796
			return (unsigned int)i;
ds@1952
   797
		}
ds@1952
   798
	}
ds@1952
   799
ds@1952
   800
	CPhraseFile *pFile = new CPhraseFile(this, phrase_file);
ds@1952
   801
	unsigned int idx = (unsigned int)m_Files.size();
ds@1952
   802
	
ds@1952
   803
	m_Files.push_back(pFile);
ds@1952
   804
ds@1952
   805
	pFile->ReparseFile();
ds@1952
   806
ds@1952
   807
	return idx;
ds@1952
   808
}
ds@1952
   809
ds@1952
   810
void Translator::RebuildLanguageDatabase(const char *lang_header_file)
ds@1952
   811
{
ds@1952
   812
	/* Erase everything we have */
ds@1952
   813
	sm_trie_destroy(m_pLCodeLookup);
ds@1952
   814
	m_pLCodeLookup = sm_trie_create();
ds@1952
   815
	m_pStringTab->Reset();
ds@1952
   816
ds@1952
   817
	for (size_t i=0; i<m_Languages.size(); i++)
ds@1952
   818
	{
ds@1952
   819
		delete m_Languages[i];
ds@1952
   820
	}
ds@1952
   821
	m_Languages.clear();
ds@1952
   822
ds@1952
   823
	/* Start anew */
ds@1952
   824
	SMCError err;
ds@1952
   825
	SMCStates states;
ds@1952
   826
	if ((err=textparsers->ParseFile_SMC(lang_header_file, this, &states)) != SMCError_Okay)
ds@1952
   827
	{
ds@1952
   828
		const char *str_err = textparsers->GetSMCErrorString(err);
ds@1952
   829
		if (!str_err)
ds@1952
   830
		{
ds@1952
   831
			str_err = m_CustomError.c_str();
ds@1952
   832
		}
ds@1952
   833
ds@1952
   834
		g_Logger.LogError("[SM] Failed to parse language header file: \"%s\"", lang_header_file);
ds@1952
   835
		g_Logger.LogError("[SM] Parse error (line %d, column %d): %s", states.line, states.col, str_err);
ds@1952
   836
	}
ds@1952
   837
ds@1952
   838
	void *serverLang;
ds@1952
   839
ds@1952
   840
	if (!sm_trie_retrieve(m_pLCodeLookup, m_InitialLang, &serverLang))
ds@1952
   841
	{
ds@1952
   842
		g_Logger.LogError("Server language was set to bad language \"%s\" -- reverting to English", m_InitialLang);
ds@1952
   843
ds@1952
   844
		strncopy(m_InitialLang, "en", sizeof(m_InitialLang));
ds@1952
   845
		m_ServerLang = LANGUAGE_ENGLISH;
ds@1952
   846
	}
ds@1952
   847
ds@1952
   848
	m_ServerLang = reinterpret_cast<unsigned int>(serverLang);
ds@1952
   849
ds@1952
   850
	if (!m_Languages.size())
ds@1952
   851
	{
ds@1952
   852
		g_Logger.LogError("[SM] Fatal error, no languages found! Translation will not work.");
ds@1952
   853
	}
ds@1952
   854
ds@1952
   855
	for (size_t i=0; i<m_Files.size(); i++)
ds@1952
   856
	{
ds@1952
   857
		m_Files[i]->ReparseFile();
ds@1952
   858
	}
ds@1952
   859
}
ds@1952
   860
ds@1952
   861
void Translator::ReadSMC_ParseStart()
ds@1952
   862
{
ds@1952
   863
	m_InLanguageSection = false;
ds@1952
   864
	m_CustomError.clear();
ds@1952
   865
}
ds@1952
   866
ds@1952
   867
SMCResult Translator::ReadSMC_NewSection(const SMCStates *states, const char *name)
ds@1952
   868
{
ds@1952
   869
	if (!m_InLanguageSection)
ds@1952
   870
	{
ds@1952
   871
		if (strcmp(name, "Languages") == 0)
ds@1952
   872
		{
ds@1952
   873
			m_InLanguageSection = true;
ds@1952
   874
		}
ds@1952
   875
	}
ds@1952
   876
ds@1952
   877
	if (!m_InLanguageSection)
ds@1952
   878
	{
ds@1952
   879
		g_Logger.LogError("[SM] Warning: Unrecognized section \"%s\" in languages.cfg", name);
ds@1952
   880
	}
ds@1952
   881
ds@1952
   882
	return SMCResult_Continue;
ds@1952
   883
}
ds@1952
   884
ds@1952
   885
SMCResult Translator::ReadSMC_LeavingSection(const SMCStates *states)
ds@1952
   886
{
ds@1952
   887
	m_InLanguageSection = false;
ds@1952
   888
ds@1952
   889
	return SMCResult_Continue;
ds@1952
   890
}
ds@1952
   891
ds@1952
   892
SMCResult Translator::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
ds@1952
   893
{
ds@1952
   894
	size_t len = strlen(key);
ds@1952
   895
ds@1952
   896
	if (len != 2)
ds@1952
   897
	{
ds@1952
   898
		g_Logger.LogError("[SM] Warning encountered parsing languages.cfg file.");
ds@1952
   899
		g_Logger.LogError("[SM] Invalid language code \"%s\" is being ignored.", key);
ds@1952
   900
	}
ds@1952
   901
ds@1952
   902
	AddLanguage(key, value);
ds@1952
   903
ds@1952
   904
	return SMCResult_Continue;
ds@1952
   905
}
ds@1952
   906
ds@1952
   907
bool Translator::AddLanguage(const char *langcode, const char *description)
ds@1952
   908
{
ds@1952
   909
	if (sm_trie_retrieve(m_pLCodeLookup, langcode, NULL))
ds@1952
   910
	{
ds@1952
   911
		return false;
ds@1952
   912
	}
ds@1952
   913
ds@1952
   914
	Language *pLanguage = new Language;
ds@1952
   915
	unsigned int idx = m_Languages.size();
ds@1952
   916
ds@1952
   917
	pLanguage->m_code2[0] = langcode[0];
ds@1952
   918
	pLanguage->m_code2[1] = langcode[1];
ds@1952
   919
	pLanguage->m_code2[2] = langcode[2];
ds@1952
   920
	pLanguage->m_FullName = m_pStringTab->AddString(description);
ds@1952
   921
ds@1952
   922
	sm_trie_insert(m_pLCodeLookup, langcode, reinterpret_cast<void *>(idx));
ds@1952
   923
ds@1952
   924
	m_Languages.push_back(pLanguage);
ds@1952
   925
ds@1952
   926
	return true;
ds@1952
   927
}
ds@1952
   928
ds@1952
   929
CPhraseFile *Translator::GetFileByIndex(unsigned int index)
ds@1952
   930
{
ds@1952
   931
	if (index >= m_Files.size())
ds@1952
   932
	{
ds@1952
   933
		return NULL;
ds@1952
   934
	}
ds@1952
   935
ds@1952
   936
	return m_Files[index];
ds@1952
   937
}
ds@1952
   938
ds@1952
   939
size_t Translator::Translate(char *buffer, size_t maxlength, void **params, const Translation *pTrans)
ds@1952
   940
{
ds@1952
   941
	void *new_params[MAX_TRANSLATE_PARAMS];
ds@1952
   942
ds@1952
   943
	/* Rewrite the parameter order */
ds@1952
   944
	for (unsigned int i=0; i<pTrans->fmt_count; i++)
ds@1952
   945
	{
ds@1952
   946
		new_params[i] = params[pTrans->fmt_order[i]];
ds@1952
   947
	}
ds@1952
   948
ds@1952
   949
	return gnprintf(buffer, maxlength, pTrans->szPhrase, new_params);
ds@1952
   950
}
ds@1952
   951
ds@1952
   952
TransError Translator::CoreTransEx(CPhraseFile *pFile,
ds@1952
   953
								   int client, 
ds@1952
   954
								   char *buffer, 
ds@1952
   955
								   size_t maxlength, 
ds@1952
   956
								   const char *phrase, 
ds@1952
   957
								   void **params, 
ds@1952
   958
								   size_t *outlen)
ds@1952
   959
{
ds@1952
   960
	Translation trans;
ds@1952
   961
	TransError err;
ds@1952
   962
ds@1952
   963
	/* Using server lang temporarily until client lang stuff is implemented */
ds@1952
   964
	if ((err = pFile->GetTranslation(phrase, m_ServerLang, &trans)) != Trans_Okay)
ds@1952
   965
	{
ds@1952
   966
		return err;
ds@1952
   967
	}
ds@1952
   968
ds@1952
   969
	size_t len = Translate(buffer, maxlength, params, &trans);
ds@1952
   970
ds@1952
   971
	if (outlen)
ds@1952
   972
	{
ds@1952
   973
		*outlen = len;
ds@1952
   974
	}
ds@1952
   975
ds@1952
   976
	return Trans_Okay;
ds@1952
   977
}
ds@1952
   978
ds@1952
   979
TransError Translator::CoreTrans(int client, 
ds@1952
   980
								  char *buffer, 
ds@1952
   981
								  size_t maxlength, 
ds@1952
   982
								  const char *phrase, 
ds@1952
   983
								  void **params, 
ds@1952
   984
								  size_t *outlen)
ds@1952
   985
{
ds@1952
   986
	if (!g_pCorePhrases)
ds@1952
   987
	{
ds@1952
   988
		return Trans_BadPhraseFile;
ds@1952
   989
	}
ds@1952
   990
ds@1952
   991
	return CoreTransEx(g_pCorePhrases, client, buffer, maxlength, phrase, params, outlen);
ds@1952
   992
}
ds@1952
   993
ds@1952
   994
unsigned int Translator::GetServerLanguage()
ds@1952
   995
{
ds@1952
   996
	return m_ServerLang;
ds@1952
   997
}
ds@1952
   998
ds@1952
   999
unsigned int Translator::GetClientLanguage(int client)
ds@1952
  1000
{
ds@1952
  1001
	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
ds@1952
  1002
	return pPlayer->GetLanguageId();
ds@1952
  1003
}
ds@1952
  1004
ds@1952
  1005
bool Translator::GetLanguageInfo(unsigned int number, const char **code, const char **name)
ds@1952
  1006
{
ds@1952
  1007
	if (number >= GetLanguageCount())
ds@1952
  1008
	{
ds@1952
  1009
		return false;
ds@1952
  1010
	}
ds@1952
  1011
ds@1952
  1012
	Language *l = m_Languages[number];
ds@1952
  1013
	if (code)
ds@1952
  1014
	{
ds@1952
  1015
		*code = l->m_code2;
ds@1952
  1016
	}
ds@1952
  1017
	if (name)
ds@1952
  1018
	{
ds@1952
  1019
		*name = m_pStringTab->GetString(l->m_FullName);
ds@1952
  1020
	}
ds@1952
  1021
ds@1952
  1022
	return true;
ds@1952
  1023
}