2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_LOCALE_MESSAGE_HPP_INCLUDED
9 #define BOOST_LOCALE_MESSAGE_HPP_INCLUDED
11 #include <boost/locale/config.hpp>
13 # pragma warning(push)
14 # pragma warning(disable : 4275 4251 4231 4660)
21 #include <boost/locale/formatting.hpp>
27 /// \defgroup message Message Formatting (translation)
29 ///This module provides message translation functionality, i.e. allow your application to speak native language
36 template<typename CharType>
37 struct base_message_format: public std::locale::facet
44 /// \brief This facet provides message formatting abilities
46 template<typename CharType>
47 class message_format : public base_message_format<CharType>
54 typedef CharType char_type;
58 typedef std::basic_string<CharType> string_type;
61 /// Default constructor
63 message_format(size_t refs = 0) :
64 base_message_format<CharType>(refs)
69 /// This function returns a pointer to the string for a message defined by a \a context
70 /// and identification string \a id. Both create a single key for message lookup in
71 /// a domain defined by \a domain_id.
73 /// If \a context is NULL it is not considered to be a part of the key
75 /// If a translated string is found, it is returned, otherwise NULL is returned
78 virtual char_type const *get(int domain_id,char_type const *context,char_type const *id) const = 0;
80 /// This function returns a pointer to the string for a plural message defined by a \a context
81 /// and identification string \a single_id.
83 /// If \a context is NULL it is not considered to be a part of the key
85 /// Both create a single key for message lookup in
86 /// a domain defined \a domain_id. \a n is used to pick the correct translation string for a specific
89 /// If a translated string is found, it is returned, otherwise NULL is returned
92 virtual char_type const *get(int domain_id,char_type const *context,char_type const *single_id,int n) const = 0;
95 /// Convert a string that defines \a domain to the integer id used by \a get functions
97 virtual int domain(std::string const &domain) const = 0;
100 /// Convert the string \a msg to target locale's encoding. If \a msg is already
101 /// in target encoding it would be returned otherwise the converted
102 /// string is stored in temporary \a buffer and buffer.c_str() is returned.
104 /// Note: for char_type that is char16_t, char32_t and wchar_t it is no-op, returns
107 virtual char_type const *convert(char_type const *msg,string_type &buffer) const = 0;
109 #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
110 std::locale::id& __get_id (void) const { return id; }
113 virtual ~message_format()
122 inline bool is_us_ascii_char(char c)
124 // works for null terminated strings regardless char "signness"
125 return 0<c && c<0x7F;
127 inline bool is_us_ascii_string(char const *msg)
130 if(!is_us_ascii_char(*msg++))
136 template<typename CharType>
137 struct string_cast_traits {
138 static CharType const *cast(CharType const *msg,std::basic_string<CharType> &/*unused*/)
145 struct string_cast_traits<char> {
146 static char const *cast(char const *msg,std::string &buffer)
148 if(is_us_ascii_string(msg))
150 buffer.reserve(strlen(msg));
152 while((c=*msg++)!=0) {
153 if(is_us_ascii_char(c))
156 return buffer.c_str();
164 /// \brief This class represents a message that can be converted to a specific locale message
166 /// It holds the original ASCII string that is queried in the dictionary when converting to the output string.
167 /// The created string may be UTF-8, UTF-16, UTF-32 or other 8-bit encoded string according to the target
168 /// character type and locale encoding.
170 template<typename CharType>
171 class basic_message {
174 typedef CharType char_type; ///< The character this message object is used with
175 typedef std::basic_string<char_type> string_type; ///< The string type this object can be used with
176 typedef message_format<char_type> facet_type; ///< The type of the facet the messages are fetched with
179 /// Create default empty message
190 /// Create a simple message from 0 terminated string. The string should exist
191 /// until the message is destroyed. Generally useful with static constant strings
193 explicit basic_message(char_type const *id) :
202 /// Create a simple plural form message from 0 terminated strings. The strings should exist
203 /// until the message is destroyed. Generally useful with static constant strings.
205 /// \a n is the number, \a single and \a plural are singular and plural forms of the message
207 explicit basic_message(char_type const *single,char_type const *plural,int n) :
216 /// Create a simple message from 0 terminated strings, with context
217 /// information. The string should exist
218 /// until the message is destroyed. Generally useful with static constant strings
220 explicit basic_message(char_type const *context,char_type const *id) :
229 /// Create a simple plural form message from 0 terminated strings, with context. The strings should exist
230 /// until the message is destroyed. Generally useful with static constant strings.
232 /// \a n is the number, \a single and \a plural are singular and plural forms of the message
234 explicit basic_message(char_type const *context,char_type const *single,char_type const *plural,int n) :
244 /// Create a simple message from a string.
246 explicit basic_message(string_type const &id) :
256 /// Create a simple plural form message from strings.
258 /// \a n is the number, \a single and \a plural are single and plural forms of the message
260 explicit basic_message(string_type const &single,string_type const &plural,int number) :
271 /// Create a simple message from a string with context.
273 explicit basic_message(string_type const &context,string_type const &id) :
284 /// Create a simple plural form message from strings.
286 /// \a n is the number, \a single and \a plural are single and plural forms of the message
288 explicit basic_message(string_type const &context,string_type const &single,string_type const &plural,int number) :
302 basic_message(basic_message const &other) :
305 c_context_(other.c_context_),
306 c_plural_(other.c_plural_),
308 context_(other.context_),
309 plural_(other.plural_)
314 /// Assign other message object to this one
316 basic_message const &operator=(basic_message const &other)
321 basic_message tmp(other);
327 /// Swap two message objects
329 void swap(basic_message &other)
331 std::swap(n_,other.n_);
332 std::swap(c_id_,other.c_id_);
333 std::swap(c_context_,other.c_context_);
334 std::swap(c_plural_,other.c_plural_);
337 context_.swap(other.context_);
338 plural_.swap(other.plural_);
342 /// Message class can be explicitly converted to string class
345 operator string_type () const
351 /// Translate message to a string in the default global locale, using default domain
353 string_type str() const
360 /// Translate message to a string in the locale \a locale, using default domain
362 string_type str(std::locale const &locale) const
364 return str(locale,0);
368 /// Translate message to a string using locale \a locale and message domain \a domain_id
370 string_type str(std::locale const &locale,std::string const &domain_id) const
373 if(std::has_facet<facet_type>(locale))
374 id=std::use_facet<facet_type>(locale).domain(domain_id);
375 return str(locale,id);
379 /// Translate message to a string using the default locale and message domain \a domain_id
381 string_type str(std::string const &domain_id) const
385 if(std::has_facet<facet_type>(loc))
386 id=std::use_facet<facet_type>(loc).domain(domain_id);
392 /// Translate message to a string using locale \a loc and message domain index \a id
394 string_type str(std::locale const &loc,int id) const
397 char_type const *ptr = write(loc,id,buffer);
398 if(ptr == buffer.c_str())
407 /// Translate message and write to stream \a out, using imbued locale and domain set to the
410 void write(std::basic_ostream<char_type> &out) const
412 std::locale const &loc = out.getloc();
413 int id = ios_info::get(out).domain_id();
415 out << write(loc,id,buffer);
419 char_type const *plural() const
425 return plural_.c_str();
427 char_type const *context() const
433 return context_.c_str();
436 char_type const *id() const
438 return c_id_ ? c_id_ : id_.c_str();
441 char_type const *write(std::locale const &loc,int domain_id,string_type &buffer) const
443 char_type const *translated = 0;
444 static const char_type empty_string[1] = {0};
446 char_type const *id = this->id();
447 char_type const *context = this->context();
448 char_type const *plural = this->plural();
453 facet_type const *facet = 0;
454 if(std::has_facet<facet_type>(loc))
455 facet = &std::use_facet<facet_type>(loc);
459 translated = facet->get(domain_id,context,id);
462 translated = facet->get(domain_id,context,id,n_);
467 char_type const *msg = plural ? ( n_ == 1 ? id : plural) : id;
470 translated = facet->convert(msg,buffer);
473 translated = details::string_cast_traits<char_type>::cast(msg,buffer);
482 char_type const *c_id_;
483 char_type const *c_context_;
484 char_type const *c_plural_;
486 string_type context_;
492 /// Convenience typedef for char
494 typedef basic_message<char> message;
496 /// Convenience typedef for wchar_t
498 typedef basic_message<wchar_t> wmessage;
499 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
501 /// Convenience typedef for char16_t
503 typedef basic_message<char16_t> u16message;
505 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
507 /// Convenience typedef for char32_t
509 typedef basic_message<char32_t> u32message;
513 /// Translate message \a msg and write it to stream
515 template<typename CharType>
516 std::basic_ostream<CharType> &operator<<(std::basic_ostream<CharType> &out,basic_message<CharType> const &msg)
523 /// \anchor boost_locale_translate_family \name Indirect message translation function family
527 /// \brief Translate a message, \a msg is not copied
529 template<typename CharType>
530 inline basic_message<CharType> translate(CharType const *msg)
532 return basic_message<CharType>(msg);
535 /// \brief Translate a message in context, \a msg and \a context are not copied
537 template<typename CharType>
538 inline basic_message<CharType> translate( CharType const *context,
541 return basic_message<CharType>(context,msg);
544 /// \brief Translate a plural message form, \a single and \a plural are not copied
546 template<typename CharType>
547 inline basic_message<CharType> translate( CharType const *single,
548 CharType const *plural,
551 return basic_message<CharType>(single,plural,n);
554 /// \brief Translate a plural message from in constext, \a context, \a single and \a plural are not copied
556 template<typename CharType>
557 inline basic_message<CharType> translate( CharType const *context,
558 CharType const *single,
559 CharType const *plural,
562 return basic_message<CharType>(context,single,plural,n);
566 /// \brief Translate a message, \a msg is copied
568 template<typename CharType>
569 inline basic_message<CharType> translate(std::basic_string<CharType> const &msg)
571 return basic_message<CharType>(msg);
575 /// \brief Translate a message in context,\a context and \a msg is copied
577 template<typename CharType>
578 inline basic_message<CharType> translate( std::basic_string<CharType> const &context,
579 std::basic_string<CharType> const &msg)
581 return basic_message<CharType>(context,msg);
584 /// \brief Translate a plural message form in constext, \a context, \a single and \a plural are copied
586 template<typename CharType>
587 inline basic_message<CharType> translate( std::basic_string<CharType> const &context,
588 std::basic_string<CharType> const &single,
589 std::basic_string<CharType> const &plural,
592 return basic_message<CharType>(context,single,plural,n);
596 /// \brief Translate a plural message form, \a single and \a plural are copied
599 template<typename CharType>
600 inline basic_message<CharType> translate( std::basic_string<CharType> const &single,
601 std::basic_string<CharType> const &plural,
604 return basic_message<CharType>(single,plural,n);
610 /// \anchor boost_locale_gettext_family \name Direct message translation functions family
614 /// Translate message \a id according to locale \a loc
616 template<typename CharType>
617 std::basic_string<CharType> gettext(CharType const *id,
618 std::locale const &loc=std::locale())
620 return basic_message<CharType>(id).str(loc);
623 /// Translate plural form according to locale \a loc
625 template<typename CharType>
626 std::basic_string<CharType> ngettext( CharType const *s,
629 std::locale const &loc=std::locale())
631 return basic_message<CharType>(s,p,n).str(loc);
634 /// Translate message \a id according to locale \a loc in domain \a domain
636 template<typename CharType>
637 std::basic_string<CharType> dgettext( char const *domain,
639 std::locale const &loc=std::locale())
641 return basic_message<CharType>(id).str(loc,domain);
645 /// Translate plural form according to locale \a loc in domain \a domain
647 template<typename CharType>
648 std::basic_string<CharType> dngettext( char const *domain,
652 std::locale const &loc=std::locale())
654 return basic_message<CharType>(s,p,n).str(loc,domain);
657 /// Translate message \a id according to locale \a loc in context \a context
659 template<typename CharType>
660 std::basic_string<CharType> pgettext( CharType const *context,
662 std::locale const &loc=std::locale())
664 return basic_message<CharType>(context,id).str(loc);
667 /// Translate plural form according to locale \a loc in context \a context
669 template<typename CharType>
670 std::basic_string<CharType> npgettext( CharType const *context,
674 std::locale const &loc=std::locale())
676 return basic_message<CharType>(context,s,p,n).str(loc);
679 /// Translate message \a id according to locale \a loc in domain \a domain in context \a context
681 template<typename CharType>
682 std::basic_string<CharType> dpgettext( char const *domain,
683 CharType const *context,
685 std::locale const &loc=std::locale())
687 return basic_message<CharType>(context,id).str(loc,domain);
690 /// Translate plural form according to locale \a loc in domain \a domain in context \a context
692 template<typename CharType>
693 std::basic_string<CharType> dnpgettext(char const *domain,
694 CharType const *context,
698 std::locale const &loc=std::locale())
700 return basic_message<CharType>(context,s,p,n).str(loc,domain);
708 struct BOOST_LOCALE_DECL base_message_format<char> : public std::locale::facet
710 base_message_format(size_t refs = 0) : std::locale::facet(refs)
713 static std::locale::id id;
717 struct BOOST_LOCALE_DECL base_message_format<wchar_t> : public std::locale::facet
719 base_message_format(size_t refs = 0) : std::locale::facet(refs)
722 static std::locale::id id;
725 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
728 struct BOOST_LOCALE_DECL base_message_format<char16_t> : public std::locale::facet
730 base_message_format(size_t refs = 0) : std::locale::facet(refs)
733 static std::locale::id id;
738 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
741 struct BOOST_LOCALE_DECL base_message_format<char32_t> : public std::locale::facet
743 base_message_format(size_t refs = 0) : std::locale::facet(refs)
746 static std::locale::id id;
761 std::string domain_id;
763 template<typename CharType>
764 std::basic_ostream<CharType> &operator<<(std::basic_ostream<CharType> &out, set_domain const &dom)
766 int id = std::use_facet<message_format<CharType> >(out.getloc()).domain(dom.domain_id);
767 ios_info::get(out).domain_id(id);
774 /// \addtogroup manipulators
779 /// Manipulator for switching message domain in ostream,
781 /// \note The returned object throws std::bad_cast if the I/O stream does not have \ref message_format facet installed
784 #ifdef BOOST_LOCALE_DOXYGEN
789 domain(std::string const &id)
791 details::set_domain tmp = { id };
806 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4