faluco@389: /**
ds@653: * vim: set ts=4 :
ds@1330: * =============================================================================
dvander@1231: * SourceMod
dvander@1231: * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
ds@1330: * =============================================================================
faluco@389: *
ds@1330: * This program is free software; you can redistribute it and/or modify it under
ds@1330: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330: * Free Software Foundation.
dvander@1231: *
ds@1330: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1330: * details.
dvander@1231: *
ds@1330: * You should have received a copy of the GNU General Public License along with
ds@1330: * this program. If not, see .
dvander@1231: *
ds@1330: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1330: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1330: * this exception to all derivative works. AlliedModders LLC defines further
ds@1330: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330: * or .
faluco@389: *
faluco@389: * Version: $Id$
faluco@389: */
faluco@389:
faluco@389: #include "PluginSys.h"
dvander@591: #include "Translator.h"
dvander@1014: #include "LibrarySys.h"
dvander@1014: #include "sm_stringutil.h"
dvander@1105: #include "PlayerManager.h"
faluco@389:
faluco@389: static cell_t sm_LoadTranslations(IPluginContext *pCtx, const cell_t *params)
faluco@389: {
faluco@389: char *filename;
faluco@389: unsigned int index;
faluco@389: CPlugin *pl = (CPlugin *)g_PluginSys.FindPluginByContext(pCtx->GetContext());
faluco@389:
faluco@389: pCtx->LocalToString(params[1], &filename);
faluco@389:
dvander@1014: /* Check if there is no extension */
dvander@1014: const char *ext = g_LibSys.GetFileExtension(filename);
dvander@1014: if (!ext || (strcmp(ext, "cfg") && strcmp(ext, "txt")))
dvander@1014: {
dvander@1014: /* Append one */
dvander@1014: static char new_file[PLATFORM_MAX_PATH];
dvander@1014: UTIL_Format(new_file, sizeof(new_file), "%s.txt", filename);
dvander@1014: filename = new_file;
dvander@1014: }
dvander@1014:
faluco@389: index = g_Translator.FindOrAddPhraseFile(filename);
faluco@389: pl->AddLangFile(index);
faluco@389:
faluco@389: return 1;
faluco@389: }
dvander@396:
dvander@1034: static cell_t sm_SetGlobalTransTarget(IPluginContext *pContext, const cell_t *params)
dvander@1034: {
dvander@1034: g_SourceMod.SetGlobalTarget(params[1]);
dvander@1034:
dvander@1034: return 1;
dvander@1034: }
dvander@1034:
dvander@1105: static cell_t sm_GetClientLanguage(IPluginContext *pContext, const cell_t *params)
dvander@1105: {
dvander@1105: CPlayer *player = g_Players.GetPlayerByIndex(params[1]);
dvander@1105: if (!player || !player->IsConnected())
dvander@1105: {
dvander@1105: return pContext->ThrowNativeError("Invalid client index %d", params[1]);
dvander@1105: }
dvander@1105:
dvander@1105: return g_Translator.GetClientLanguage(params[1]);
dvander@1105: }
dvander@1105:
dvander@1105: static cell_t sm_GetServerLanguage(IPluginContext *pContext, const cell_t *params)
dvander@1105: {
dvander@1105: return g_Translator.GetServerLanguage();
dvander@1105: }
dvander@1105:
dvander@1105: static cell_t sm_GetLanguageCount(IPluginContext *pContext, const cell_t *params)
dvander@1105: {
dvander@1105: return g_Translator.GetLanguageCount();
dvander@1105: }
dvander@1105:
dvander@1105: static cell_t sm_GetLanguageInfo(IPluginContext *pContext, const cell_t *params)
dvander@1105: {
dvander@1105: const char *code;
dvander@1105: const char *name;
dvander@1105:
dvander@1105: if (!g_Translator.GetLanguageInfo(params[1], &code, &name))
dvander@1105: {
dvander@1105: return pContext->ThrowNativeError("Invalid language number %d", params[1]);
dvander@1105: }
dvander@1105:
dvander@1105: pContext->StringToLocalUTF8(params[2], params[3], code, NULL);
dvander@1105: pContext->StringToLocalUTF8(params[4], params[5], name, NULL);
dvander@1105:
dvander@1105: return 1;
dvander@1105: }
dvander@1105:
dvander@1105: REGISTER_NATIVES(langNatives)
dvander@396: {
dvander@396: {"LoadTranslations", sm_LoadTranslations},
dvander@1034: {"SetGlobalTransTarget", sm_SetGlobalTransTarget},
dvander@1105: {"GetClientLanguage", sm_GetClientLanguage},
dvander@1105: {"GetServerLanguage", sm_GetServerLanguage},
dvander@1105: {"GetLanguageCount", sm_GetLanguageCount},
dvander@1105: {"GetLanguageInfo", sm_GetLanguageInfo},
dvander@396: {NULL, NULL},
dvander@396: };