added %T format support

added a new lang native

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40395
This commit is contained in:
Borja Ferrer 2007-01-27 03:25:34 +00:00
parent 7f44c014ae
commit aa8bf86dc1
8 changed files with 150 additions and 1 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="8.00"
Version="8,00"
Name="sourcemod_mm"
ProjectGUID="{E39527CD-7CAB-4420-97CC-DA1B93B260BC}"
RootNamespace="sourcemod_mm"
@ -243,6 +243,10 @@
RelativePath="..\smn_handles.cpp"
>
</File>
<File
RelativePath="..\smn_lang.cpp"
>
</File>
<File
RelativePath="..\smn_player.cpp"
>

View File

@ -16,6 +16,8 @@
#include <stdarg.h>
#include "sm_stringutil.h"
#include "CLogger.h"
#include "PluginSys.h"
#include "CTranslator.h"
#define LADJUST 0x00000004 /* left adjustment */
#define ZEROPAD 0x00000080 /* zero (as opposed to blank) pad */
@ -28,6 +30,47 @@
return 0; \
}
size_t Translate(char *buffer, size_t maxlen, IPluginContext *pCtx, const char *key, cell_t target, const cell_t *params, int *arg)
{
unsigned int langid;
CPlugin *pl = (CPlugin *)g_PluginSys.FindPluginByContext(pCtx->GetContext());
size_t langcount = pl->GetLangFileCount();
if (!g_Translator.GetLanguageByCode("en", &langid)) //:TODO: hardcoded this just for testing
{
//:TODO: error out something
}
CPhraseFile *phrfl;
TransError err = Trans_Okay;
Translation pTrans;
for (size_t i=0; i<langcount && err!=Trans_Okay; i++)
{
phrfl = g_Translator.GetFileByIndex(pl->GetFileByIndex(i));
err = phrfl->GetTranslation(key, langid, &pTrans);
}
if (err != Trans_Okay)
{
//:TODO: we didnt find our translation in any file :o
}
void *new_params[MAX_TRANSLATE_PARAMS];
unsigned int max_params = pTrans.fmt_count;
for (size_t i=0; i<max_params; i++)
{
pCtx->LocalToPhysAddr(params[*arg], reinterpret_cast<cell_t **>(&new_params[i]));
(*arg)++;
if ((*arg) + i > (size_t)params[0])
{
//:TODO: we are missing arguments zOMG
}
}
return g_Translator.Translate(buffer, maxlen, new_params, &pTrans);
}
//:TODO: review this code before we choose a license
void AddString(char **buf_p, size_t &maxlen, const char *string, int width, int prec)
@ -564,6 +607,16 @@ reswitch:
arg++;
break;
}
case 'T':
{
CHECK_ARGS(0);
char *key;
cell_t target = params[arg++];
pCtx->LocalToString(params[arg++], &key);
llen -= Translate(buf_p, llen, pCtx, key, target, params, &arg);
buf_p += llen;
break;
}
case '%':
{
if (!llen)

View File

@ -20,6 +20,8 @@
using namespace SourcePawn;
#define LANG_SERVER 0
size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext *pCtx, const cell_t *params, int *param);
const char *stristr(const char *str, const char *substr);
unsigned int strncopy(char *dest, const char *src, size_t count);

29
core/smn_lang.cpp Normal file
View File

@ -0,0 +1,29 @@
/**
* ===============================================================
* SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
* ===============================================================
*
* This file is not open source and may not be copied without explicit
* written permission of AlliedModders LLC. This file may not be redistributed
* in whole or significant part.
* For information, see LICENSE.txt or http://www.sourcemod.net/license.php
*
* Version: $Id$
*/
#include "PluginSys.h"
#include "CTranslator.h"
static cell_t sm_LoadTranslations(IPluginContext *pCtx, const cell_t *params)
{
char *filename;
unsigned int index;
CPlugin *pl = (CPlugin *)g_PluginSys.FindPluginByContext(pCtx->GetContext());
pCtx->LocalToString(params[1], &filename);
index = g_Translator.FindOrAddPhraseFile(filename);
pl->AddLangFile(index);
return 1;
}

View File

@ -268,6 +268,9 @@ static cell_t SMC_ParseFile(IPluginContext *pContext, const cell_t *params)
pContext->LocalToPhysAddr(params[3], &c_line);
pContext->LocalToPhysAddr(params[4], &c_col);
*c_line = line;
*c_col = col;
return (cell_t)p_err;
}

View File

@ -491,6 +491,21 @@ void CPlugin::SetTimeStamp(time_t t)
m_LastAccess = t;
}
void CPlugin::AddLangFile(unsigned int index)
{
m_PhraseFiles.push_back(index);
}
size_t CPlugin::GetLangFileCount() const
{
return m_PhraseFiles.size();
}
unsigned int CPlugin::GetFileByIndex(unsigned int index) const
{
return m_PhraseFiles.at(index);
}
/*******************
* PLUGIN ITERATOR *
*******************/

View File

@ -21,6 +21,7 @@
#include <IHandleSys.h>
#include <sh_list.h>
#include <sh_stack.h>
#include <sh_vector.h>
#include "sm_globals.h"
#include "vm/sp_vm_basecontext.h"
#include "PluginInfoDatabase.h"
@ -176,6 +177,21 @@ public:
* Returns true if a plugin is usable.
*/
bool IsRunnable() const;
/**
* Adds a language file index to the plugin's list.
*/
void AddLangFile(unsigned int index);
/**
* Get language file count for this plugin.
*/
size_t GetLangFileCount() const;
/**
* Get language file index based on the vector index.
*/
unsigned int GetFileByIndex(unsigned int index) const;
public:
/**
* Returns the modification time during last plugin load.
@ -207,6 +223,7 @@ private:
IdentityToken_t *m_ident;
Handle_t m_handle;
bool m_WasRunning;
CVector<unsigned int> m_PhraseFiles;
};
class CPluginManager :

26
plugins/include/lang.inc Normal file
View File

@ -0,0 +1,26 @@
/**
* ===============================================================
* SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
* ===============================================================
*
* This file is part of the SourceMod/SourcePawn SDK. This file may only be used
* or modified under the Terms and Conditions of its License Agreement, which is found
* in LICENSE.txt. The Terms and Conditions for making SourceMod extensions/plugins
* may change at any time. To view the latest information, see:
* http://www.sourcemod.net/license.php
*
* Version: $Id$
*/
#if defined _lang_included
#endinput
#endif
#define _lang_included
/**
* @brief Loads a translation file for the plugin calling this native.
*
* @param path Translation file.
* @noreturn
*/
native LoadTranslations(const String:file[]);