7 #ifndef BOOST_LOCALE_MESSAGE_HPP_INCLUDED 8 #define BOOST_LOCALE_MESSAGE_HPP_INCLUDED 10 #include <boost/locale/formatting.hpp> 11 #include <boost/locale/util/string.hpp> 18 # pragma warning(push) 19 # pragma warning(disable : 4275 4251 4231 4660) 30 namespace boost {
namespace locale {
41 template<
typename CharType>
42 struct base_message_format;
47 template<
typename CharType>
48 class BOOST_SYMBOL_VISIBLE
message_format :
public base_message_format<CharType> {
65 virtual const char_type* get(
int domain_id,
const char_type* context,
const char_type*
id)
const = 0;
77 virtual const char_type*
78 get(
int domain_id,
const char_type* context,
const char_type* single_id,
int n)
const = 0;
89 virtual const char_type*
convert(
const char_type* msg, string_type& buffer)
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>
160 n_(n), c_id_(single), c_context_(0), c_plural_(plural)
167 n_(0), c_id_(id), c_context_(context), c_plural_(0)
175 n_(n), c_id_(single), c_context_(context), c_plural_(plural)
185 n_(
number), c_id_(0), c_context_(0), c_plural_(0), id_(single), plural_(plural)
190 n_(0), c_id_(0), c_context_(0), c_plural_(0), id_(id), context_(context)
201 c_id_(0), c_context_(0), c_plural_(0), id_(single), context_(context), plural_(plural)
215 std::swap(n_, other.n_);
216 std::swap(c_id_, other.c_id_);
217 std::swap(c_context_, other.c_context_);
218 std::swap(c_plural_, other.c_plural_);
221 context_.swap(other.context_);
222 plural_.swap(other.plural_);
242 if(std::has_facet<facet_type>(locale))
243 id = std::use_facet<facet_type>(locale).domain(domain_id);
244 return str(locale,
id);
252 if(std::has_facet<facet_type>(loc))
253 id = std::use_facet<facet_type>(loc).domain(domain_id);
262 if(ptr == buffer.c_str())
271 void write(std::basic_ostream<char_type>& out)
const 273 const std::locale& loc = out.getloc();
276 out <<
write(loc,
id, buffer);
286 return plural_.c_str();
294 return context_.c_str();
297 const char_type* id()
const {
return c_id_ ? c_id_ : id_.c_str(); }
302 static const char_type empty_string[1] = {0};
305 const char_type* context = this->context();
306 const char_type* plural = this->plural();
312 if(std::has_facet<facet_type>(loc))
313 facet = &std::use_facet<facet_type>(loc);
317 translated = facet->get(domain_id, context,
id);
319 translated = facet->get(domain_id, context,
id, n_);
324 const char_type* msg = plural ? (n_ == 1 ? id : plural) :
id;
327 translated = facet->convert(msg, buffer);
329 translated = detail::string_cast_traits<char_type>::cast(msg, buffer);
350 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T 354 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T 360 template<
typename CharType>
371 template<
typename CharType>
378 template<
typename CharType>
385 template<
typename CharType>
392 template<
typename CharType>
393 inline basic_message<CharType>
394 translate(
const CharType* context,
const CharType* single,
const CharType* plural,
int n)
400 template<
typename CharType>
407 template<
typename CharType>
409 const std::basic_string<CharType>& msg)
415 template<
typename CharType>
417 const std::basic_string<CharType>& single,
418 const std::basic_string<CharType>& plural,
425 template<
typename CharType>
426 inline basic_message<CharType>
427 translate(
const std::basic_string<CharType>& single,
const std::basic_string<CharType>& plural,
int n)
437 template<
typename CharType>
438 std::basic_string<CharType>
gettext(
const CharType*
id,
const std::locale& loc = std::locale())
443 template<
typename CharType>
444 std::basic_string<CharType>
445 ngettext(
const CharType* s,
const CharType* p,
int n,
const std::locale& loc = std::locale())
451 template<
typename CharType>
452 std::basic_string<CharType>
dgettext(
const char*
domain,
const CharType*
id,
const std::locale& loc = std::locale())
458 template<
typename CharType>
459 std::basic_string<CharType>
460 dngettext(
const char*
domain,
const CharType* s,
const CharType* p,
int n,
const std::locale& loc = std::locale())
466 template<
typename CharType>
467 std::basic_string<CharType>
468 pgettext(
const CharType* context,
const CharType*
id,
const std::locale& loc = std::locale())
474 template<
typename CharType>
475 std::basic_string<CharType>
npgettext(
const CharType* context,
479 const std::locale& loc = std::locale())
485 template<
typename CharType>
486 std::basic_string<CharType>
487 dpgettext(
const char*
domain,
const CharType* context,
const CharType*
id,
const std::locale& loc = std::locale())
493 template<
typename CharType>
495 const CharType* context,
499 const std::locale& loc = std::locale())
507 struct BOOST_LOCALE_DECL base_message_format<char> :
public std::locale::facet {
508 base_message_format(
size_t refs = 0) : std::locale::facet(refs) {}
509 ~base_message_format();
510 static std::locale::id id;
514 struct BOOST_LOCALE_DECL base_message_format<wchar_t> :
public std::locale::facet {
515 base_message_format(
size_t refs = 0) : std::locale::facet(refs) {}
516 ~base_message_format();
517 static std::locale::id id;
520 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T 523 struct BOOST_LOCALE_DECL base_message_format<char16_t> :
public std::locale::facet {
524 base_message_format(
size_t refs = 0) : std::locale::facet(refs) {}
525 ~base_message_format();
526 static std::locale::id id;
531 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T 534 struct BOOST_LOCALE_DECL base_message_format<char32_t> :
public std::locale::facet {
535 base_message_format(
size_t refs = 0) : std::locale::facet(refs) {}
536 ~base_message_format();
537 static std::locale::id id;
551 template<
typename CharType>
552 std::basic_ostream<CharType>&
operator<<(std::basic_ostream<CharType>& out,
const set_domain& dom)
554 int id = std::use_facet<message_format<CharType>>(out.getloc()).
domain(dom.domain_id);
570 #ifdef BOOST_LOCALE_DOXYGEN 577 detail::set_domain tmp = {
id};
585 # pragma warning(pop) basic_message< CharType > translate(const CharType *msg)
Translate a message, msg is not copied.
Definition: message.hpp:372
message_format(size_t refs=0)
Default constructor.
Definition: message.hpp:56
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:239
std::basic_string< CharType > npgettext(const CharType *context, const CharType *s, const CharType *p, int n, const std::locale &loc=std::locale())
Translate plural form according to locale loc in context context.
Definition: message.hpp:475
std::basic_string< CharType > ngettext(const CharType *s, const CharType *p, int n, const std::locale &loc=std::locale())
Translate plural form according to locale loc.
Definition: message.hpp:445
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:258
basic_message(const char_type *context, const char_type *single, const char_type *plural, int n)
Definition: message.hpp:174
CharType char_type
The character this message object is used with.
Definition: message.hpp:144
basic_message & operator=(const basic_message &other)=default
Assign other message object to this one.
basic_message(const string_type &context, const string_type &single, const string_type &plural, int number)
Definition: message.hpp:196
std::basic_string< char_type > string_type
The string type this object can be used with.
Definition: message.hpp:145
void swap(basic_message &other)
Swap two message objects.
Definition: message.hpp:213
basic_message< wchar_t > wmessage
Convenience typedef for wchar_t.
Definition: message.hpp:349
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:468
This class represents a message that can be converted to a specific locale message.
Definition: message.hpp:142
basic_message< char32_t > u32message
Convenience typedef for char32_t.
Definition: message.hpp:356
string_type str() const
Translate message to a string in the default global locale, using default domain.
Definition: message.hpp:229
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:487
Generate conversion facets.
std::ios_base & number(std::ios_base &ios)
Definition: formatting.hpp:211
CharType char_type
Character type.
Definition: message.hpp:51
basic_message< char > message
Convenience typedef for char.
Definition: message.hpp:347
basic_message(const char_type *single, const char_type *plural, int n)
Definition: message.hpp:159
unspecified_type domain(const std::string &id)
Definition: message.hpp:575
static ios_info & get(std::ios_base &ios)
Get ios_info instance for specific stream object.
basic_message()
Create default empty message.
Definition: message.hpp:149
void write(std::basic_ostream< char_type > &out) const
Definition: message.hpp:271
std::basic_ostream< CharType > & operator<<(std::basic_ostream< CharType > &out, const date_time &t)
Definition: date_time.hpp:719
void domain_id(int)
Set special message domain identification.
basic_message< char16_t > u16message
Convenience typedef for char16_t.
Definition: message.hpp:352
std::basic_string< CharType > string_type
String type.
Definition: message.hpp:53
std::basic_string< CharType > dngettext(const char *domain, const CharType *s, const CharType *p, int n, const std::locale &loc=std::locale())
Translate plural form according to locale loc in domain domain.
Definition: message.hpp:460
basic_message(const string_type &single, const string_type &plural, int number)
Definition: message.hpp:184
message_format< char_type > facet_type
The type of the facet the messages are fetched with.
Definition: message.hpp:146
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:452
basic_message(const char_type *context, const char_type *id)
Definition: message.hpp:166
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:248
basic_message(const string_type &context, const string_type &id)
Create a simple message from a string with context.
Definition: message.hpp:189
std::basic_string< CharType > dnpgettext(const char *domain, const CharType *context, const CharType *s, const CharType *p, int n, const std::locale &loc=std::locale())
Translate plural form according to locale loc in domain domain in context context.
Definition: message.hpp:494
Domain code - for message formatting.
Definition: formatting.hpp:77
string_type str(const std::locale &locale) const
Translate message to a string in the locale locale, using default domain.
Definition: message.hpp:236
basic_message(const char_type *id)
Definition: message.hpp:153
basic_message(const string_type &id)
Create a simple message from a string.
Definition: message.hpp:179
std::basic_string< CharType > gettext(const CharType *id, const std::locale &loc=std::locale())
Translate message id according to locale loc.
Definition: message.hpp:438