7#include "text/language.hpp"
8#include "text/translation.hpp"
9#include "forward_value.hpp"
36 [[nodiscard]]
virtual long long n() const noexcept = 0;
42 [[nodiscard]] virtual
bool equal_to(
l10n_args_base const &rhs) const noexcept = 0;
46 return lhs.equal_to(rhs);
54template<
typename... Values>
74 template<
typename... Args>
75 l10n_args(Args
const &...args) noexcept : _values(args...)
81 return std::make_unique<l10n_args>(*
this);
84 [[nodiscard]]
virtual bool equal_to(
l10n_args_base const &rhs)
const noexcept
86 if (
auto *rhs_ =
dynamic_cast<l10n_args const *
>(&rhs)) {
87 return _values == rhs_->_values;
104 [[nodiscard]]
long long n_recurse() const noexcept
106 if constexpr (I <
sizeof...(Values)) {
107 if constexpr (std::is_integral_v<decltype(std::get<I>(_values))>) {
108 return narrow_cast<long long>(std::get<I>(_values));
110 return n_recurse<I + 1>();
117 [[nodiscard]]
long long n() const noexcept
override
119 return n_recurse<0>();
125 template<
typename... Args>
126 static std::string format_wrapper(std::string_view fmt, Args
const &...args)
128 return std::format(fmt, args...);
131 template<
typename... Args>
132 static std::string format_locale_wrapper(
std::locale const &loc, std::string_view fmt, Args
const &...args)
134 return std::format(loc, fmt, args...);
138template<
typename... Args>
139l10n_args(Args &&...) -> l10n_args<forward_value_t<Args>...>;
153 constexpr l10n() noexcept : _msg_id(), _args(
nullptr) {}
156 l10n &operator=(
l10n &&) noexcept = default;
158 l10n(
l10n const &other) noexcept : _msg_id(other._msg_id), _args(other._args ? other._args->unique_copy() :
nullptr) {}
160 l10n &operator=(
l10n const &other)
noexcept
162 _msg_id = other._msg_id;
163 _args = other._args ? other._args->unique_copy() :
nullptr;
169 [[nodiscard]]
explicit operator bool() const noexcept
171 return not _msg_id.
empty();
187 template<
typename... Args>
188 l10n(std::string_view msg_id, Args
const &...args) noexcept :
189 _msg_id(msg_id), _args(
sizeof...(Args) ? std::make_unique<
detail::l10n_args<forward_value_t<Args>...>>(args...) : nullptr)
203 auto fmt = ::tt::get_translation(_msg_id, _args->n(), languages);
204 return _args->format(fmt);
206 return std::string{::tt::get_translation(_msg_id, 0, languages)};
221 auto fmt = ::tt::get_translation(_msg_id, _args->n(), languages);
222 return _args->format(loc, fmt);
224 return std::string{::tt::get_translation(_msg_id, 0, languages)};
236 if (lhs._args == rhs._args) {
237 return lhs._msg_id == rhs._msg_id;
238 }
else if (lhs._args and rhs._args) {
239 return lhs._msg_id == rhs._msg_id and *lhs._args == *rhs._args;
virtual std::string format(std::string_view fmt) const noexcept=0
Format text from the arguments and the given format string.
virtual std::unique_ptr< l10n_args_base > unique_copy() const noexcept=0
Make a unique copy of the arguments.
virtual std::string format(std::locale const &loc, std::string_view fmt) const noexcept=0
Format text from the arguments and the given format string.
virtual long long n() const noexcept=0
The numeric value of the first numeric argument.
Delayed formatting.
Definition l10n.hpp:55
l10n_args(Args const &...args) noexcept
Construct a l10n arguments.
Definition l10n.hpp:75
std::unique_ptr< l10n_args_base > unique_copy() const noexcept
Make a unique copy of the arguments.
Definition l10n.hpp:79
std::string format(std::locale const &loc, std::string_view fmt) const noexcept override
Format text from the arguments and the given format string.
Definition l10n.hpp:98
long long n() const noexcept override
The numeric value of the first numeric argument.
Definition l10n.hpp:117
std::string format(std::string_view fmt) const noexcept override
Format text from the arguments and the given format string.
Definition l10n.hpp:93
A localizable message.
Definition l10n.hpp:149
friend bool operator==(l10n const &lhs, l10n const &rhs) noexcept
Compare two localizable messages.
Definition l10n.hpp:234
constexpr l10n() noexcept
Construct an empty message.
Definition l10n.hpp:153
std::string operator()(std::locale const &loc, std::vector< language * > const &languages=language::preferred_languages()) const noexcept
Translate and format the message.
Definition l10n.hpp:218
std::string operator()(std::vector< language * > const &languages=language::preferred_languages()) const noexcept
Translate and format the message.
Definition l10n.hpp:200
l10n(std::string_view msg_id, Args const &...args) noexcept
Construct a localizable message.
Definition l10n.hpp:188