|
| 1 | +/***************************************************************************** |
| 2 | + * |
| 3 | + * PROJECT: Multi Theft Auto |
| 4 | + * LICENSE: See LICENSE in the top level directory |
| 5 | + * FILE: Client/mods/deathmatch/logic/luadefs/CLuaTranslationDefsClient.cpp |
| 6 | + * PURPOSE: Client-only Lua translation definitions class |
| 7 | + * |
| 8 | + * Multi Theft Auto is available from https://www.multitheftauto.com/ |
| 9 | + * |
| 10 | + *****************************************************************************/ |
| 11 | + |
| 12 | +#include "StdInc.h" |
| 13 | +#include "CLuaTranslationDefsClient.h" |
| 14 | +#include "CScriptArgReader.h" |
| 15 | +#include "CResourceTranslationManager.h" |
| 16 | +#include "net/Packets.h" |
| 17 | + |
| 18 | +void CLuaTranslationDefsClient::LoadFunctions() |
| 19 | +{ |
| 20 | + constexpr static const std::pair<const char*, lua_CFunction> functions[]{ |
| 21 | + {"setCurrentTranslationLanguage", SetCurrentTranslationLanguage}, |
| 22 | + {"getCurrentTranslationLanguage", GetCurrentTranslationLanguage}, |
| 23 | + {"getTranslation", GetTranslation}, |
| 24 | + {"getAvailableTranslations", GetAvailableTranslations}, |
| 25 | + }; |
| 26 | + |
| 27 | + for (const auto& [name, func] : functions) |
| 28 | + CLuaCFunctions::AddFunction(name, func); |
| 29 | +} |
| 30 | + |
| 31 | +int CLuaTranslationDefsClient::SetCurrentTranslationLanguage(lua_State* luaVM) |
| 32 | +{ |
| 33 | + SString language; |
| 34 | + |
| 35 | + CScriptArgReader argStream(luaVM); |
| 36 | + argStream.ReadString(language); |
| 37 | + |
| 38 | + if (!argStream.HasErrors()) |
| 39 | + { |
| 40 | + CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM); |
| 41 | + if (luaMain) |
| 42 | + { |
| 43 | + CResource* resource = luaMain->GetResource(); |
| 44 | + if (resource) |
| 45 | + { |
| 46 | + if (resource->GetTranslationManager()) |
| 47 | + { |
| 48 | + std::string oldLanguage = resource->GetTranslationManager()->GetClientLanguage(); |
| 49 | + resource->GetTranslationManager()->SetClientLanguage(language); |
| 50 | + std::string newLanguage = resource->GetTranslationManager()->GetClientLanguage(); |
| 51 | + |
| 52 | + if (oldLanguage != newLanguage) |
| 53 | + { |
| 54 | + CLuaArguments args; |
| 55 | + args.PushString(oldLanguage); |
| 56 | + args.PushString(newLanguage); |
| 57 | + |
| 58 | + if (g_pClientGame->GetLocalPlayer()) |
| 59 | + { |
| 60 | + g_pClientGame->GetLocalPlayer()->CallEvent("onClientTranslationLanguageChange", args, true); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + lua_pushboolean(luaVM, true); |
| 65 | + return 1; |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + m_pScriptDebugging->LogError(luaVM, "Translation system not initialized for resource '%s'", resource->GetName()); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + else |
| 75 | + m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); |
| 76 | + |
| 77 | + lua_pushboolean(luaVM, false); |
| 78 | + return 1; |
| 79 | +} |
| 80 | + |
| 81 | +int CLuaTranslationDefsClient::GetCurrentTranslationLanguage(lua_State* luaVM) |
| 82 | +{ |
| 83 | + CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM); |
| 84 | + if (luaMain) |
| 85 | + { |
| 86 | + CResource* resource = luaMain->GetResource(); |
| 87 | + if (resource) |
| 88 | + { |
| 89 | + if (resource->GetTranslationManager()) |
| 90 | + { |
| 91 | + std::string currentLanguage = resource->GetTranslationManager()->GetClientLanguage(); |
| 92 | + lua_pushstring(luaVM, currentLanguage.c_str()); |
| 93 | + return 1; |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + m_pScriptDebugging->LogError(luaVM, "Translation system not initialized for resource '%s'", resource->GetName()); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + lua_pushstring(luaVM, ""); |
| 103 | + return 1; |
| 104 | +} |
| 105 | + |
| 106 | +int CLuaTranslationDefsClient::GetTranslation(lua_State* luaVM) |
| 107 | +{ |
| 108 | + SString msgid; |
| 109 | + SString language; |
| 110 | + |
| 111 | + CScriptArgReader argStream(luaVM); |
| 112 | + argStream.ReadString(msgid); |
| 113 | + argStream.ReadString(language, ""); |
| 114 | + |
| 115 | + if (!argStream.HasErrors()) |
| 116 | + { |
| 117 | + CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM); |
| 118 | + if (luaMain) |
| 119 | + { |
| 120 | + CResource* resource = luaMain->GetResource(); |
| 121 | + if (resource) |
| 122 | + { |
| 123 | + if (resource->GetTranslationManager()) |
| 124 | + { |
| 125 | + // If no language specified, use current client language |
| 126 | + std::string targetLanguage = language.empty() ? |
| 127 | + resource->GetTranslationManager()->GetClientLanguage() : language; |
| 128 | + std::string result = resource->GetTranslationManager()->GetTranslation(msgid, targetLanguage); |
| 129 | + lua_pushstring(luaVM, result.c_str()); |
| 130 | + return 1; |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + m_pScriptDebugging->LogError(luaVM, "Translation system not initialized for resource '%s' when requesting '%s'", resource->GetName(), msgid.c_str()); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + else |
| 140 | + m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); |
| 141 | + |
| 142 | + lua_pushstring(luaVM, msgid.c_str()); |
| 143 | + return 1; |
| 144 | +} |
| 145 | + |
| 146 | +int CLuaTranslationDefsClient::GetAvailableTranslations(lua_State* luaVM) |
| 147 | +{ |
| 148 | + CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM); |
| 149 | + if (luaMain) |
| 150 | + { |
| 151 | + CResource* resource = luaMain->GetResource(); |
| 152 | + if (resource) |
| 153 | + { |
| 154 | + if (resource->GetTranslationManager()) |
| 155 | + { |
| 156 | + std::vector<std::string> languages = resource->GetTranslationManager()->GetAvailableLanguages(); |
| 157 | + |
| 158 | + lua_newtable(luaVM); |
| 159 | + int index = 1; |
| 160 | + for (size_t i = 0; i < languages.size(); ++i) |
| 161 | + { |
| 162 | + lua_pushinteger(luaVM, index++); |
| 163 | + lua_pushstring(luaVM, languages[i].c_str()); |
| 164 | + lua_settable(luaVM, -3); |
| 165 | + } |
| 166 | + return 1; |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + m_pScriptDebugging->LogError(luaVM, "Translation system not initialized for resource '%s'", resource->GetName()); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + lua_newtable(luaVM); |
| 176 | + return 1; |
| 177 | +} |
0 commit comments