8#include "translation.hpp"
9#include "../forward_value.hpp"
11#include "../os_settings.hpp"
17namespace hi::inline v1 {
37 [[nodiscard]]
virtual long long n() const noexcept = 0;
47 return lhs.equal_to(rhs);
55template<
typename... Values>
75 template<
typename... Args>
82 return std::make_unique<translate_args>(*
this);
88 return _values == rhs_->_values;
104 template<std::
size_t I>
105 [[nodiscard]]
long long n_recurse() const noexcept
107 if constexpr (I <
sizeof...(Values)) {
108 if constexpr (std::is_integral_v<decltype(std::get<I>(_values))>) {
109 return narrow_cast<long long>(std::get<I>(_values));
111 return n_recurse<I + 1>();
118 [[nodiscard]]
long long n() const noexcept
override
120 return n_recurse<0>();
126 template<
typename... Args>
127 static std::string format_wrapper(std::string_view fmt, Args
const &...args)
129 return std::vformat(fmt, std::make_format_args(args...));
132 template<
typename... Args>
133 static std::string format_locale_wrapper(
std::locale const &loc, std::string_view fmt, Args
const &...args)
135 return std::vformat(loc, fmt, std::make_format_args(args...));
139template<
typename... Args>
140translate_args(Args &&...) -> translate_args<forward_value_t<Args>...>;
154 constexpr translate() noexcept : _msg_id(), _args(
nullptr) {}
159 translate(
translate const &other) noexcept : _msg_id(other._msg_id), _args(other._args ? other._args->unique_copy() :
nullptr) {}
161 translate &operator=(translate
const &other)
noexcept
163 _msg_id = other._msg_id;
164 _args = other._args ? other._args->unique_copy() :
nullptr;
168 [[nodiscard]]
constexpr bool empty() const noexcept
170 return _msg_id.empty();
175 [[nodiscard]]
constexpr explicit operator bool() const noexcept
193 template<
typename... Args>
194 translate(std::string_view msg_id, Args
const &...args) noexcept :
195 _msg_id(msg_id), _args(
sizeof...(Args) ? std::make_unique<
detail::translate_args<forward_value_t<Args>...>>(args...) : nullptr)
208 auto fmt = ::hi::get_translation(_msg_id, _args->n(), languages);
209 return _args->format(fmt);
211 return std::string{::hi::get_translation(_msg_id, 0, languages)};
226 auto fmt = ::hi::get_translation(_msg_id, _args->n(), languages);
227 return _args->format(loc, fmt);
229 return std::string{::hi::get_translation(_msg_id, 0, languages)};
241 if (lhs._args == rhs._args) {
242 return lhs._msg_id == rhs._msg_id;
243 }
else if (lhs._args and rhs._args) {
244 return lhs._msg_id == rhs._msg_id and *lhs._args == *rhs._args;
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:56
long long n() const noexcept override
The numeric value of the first numeric argument.
Definition translate.hpp:118
std::unique_ptr< translate_args_base > unique_copy() const noexcept
Make a unique copy of the arguments.
Definition translate.hpp:80
std::string format(std::string_view fmt) const noexcept override
Format text from the arguments and the given format string.
Definition translate.hpp:94
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:99
translate_args(Args const &...args) noexcept
Construct a translate arguments.
Definition translate.hpp:76
A localizable message.
Definition translate.hpp:150
friend bool operator==(translate const &lhs, translate const &rhs) noexcept
Compare two localizable messages.
Definition translate.hpp:239
translate(std::string_view msg_id, Args const &...args) noexcept
Construct a localizable message.
Definition translate.hpp:194
constexpr translate() noexcept
Construct an empty message.
Definition translate.hpp:154
std::string operator()(std::vector< language * > const &languages=os_settings::languages()) const noexcept
Translate and format the message.
Definition translate.hpp:205
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:223