VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
ClientLanguage.cpp
Go to the documentation of this file.
3#include <array>
4#include <string_view>
5
6namespace VoltMod::Internal
7{
8
10{
11 std::string_view Steam;
12 std::string_view Code;
13};
14
15/** Steam's language names; a code no plugin translates falls back to the server language. */
16static constexpr std::array SteamLanguages{
17 SteamLanguage{.Steam = "english", .Code = "en"}, SteamLanguage{.Steam = "russian", .Code = "ru"},
18 SteamLanguage{.Steam = "ukrainian", .Code = "uk"}, SteamLanguage{.Steam = "german", .Code = "de"},
19 SteamLanguage{.Steam = "french", .Code = "fr"}, SteamLanguage{.Steam = "spanish", .Code = "es"},
20 SteamLanguage{.Steam = "portuguese", .Code = "pt"}, SteamLanguage{.Steam = "polish", .Code = "pl"},
21 SteamLanguage{.Steam = "turkish", .Code = "tr"},
22};
23
24static std::string_view CodeFor(std::string_view steamLanguage)
25{
27 {
28 if (language.Steam == steamLanguage)
29 {
30 return language.Code;
31 }
32 }
33 return {};
34}
35
37 : _clientConVars(clientConVars), _translations(translations)
38{
39 _connected = players.FullyConnected += [this](Player& player) {
40 if (player.IsBot() || !_translations.PlayerLanguage(player.Slot()).empty())
41 {
42 return;
43 }
44 // Pending queries are dropped when the slot changes hands, so the answer is this player's.
45 _clientConVars.Query(player.Slot(), "cl_language",
46 [this](int slot, ClientConVarStatus status, std::string_view, std::string_view value) {
48 {
49 return;
50 }
51 const std::string_view code = CodeFor(value);
52 // A pick made while the query was out wins.
53 if (!code.empty() && _translations.PlayerLanguage(slot).empty())
54 {
55 _translations.SetPlayerLanguage(slot, code);
56 }
57 });
58 };
59}
60
61} // namespace VoltMod::Internal
Asks a connected client what one of its own convars is set to.
bool Query(int slot, std::string_view cvarName, QueryCallback callback)
ClientLanguage(PlayerManager &players, ClientConVars &clientConVars, Translations &translations)
The roster: every connected player, and the signals for the connection lifecycle.
One connected player, owned by PlayerManager for the length of the connection.
Definition Player.hpp:23
Localization system. Loads one JSON file per language; nested objects flatten into dotted keys (categ...
std::string_view PlayerLanguage(int slot) const
void SetPlayerLanguage(int slot, std::string_view lang)
static constexpr std::array SteamLanguages
static std::string_view CodeFor(std::string_view steamLanguage)
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
ClientConVarStatus
How the client answered a convar query (CCLCMsg_RespondCvarValue::status_code).
@ Answered
The client reported the convar's current value.