8#include "translation.hpp"
9#include "../forward_value.hpp"
11#include "../os_settings.hpp"
17namespace hi::inline v1 {
42 [[nodiscard]]
virtual long long n() const noexcept = 0;
52 return lhs.equal_to(rhs);
60template<
typename... Values>
80 template<
typename... Args>
87 return std::make_unique<translate_args>(*
this);
93 return _values == rhs_->_values;
109 template<std::
size_t I>
110 [[nodiscard]]
long long n_recurse() const noexcept
112 if constexpr (I <
sizeof...(Values)) {
113 if constexpr (std::is_integral_v<decltype(std::get<I>(_values))>) {
114 return narrow_cast<long long>(std::get<I>(_values));
116 return n_recurse<I + 1>();
123 [[nodiscard]]
long long n() const noexcept
override
125 return n_recurse<0>();
131 template<
typename... Args>
132 static std::string format_wrapper(std::string_view fmt, Args
const&...args)
134 return std::vformat(fmt, std::make_format_args(args...));
137 template<
typename... Args>
138 static std::string format_locale_wrapper(
std::locale const& loc, std::string_view fmt, Args
const&...args)
140 return std::vformat(loc, fmt, std::make_format_args(args...));
144template<
typename... Args>
145translate_args(Args&&...) -> translate_args<forward_value_t<Args>...>;
161 constexpr translate() noexcept : _msg_id(), _args(
nullptr), _has_args(false) {}
164 _msg_id(
std::move(other._msg_id)), _args(
nullptr), _has_args(other._has_args)
171 constexpr translate& operator=(translate&& other)
noexcept
174 _has_args = other._has_args;
181 constexpr translate(translate
const& other) noexcept : _msg_id(other._msg_id), _args(
nullptr), _has_args(other._has_args)
184 _args = other._args->unique_copy();
188 constexpr translate& operator=(translate
const& other)
noexcept
190 _msg_id = other._msg_id;
191 _has_args = other._has_args;
193 _args = other._args->unique_copy();
198 [[nodiscard]]
constexpr bool empty() const noexcept
200 return _msg_id.empty();
205 [[nodiscard]]
constexpr explicit operator bool() const noexcept
220 constexpr translate(std::string_view msg_id) noexcept : _msg_id(msg_id), _args(
nullptr), _has_args(
false) {}
235 template<
typename FirstArg,
typename... Args>
236 translate(std::string_view msg_id, FirstArg
const& first_arg, Args
const&...args) noexcept :
238 _args(std::make_unique<
detail::translate_args<forward_value_t<FirstArg>, forward_value_t<Args>...>>(first_arg, args...)),
252 hilet fmt = ::hi::get_translation(_msg_id, _args->n(), languages);
253 return _args->format(fmt);
255 return std::string{::hi::get_translation(_msg_id, 0, languages)};
270 hilet fmt = ::hi::get_translation(_msg_id, _args->n(), languages);
271 return _args->format(loc, fmt);
273 return std::string{::hi::get_translation(_msg_id, 0, languages)};
285 if (lhs._has_args != rhs._has_args) {
289 if (lhs._has_args and *lhs._args != *rhs._args) {
293 return lhs._msg_id == rhs._msg_id;
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
Definition translate.hpp:19
virtual std::string format(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.
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.
Delayed formatting.
Definition translate.hpp:61
std::unique_ptr< translate_args_base > unique_copy() const noexcept override
Make a unique copy of the arguments.
Definition translate.hpp:85
long long n() const noexcept override
The numeric value of the first numeric argument.
Definition translate.hpp:123
std::string format(std::string_view fmt) const noexcept override
Format text from the arguments and the given format string.
Definition translate.hpp:99
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 translate.hpp:104
translate_args(Args const &...args) noexcept
Construct a translate arguments.
Definition translate.hpp:81
A localizable message.
Definition translate.hpp:155
constexpr friend bool operator==(translate const &lhs, translate const &rhs) noexcept
Compare two localizable messages.
Definition translate.hpp:283
translate(std::string_view msg_id, FirstArg const &first_arg, Args const &...args) noexcept
Construct a localizable message.
Definition translate.hpp:236
constexpr translate(std::string_view msg_id) noexcept
Construct a localizable message.
Definition translate.hpp:220
constexpr translate() noexcept
Construct an empty message.
Definition translate.hpp:161
std::string operator()(std::vector< language * > const &languages=os_settings::languages()) const noexcept
Translate and format the message.
Definition translate.hpp:249
std::string operator()(std::locale const &loc, std::vector< language * > const &languages=os_settings::languages()) const noexcept
Translate and format the message.
Definition translate.hpp:267