libnick 2025.9.1
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
gettext.h
Go to the documentation of this file.
1
22
23#ifndef GETTEXT_H
24#define GETTEXT_H
25
26#include <format>
27#include <string>
28#include <vector>
29#include <libintl.h>
30
31#define GETTEXT_CONTEXT_SEPARATOR "\004"
32#define _(String) ::Nickvision::Localization::Gettext::dgettext(String)
33#define _n(String, StringPlural, N) ::Nickvision::Localization::Gettext::dngettext(String, StringPlural, static_cast<unsigned long>(N))
34#define _f(String, ...) ::Nickvision::Localization::Gettext::fgettext(String, __VA_ARGS__)
35#define _fn(String, StringPlural, N, ...) ::Nickvision::Localization::Gettext::fngettext(String, StringPlural, static_cast<unsigned long>(N), __VA_ARGS__)
36#define _p(Context, String) ::Nickvision::Localization::Gettext::pgettext(Context GETTEXT_CONTEXT_SEPARATOR String, String)
37#define _pn(Context, String, StringPlural, N) ::Nickvision::Localization::Gettext::pngettext(Context GETTEXT_CONTEXT_SEPARATOR String, String, StringPlural, static_cast<unsigned long>(N))
38
40{
46 bool init(const std::string& domainName) noexcept;
51 const std::string& getDomainName() noexcept;
57 const std::vector<std::string>& getAvailableLanguages() noexcept;
63 bool changeLanguage(const std::string& language) noexcept;
69 const char* dgettext(const char* msgid) noexcept;
77 const char* dngettext(const char* msg, const char* msgPlural, unsigned long n) noexcept;
84 template<typename... Args>
85 std::string fgettext(const char* msg, Args&&... args) noexcept
86 {
87 return std::vformat(Nickvision::Localization::Gettext::dgettext(msg), std::make_format_args(args...));
88 }
89
97 template<typename... Args>
98 std::string fngettext(const char* msg, const char* msgPlural, unsigned long n, Args&&... args) noexcept
99 {
100 return std::vformat(Nickvision::Localization::Gettext::dngettext(msg, msgPlural, n), std::make_format_args(args...));
101 }
102
108 const char* pgettext(const char* context, const char* msg) noexcept;
117 const char* pngettext(const char* context, const char* msg, const char* msgPlural, unsigned long n) noexcept;
118}
119
120#endif //GETTEXT_H
Definition gettext.h:40
const char * pngettext(const char *context, const char *msg, const char *msgPlural, unsigned long n) noexcept
Translates a plural message for a given context.
const char * dgettext(const char *msgid) noexcept
Translates a message.
const char * pgettext(const char *context, const char *msg) noexcept
Translates a message for a given context.
bool init(const std::string &domainName) noexcept
Initializes the gettext system. This function should only be called once, regardless of with differen...
std::string fgettext(const char *msg, Args &&... args) noexcept
Translates a message and formats it with the given arguments.
Definition gettext.h:85
std::string fngettext(const char *msg, const char *msgPlural, unsigned long n, Args &&... args) noexcept
Translates a plural message and formats it with the given arguments.
Definition gettext.h:98
const std::vector< std::string > & getAvailableLanguages() noexcept
Gets the list of available translated languages.
bool changeLanguage(const std::string &language) noexcept
Changes the current language for gettext translations.
const std::string & getDomainName() noexcept
Gets the domain name used for gettext translations.
const char * dngettext(const char *msg, const char *msgPlural, unsigned long n) noexcept
Translates a plural message.