8#ifndef BOOST_LOCALE_MESSAGE_HPP_INCLUDED
9#define BOOST_LOCALE_MESSAGE_HPP_INCLUDED
11#include <boost/locale/detail/facet_id.hpp>
12#include <boost/locale/detail/is_supported_char.hpp>
13#include <boost/locale/formatting.hpp>
14#include <boost/locale/util/string.hpp>
23# pragma warning(disable : 4275 4251 4231 4660)
34namespace boost {
namespace locale {
47 template<
typename CharType>
49 public detail::facet_id<message_format<CharType>> {
50 BOOST_LOCALE_ASSERT_IS_SUPPORTED(CharType);
84 virtual int domain(
const std::string& domain)
const = 0;
98 inline bool is_us_ascii_char(
char c)
101 return 0 < c && c < 0x7F;
103 inline bool is_us_ascii_string(
const char* msg)
106 if(!is_us_ascii_char(*msg++))
112 template<
typename CharType>
113 struct string_cast_traits {
114 static const CharType* cast(
const CharType* msg, std::basic_string<CharType>& ) {
return msg; }
118 struct string_cast_traits<char> {
119 static const char* cast(
const char* msg, std::string& buffer)
121 if(is_us_ascii_string(msg))
123 buffer.reserve(strlen(msg));
125 while((c = *msg++) != 0) {
126 if(is_us_ascii_char(c))
129 return buffer.c_str();
141 template<
typename CharType>
149 basic_message() : n_(0), c_id_(nullptr), c_context_(nullptr), c_plural_(nullptr) {}
160 n_(n), c_id_(single), c_context_(nullptr), c_plural_(plural)
167 n_(0), c_id_(id), c_context_(context), c_plural_(nullptr)
179 c_id_(single), c_context_(context), c_plural_(plural)
184 n_(0), c_id_(nullptr), c_context_(nullptr), c_plural_(nullptr), id_(id)
191 n_(number), c_id_(nullptr), c_context_(nullptr), c_plural_(nullptr), id_(single), plural_(plural)
196 n_(0), c_id_(nullptr), c_context_(nullptr), c_plural_(nullptr), id_(id), context_(context)
207 c_id_(nullptr), c_context_(nullptr), c_plural_(nullptr), id_(single), context_(context), plural_(plural)
225 swap(c_id_, other.c_id_);
226 swap(c_context_, other.c_context_);
227 swap(c_plural_, other.c_plural_);
228 swap(id_, other.id_);
229 swap(context_, other.context_);
230 swap(plural_, other.plural_);
247 if(std::has_facet<facet_type>(locale))
248 id = std::use_facet<facet_type>(locale).domain(domain_id);
249 return str(locale,
id);
260 if(ptr != buffer.c_str())
267 void write(std::basic_ostream<char_type>& out)
const
269 const std::locale& loc = out.getloc();
272 out <<
write(loc,
id, buffer);
282 return plural_.c_str();
290 return context_.c_str();
293 const char_type* id()
const {
return c_id_ ? c_id_ : id_.c_str(); }
297 static const char_type empty_string[1] = {0};
300 const char_type* context = this->context();
301 const char_type* plural = this->plural();
307 if(std::has_facet<facet_type>(loc))
308 facet = &std::use_facet<facet_type>(loc);
313 translated = facet->get(domain_id, context,
id);
315 translated = facet->get(domain_id, context,
id, n_);
319 const char_type* msg = plural ? (n_ == 1 ? id : plural) :
id;
322 translated = facet->convert(msg, buffer);
324 translated = detail::string_cast_traits<char_type>::cast(msg, buffer);
344#ifndef BOOST_LOCALE_NO_CXX20_STRING8
348#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
352#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
358 template<
typename CharType>
369 template<
typename CharType>
376 template<
typename CharType>
383 template<
typename CharType>
390 template<
typename CharType>
391 inline basic_message<CharType>
398 template<
typename CharType>
405 template<
typename CharType>
407 const std::basic_string<CharType>& msg)
413 template<
typename CharType>
415 const std::basic_string<CharType>& single,
416 const std::basic_string<CharType>& plural,
423 template<
typename CharType>
424 inline basic_message<CharType>
425 translate(
const std::basic_string<CharType>& single,
const std::basic_string<CharType>& plural,
count_type n)
435 template<
typename CharType>
436 std::basic_string<CharType>
gettext(
const CharType*
id,
const std::locale& loc = std::locale())
441 template<
typename CharType>
442 std::basic_string<CharType>
449 template<
typename CharType>
450 std::basic_string<CharType>
dgettext(
const char* domain,
const CharType*
id,
const std::locale& loc = std::locale())
456 template<
typename CharType>
457 std::basic_string<CharType>
dngettext(
const char* domain,
461 const std::locale& loc = std::locale())
467 template<
typename CharType>
468 std::basic_string<CharType>
469 pgettext(
const CharType* context,
const CharType*
id,
const std::locale& loc = std::locale())
475 template<
typename CharType>
476 std::basic_string<CharType>
npgettext(
const CharType* context,
480 const std::locale& loc = std::locale())
486 template<
typename CharType>
487 std::basic_string<CharType>
488 dpgettext(
const char* domain,
const CharType* context,
const CharType*
id,
const std::locale& loc = std::locale())
494 template<
typename CharType>
496 const CharType* context,
500 const std::locale& loc = std::locale())
511 std::string domain_id;
513 template<
typename CharType>
514 std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out,
const set_domain& dom)
516 int id = std::use_facet<message_format<CharType>>(out.getloc()).domain(dom.domain_id);
517 ios_info::get(out).domain_id(
id);
532#ifdef BOOST_LOCALE_DOXYGEN
539 detail::set_domain tmp = {
id};
This class represents a message that can be converted to a specific locale message.
Definition: message.hpp:142
void domain_id(int)
Set special message domain identification.
static ios_info & get(std::ios_base &ios)
Get ios_info instance for specific stream object.
std::basic_ostream< CharType > & operator<<(std::basic_ostream< CharType > &out, const date_time &t)
Definition: date_time.hpp:724
unspecified_type domain(const std::string &id)
Definition: message.hpp:537
string_type str(const std::locale &loc, int id) const
Translate message to a string using locale loc and message domain index id.
Definition: message.hpp:256
basic_message(const char_type *context, const char_type *id)
Definition: message.hpp:166
basic_message< char8_t > u8message
Convenience typedef for char8_t.
Definition: message.hpp:346
basic_message()
Create default empty message.
Definition: message.hpp:149
basic_message< char16_t > u16message
Convenience typedef for char16_t.
Definition: message.hpp:350
std::basic_string< char_type > string_type
The string type this object can be used with.
Definition: message.hpp:145
basic_message< char > message
Convenience typedef for char.
Definition: message.hpp:341
virtual const char_type * get(int domain_id, const char_type *context, const char_type *id) const =0
basic_message(const string_type &single, const string_type &plural, count_type number)
Definition: message.hpp:190
string_type str(const std::locale &locale, const std::string &domain_id) const
Translate message to a string using locale locale and message domain domain_id.
Definition: message.hpp:244
basic_message(const basic_message &)=default
Copy an object.
basic_message< char32_t > u32message
Convenience typedef for char32_t.
Definition: message.hpp:354
string_type str(const std::string &domain_id) const
Translate message to a string using the default locale and message domain domain_id.
Definition: message.hpp:253
long long count_type
Type used for the count/n argument to the translation functions choosing between singular and plural ...
Definition: message.hpp:44
void write(std::basic_ostream< char_type > &out) const
Definition: message.hpp:267
std::basic_string< CharType > pgettext(const CharType *context, const CharType *id, const std::locale &loc=std::locale())
Translate message id according to locale loc in context context.
Definition: message.hpp:469
std::basic_string< CharType > gettext(const CharType *id, const std::locale &loc=std::locale())
Translate message id according to locale loc.
Definition: message.hpp:436
CharType char_type
Character type.
Definition: message.hpp:54
std::basic_string< CharType > dnpgettext(const char *domain, const CharType *context, const CharType *s, const CharType *p, count_type n, const std::locale &loc=std::locale())
Translate plural form according to locale loc in domain domain in context context.
Definition: message.hpp:495
void swap(basic_message &other) noexcept(noexcept(std::declval< string_type & >().swap(std::declval< string_type & >())))
Swap two message objects.
Definition: message.hpp:221
basic_message(const string_type &context, const string_type &id)
Create a simple message from a string with context.
Definition: message.hpp:195
basic_message(const char_type *single, const char_type *plural, count_type n)
Definition: message.hpp:159
basic_message(const string_type &id)
Create a simple message from a string.
Definition: message.hpp:183
CharType char_type
The character this message object is used with.
Definition: message.hpp:144
message_format< char_type > facet_type
The type of the facet the messages are fetched with.
Definition: message.hpp:146
std::basic_string< CharType > string_type
String type.
Definition: message.hpp:56
basic_message(const char_type *context, const char_type *single, const char_type *plural, count_type n)
Definition: message.hpp:174
std::basic_string< CharType > dpgettext(const char *domain, const CharType *context, const CharType *id, const std::locale &loc=std::locale())
Translate message id according to locale loc in domain domain in context context.
Definition: message.hpp:488
string_type str() const
Translate message to a string in the default global locale, using default domain.
Definition: message.hpp:238
std::basic_string< CharType > npgettext(const CharType *context, const CharType *s, const CharType *p, count_type n, const std::locale &loc=std::locale())
Translate plural form according to locale loc in context context.
Definition: message.hpp:476
virtual const char_type * convert(const char_type *msg, string_type &buffer) const =0
basic_message< CharType > translate(const CharType *msg)
Translate a message, msg is not copied.
Definition: message.hpp:370
string_type str(const std::locale &locale) const
Translate message to a string in the locale locale, using default domain.
Definition: message.hpp:241
virtual const char_type * get(int domain_id, const char_type *context, const char_type *single_id, count_type n) const =0
std::basic_string< CharType > ngettext(const CharType *s, const CharType *p, count_type n, const std::locale &loc=std::locale())
Translate plural form according to locale loc.
Definition: message.hpp:443
std::basic_string< CharType > dngettext(const char *domain, const CharType *s, const CharType *p, count_type n, const std::locale &loc=std::locale())
Translate plural form according to locale loc in domain domain.
Definition: message.hpp:457
basic_message(const char_type *id)
Definition: message.hpp:153
virtual int domain(const std::string &domain) const =0
Convert a string that defines domain to the integer id used by get functions.
basic_message(const string_type &context, const string_type &single, const string_type &plural, count_type number)
Definition: message.hpp:202
std::basic_string< CharType > dgettext(const char *domain, const CharType *id, const std::locale &loc=std::locale())
Translate message id according to locale loc in domain domain.
Definition: message.hpp:450
message_format(size_t refs=0)
Standard constructor.
Definition: message.hpp:59
basic_message< wchar_t > wmessage
Convenience typedef for wchar_t.
Definition: message.hpp:343