Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
i18n.hpp
Go to the documentation of this file.
1#pragma once
2
3// Internationalization — string translation and locale management.
4// Supports .strings and .po file formats, positional argument substitution.
5
6#include <string>
7#include <string_view>
8#include <map>
9#include <optional>
10#include <vector>
11
12namespace pulp::runtime {
13
16public:
17 LocalisedStrings() = default;
18
20 bool load_strings_file(std::string_view path);
21
23 bool load_po_file(std::string_view path);
24
26 bool load_json_file(std::string_view path);
27
29 void add(std::string_view key, std::string_view value);
30
32 std::string translate(std::string_view key) const;
33
36 std::string translate(std::string_view key, const std::vector<std::string>& args) const;
37
39 std::string t(std::string_view key) const { return translate(key); }
40
42 bool has(std::string_view key) const;
43
45 int count() const { return static_cast<int>(strings_.size()); }
46
48 void clear() { strings_.clear(); }
49
51 const std::string& locale() const { return locale_; }
52 void set_locale(std::string_view loc) { locale_ = std::string(loc); }
53
54 // ── Global instance ─────────────────────────────────────────────────
55
58
60 static std::string system_locale();
61
62private:
63 std::map<std::string, std::string, std::less<>> strings_;
64 std::string locale_ = "en";
65};
66
68inline std::string tr(std::string_view key) {
70}
71
73inline std::string tr(std::string_view key, const std::vector<std::string>& args) {
74 return LocalisedStrings::instance().translate(key, args);
75}
76
77} // namespace pulp::runtime
void set_locale(std::string_view loc)
Definition i18n.hpp:52
static LocalisedStrings & instance()
Get the global translation instance.
void clear()
Clear all translations.
Definition i18n.hpp:48
void add(std::string_view key, std::string_view value)
Add a single translation.
std::string translate(std::string_view key) const
Look up a translation. Returns the key itself if not found.
bool load_json_file(std::string_view path)
Load translations from a JSON file ({"key": "value"}).
std::string translate(std::string_view key, const std::vector< std::string > &args) const
Look up with positional argument substitution.
bool load_strings_file(std::string_view path)
Load translations from a .strings file (Apple format: "key" = "value";).
int count() const
Number of translations loaded.
Definition i18n.hpp:45
bool load_po_file(std::string_view path)
Load translations from a .po file (gettext format).
std::string t(std::string_view key) const
Shorthand: t("key") == translate("key").
Definition i18n.hpp:39
const std::string & locale() const
The locale identifier (e.g., "en", "de", "ja").
Definition i18n.hpp:51
bool has(std::string_view key) const
Whether a key has a translation.
static std::string system_locale()
Detect the system locale.
Definition generated_event_source.hpp:33
std::string tr(std::string_view key)
Convenience: translate a key using the global instance.
Definition i18n.hpp:68