VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Translations.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <optional>
7#include <string>
8#include <string_view>
9#include <vector>
10
11namespace VoltMod
12{
13
14/**
15 * @brief Localization system. Loads one JSON file per language; nested objects flatten into
16 * dotted keys (`category.punish`). Use @ref Get(key, slot) for per-player text.
17 *
18 * Lookup order: the slot's language (@ref PlayerLanguage), the active language, English, the
19 * framework's English defaults for its own keys (`cmd.*`, `target.*`), then the key verbatim.
20 */
22{
23public:
24 /** @p languages holds each player's pick and must outlive this. */
26
27 bool Load(std::string_view dirPath);
28 void SetLanguage(std::string_view lang);
29 const std::string& GetLanguage() const;
30
31 /** Look up a key in the active (server) language, falling back to English. For broadcasts;
32 * prefer @ref Get(key, slot) for any message addressed to a specific player. */
33 std::string Get(std::string_view key) const;
34
35 /** Look up a key in @p slot's registered language, falling back to the active language then English. */
36 std::string Get(std::string_view key, int slot) const;
37
38 /** @ref Get(key, slot), but @p fallback instead of the key itself when nothing carries it.
39 * For text the framework or a plugin can render without the consumer shipping the key. */
40 std::string GetOr(std::string_view key, int slot, std::string_view fallback) const;
41
42 /** @ref Get(key, slot), then replace each `{token}` occurrence from @p tokens. */
43 std::string Get(std::string_view key, int slot, const Tokens& tokens) const;
44
45 /** Active-language variant of the token-substituting @ref Get. */
46 std::string Get(std::string_view key, const Tokens& tokens) const;
47
48 /** Language codes that were successfully loaded (one per JSON file). */
49 std::vector<std::string> GetAvailableLanguages() const;
50
51 /** Set @p slot's language for every plugin. Empty means the active language. */
52 void SetPlayerLanguage(int slot, std::string_view lang);
53
54 /** Empty means the active language. Valid until the next change. */
55 std::string_view PlayerLanguage(int slot) const;
56
57private:
58 // Engaged (possibly with an empty view) when lang/key is present, nullopt when it is absent.
59 // A view, not a string, so a lookup on the per-frame menu path copies nothing; the tables and
60 // the built-in defaults both outlive the call.
61 std::optional<std::string_view> LookupIn(std::string_view lang, std::string_view key) const;
62
63 // The whole lookup chain for @p slot short of the final fallback: nullopt means nothing
64 // carries the key, which is what Get and GetOr answer differently.
65 std::optional<std::string_view> Resolve(std::string_view key, int slot) const;
66
67 /** Key -> text, for one language. */
68 using Phrases = StringMap<std::string>;
69
70 StringMap<Phrases> _translations; ///< by language code
71 std::string _activeLang = "en";
72 PlayerLanguages& _languages;
73};
74
75} // namespace VoltMod
Localization system. Loads one JSON file per language; nested objects flatten into dotted keys (categ...
void SetLanguage(std::string_view lang)
std::string_view PlayerLanguage(int slot) const
std::vector< std::string > GetAvailableLanguages() const
bool Load(std::string_view dirPath)
void SetPlayerLanguage(int slot, std::string_view lang)
const std::string & GetLanguage() const
std::string GetOr(std::string_view key, int slot, std::string_view fallback) const
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
std::unordered_map< std::string, Value, StringHash, std::equal_to<> > StringMap
Definition StringMap.hpp:21
std::map< std::string, std::string > Tokens
Definition Strings.hpp:14