message.hpp 21.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
//
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#ifndef BOOST_LOCALE_MESSAGE_HPP_INCLUDED
#define BOOST_LOCALE_MESSAGE_HPP_INCLUDED

#include <boost/locale/formatting.hpp>
#include <boost/locale/util/string.hpp>
#include <locale>
#include <memory>
#include <set>
#include <string>

#ifdef BOOST_MSVC
#    pragma warning(push)
#    pragma warning(disable : 4275 4251 4231 4660)
#endif

// glibc < 2.3.4 declares those as macros if compiled with optimization turned on
#ifdef gettext
#    undef gettext
#    undef ngettext
#    undef dgettext
#    undef dngettext
#endif

namespace boost { namespace locale {
    ///
    /// \defgroup message Message Formatting (translation)
    ///
    /// This module provides message translation functionality, i.e. allow your application to speak native language
    ///
    /// @{
    ///

    /// \cond INTERNAL

    template<typename CharType>
    struct base_message_format;

    /// \endcond

    /// \brief This facet provides message formatting abilities
    template<typename CharType>
    class BOOST_SYMBOL_VISIBLE message_format : public base_message_format<CharType> {
    public:
        /// Character type
        typedef CharType char_type;
        /// String type
        typedef std::basic_string<CharType> string_type;

        /// Default constructor
        message_format(size_t refs = 0) : base_message_format<CharType>(refs) {}

        /// This function returns a pointer to the string for a message defined by a \a context
        /// and identification string \a id. Both create a single key for message lookup in
        /// a domain defined by \a domain_id.
        ///
        /// If \a context is NULL it is not considered to be a part of the key
        ///
        /// If a translated string is found, it is returned, otherwise NULL is returned
        virtual const char_type* get(int domain_id, const char_type* context, const char_type* id) const = 0;

        /// This function returns a pointer to the string for a plural message defined by a \a context
        /// and identification string \a single_id.
        ///
        /// If \a context is NULL it is not considered to be a part of the key
        ///
        /// Both create a single key for message lookup in
        /// a domain defined \a domain_id. \a n is used to pick the correct translation string for a specific
        /// number.
        ///
        /// If a translated string is found, it is returned, otherwise NULL is returned
        virtual const char_type*
        get(int domain_id, const char_type* context, const char_type* single_id, int n) const = 0;

        /// Convert a string that defines \a domain to the integer id used by \a get functions
        virtual int domain(const std::string& domain) const = 0;

        /// Convert the string \a msg to target locale's encoding. If \a msg is already
        /// in target encoding it would be returned otherwise the converted
        /// string is stored in temporary \a buffer and buffer.c_str() is returned.
        ///
        /// Note: for char_type that is char16_t, char32_t and wchar_t it is no-op, returns
        /// msg
        virtual const char_type* convert(const char_type* msg, string_type& buffer) const = 0;

    protected:
        virtual ~message_format() = default;
    };

    /// \cond INTERNAL

    namespace detail {
        inline bool is_us_ascii_char(char c)
        {
            // works for null terminated strings regardless char "signedness"
            return 0 < c && c < 0x7F;
        }
        inline bool is_us_ascii_string(const char* msg)
        {
            while(*msg) {
                if(!is_us_ascii_char(*msg++))
                    return false;
            }
            return true;
        }

        template<typename CharType>
        struct string_cast_traits {
            static const CharType* cast(const CharType* msg, std::basic_string<CharType>& /*unused*/) { return msg; }
        };

        template<>
        struct string_cast_traits<char> {
            static const char* cast(const char* msg, std::string& buffer)
            {
                if(is_us_ascii_string(msg))
                    return msg;
                buffer.reserve(strlen(msg));
                char c;
                while((c = *msg++) != 0) {
                    if(is_us_ascii_char(c))
                        buffer += c;
                }
                return buffer.c_str();
            }
        };
    } // namespace detail

    /// \endcond

    /// \brief This class represents a message that can be converted to a specific locale message
    ///
    /// It holds the original ASCII string that is queried in the dictionary when converting to the output string.
    /// The created string may be UTF-8, UTF-16, UTF-32 or other 8-bit encoded string according to the target
    /// character type and locale encoding.
    template<typename CharType>
    class basic_message {
    public:
        typedef CharType char_type;                       ///< The character this message object is used with
        typedef std::basic_string<char_type> string_type; ///< The string type this object can be used with
        typedef message_format<char_type> facet_type;     ///< The type of the facet the messages are fetched with

        /// Create default empty message
        basic_message() : n_(0), c_id_(0), c_context_(0), c_plural_(0) {}

        /// Create a simple message from 0 terminated string. The string should exist
        /// until the message is destroyed. Generally useful with static constant strings
        explicit basic_message(const char_type* id) : n_(0), c_id_(id), c_context_(0), c_plural_(0) {}

        /// Create a simple plural form message from 0 terminated strings. The strings should exist
        /// until the message is destroyed. Generally useful with static constant strings.
        ///
        /// \a n is the number, \a single and \a plural are singular and plural forms of the message
        explicit basic_message(const char_type* single, const char_type* plural, int n) :
            n_(n), c_id_(single), c_context_(0), c_plural_(plural)
        {}

        /// Create a simple message from 0 terminated strings, with context
        /// information. The string should exist
        /// until the message is destroyed. Generally useful with static constant strings
        explicit basic_message(const char_type* context, const char_type* id) :
            n_(0), c_id_(id), c_context_(context), c_plural_(0)
        {}

        /// Create a simple plural form message from 0 terminated strings, with context. The strings should exist
        /// until the message is destroyed. Generally useful with static constant strings.
        ///
        /// \a n is the number, \a single and \a plural are singular and plural forms of the message
        explicit basic_message(const char_type* context, const char_type* single, const char_type* plural, int n) :
            n_(n), c_id_(single), c_context_(context), c_plural_(plural)
        {}

        /// Create a simple message from a string.
        explicit basic_message(const string_type& id) : n_(0), c_id_(0), c_context_(0), c_plural_(0), id_(id) {}

        /// Create a simple plural form message from strings.
        ///
        /// \a n is the number, \a single and \a plural are single and plural forms of the message
        explicit basic_message(const string_type& single, const string_type& plural, int number) :
            n_(number), c_id_(0), c_context_(0), c_plural_(0), id_(single), plural_(plural)
        {}

        /// Create a simple message from a string with context.
        explicit basic_message(const string_type& context, const string_type& id) :
            n_(0), c_id_(0), c_context_(0), c_plural_(0), id_(id), context_(context)
        {}

        /// Create a simple plural form message from strings.
        ///
        /// \a n is the number, \a single and \a plural are single and plural forms of the message
        explicit basic_message(const string_type& context,
                               const string_type& single,
                               const string_type& plural,
                               int number) :
            n_(number),
            c_id_(0), c_context_(0), c_plural_(0), id_(single), context_(context), plural_(plural)
        {}

        /// Copy an object
        basic_message(const basic_message& other) = default;
        basic_message(basic_message&& other) = default;

        /// Assign other message object to this one
        basic_message& operator=(const basic_message& other) = default;
        basic_message& operator=(basic_message&& other) = default;

        /// Swap two message objects
        void swap(basic_message& other)
        {
            std::swap(n_, other.n_);
            std::swap(c_id_, other.c_id_);
            std::swap(c_context_, other.c_context_);
            std::swap(c_plural_, other.c_plural_);

            id_.swap(other.id_);
            context_.swap(other.context_);
            plural_.swap(other.plural_);
        }

        /// Message class can be explicitly converted to string class
        operator string_type() const { return str(); }

        /// Translate message to a string in the default global locale, using default domain
        string_type str() const
        {
            std::locale loc;
            return str(loc, 0);
        }

        /// Translate message to a string in the locale \a locale, using default domain
        string_type str(const std::locale& locale) const { return str(locale, 0); }

        /// Translate message to a string using locale \a locale and message domain  \a domain_id
        string_type str(const std::locale& locale, const std::string& domain_id) const
        {
            int id = 0;
            if(std::has_facet<facet_type>(locale))
                id = std::use_facet<facet_type>(locale).domain(domain_id);
            return str(locale, id);
        }

        /// Translate message to a string using the default locale and message domain  \a domain_id
        string_type str(const std::string& domain_id) const
        {
            int id = 0;
            std::locale loc;
            if(std::has_facet<facet_type>(loc))
                id = std::use_facet<facet_type>(loc).domain(domain_id);
            return str(loc, id);
        }

        /// Translate message to a string using locale \a loc and message domain index  \a id
        string_type str(const std::locale& loc, int id) const
        {
            string_type buffer;
            const char_type* ptr = write(loc, id, buffer);
            if(ptr == buffer.c_str())
                return buffer;
            else
                buffer = ptr;
            return buffer;
        }

        /// Translate message and write to stream \a out, using imbued locale and domain set to the
        /// stream
        void write(std::basic_ostream<char_type>& out) const
        {
            const std::locale& loc = out.getloc();
            int id = ios_info::get(out).domain_id();
            string_type buffer;
            out << write(loc, id, buffer);
        }

    private:
        const char_type* plural() const
        {
            if(c_plural_)
                return c_plural_;
            if(plural_.empty())
                return 0;
            return plural_.c_str();
        }
        const char_type* context() const
        {
            if(c_context_)
                return c_context_;
            if(context_.empty())
                return 0;
            return context_.c_str();
        }

        const char_type* id() const { return c_id_ ? c_id_ : id_.c_str(); }

        const char_type* write(const std::locale& loc, int domain_id, string_type& buffer) const
        {
            const char_type* translated = 0;
            static const char_type empty_string[1] = {0};

            const char_type* id = this->id();
            const char_type* context = this->context();
            const char_type* plural = this->plural();

            if(*id == 0)
                return empty_string;

            const facet_type* facet = 0;
            if(std::has_facet<facet_type>(loc))
                facet = &std::use_facet<facet_type>(loc);

            if(facet) {
                if(!plural) {
                    translated = facet->get(domain_id, context, id);
                } else {
                    translated = facet->get(domain_id, context, id, n_);
                }
            }

            if(!translated) {
                const char_type* msg = plural ? (n_ == 1 ? id : plural) : id;

                if(facet) {
                    translated = facet->convert(msg, buffer);
                } else {
                    translated = detail::string_cast_traits<char_type>::cast(msg, buffer);
                }
            }
            return translated;
        }

        /// members

        int n_;
        const char_type* c_id_;
        const char_type* c_context_;
        const char_type* c_plural_;
        string_type id_;
        string_type context_;
        string_type plural_;
    };

    /// Convenience typedef for char
    typedef basic_message<char> message;
    /// Convenience typedef for wchar_t
    typedef basic_message<wchar_t> wmessage;
#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
    /// Convenience typedef for char16_t
    typedef basic_message<char16_t> u16message;
#endif
#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
    /// Convenience typedef for char32_t
    typedef basic_message<char32_t> u32message;
#endif

    /// Translate message \a msg and write it to stream
    template<typename CharType>
    std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const basic_message<CharType>& msg)
    {
        msg.write(out);
        return out;
    }

    /// \anchor boost_locale_translate_family \name Indirect message translation function family
    /// @{

    /// \brief Translate a message, \a msg is not copied
    template<typename CharType>
    inline basic_message<CharType> translate(const CharType* msg)
    {
        return basic_message<CharType>(msg);
    }

    /// \brief Translate a message in context, \a msg and \a context are not copied
    template<typename CharType>
    inline basic_message<CharType> translate(const CharType* context, const CharType* msg)
    {
        return basic_message<CharType>(context, msg);
    }

    /// \brief Translate a plural message form, \a single and \a plural are not copied
    template<typename CharType>
    inline basic_message<CharType> translate(const CharType* single, const CharType* plural, int n)
    {
        return basic_message<CharType>(single, plural, n);
    }

    /// \brief Translate a plural message from in constext, \a context, \a single and \a plural are not copied
    template<typename CharType>
    inline basic_message<CharType>
    translate(const CharType* context, const CharType* single, const CharType* plural, int n)
    {
        return basic_message<CharType>(context, single, plural, n);
    }

    /// \brief Translate a message, \a msg is copied
    template<typename CharType>
    inline basic_message<CharType> translate(const std::basic_string<CharType>& msg)
    {
        return basic_message<CharType>(msg);
    }

    /// \brief Translate a message in context,\a context and \a msg is copied
    template<typename CharType>
    inline basic_message<CharType> translate(const std::basic_string<CharType>& context,
                                             const std::basic_string<CharType>& msg)
    {
        return basic_message<CharType>(context, msg);
    }

    /// \brief Translate a plural message form in constext, \a context, \a single and \a plural are copied
    template<typename CharType>
    inline basic_message<CharType> translate(const std::basic_string<CharType>& context,
                                             const std::basic_string<CharType>& single,
                                             const std::basic_string<CharType>& plural,
                                             int n)
    {
        return basic_message<CharType>(context, single, plural, n);
    }

    /// \brief Translate a plural message form, \a single and \a plural are copied
    template<typename CharType>
    inline basic_message<CharType>
    translate(const std::basic_string<CharType>& single, const std::basic_string<CharType>& plural, int n)
    {
        return basic_message<CharType>(single, plural, n);
    }

    /// @}

    /// \anchor boost_locale_gettext_family \name Direct message translation functions family

    /// Translate message \a id according to locale \a loc
    template<typename CharType>
    std::basic_string<CharType> gettext(const CharType* id, const std::locale& loc = std::locale())
    {
        return basic_message<CharType>(id).str(loc);
    }
    /// Translate plural form according to locale \a loc
    template<typename CharType>
    std::basic_string<CharType>
    ngettext(const CharType* s, const CharType* p, int n, const std::locale& loc = std::locale())
    {
        return basic_message<CharType>(s, p, n).str(loc);
    }

    /// Translate message \a id according to locale \a loc in domain \a domain
    template<typename CharType>
    std::basic_string<CharType> dgettext(const char* domain, const CharType* id, const std::locale& loc = std::locale())
    {
        return basic_message<CharType>(id).str(loc, domain);
    }

    /// Translate plural form according to locale \a loc in domain \a domain
    template<typename CharType>
    std::basic_string<CharType>
    dngettext(const char* domain, const CharType* s, const CharType* p, int n, const std::locale& loc = std::locale())
    {
        return basic_message<CharType>(s, p, n).str(loc, domain);
    }

    /// Translate message \a id according to locale \a loc in context \a context
    template<typename CharType>
    std::basic_string<CharType>
    pgettext(const CharType* context, const CharType* id, const std::locale& loc = std::locale())
    {
        return basic_message<CharType>(context, id).str(loc);
    }

    /// Translate plural form according to locale \a loc in context \a context
    template<typename CharType>
    std::basic_string<CharType> npgettext(const CharType* context,
                                          const CharType* s,
                                          const CharType* p,
                                          int n,
                                          const std::locale& loc = std::locale())
    {
        return basic_message<CharType>(context, s, p, n).str(loc);
    }

    /// Translate message \a id according to locale \a loc in domain \a domain in context \a context
    template<typename CharType>
    std::basic_string<CharType>
    dpgettext(const char* domain, const CharType* context, const CharType* id, const std::locale& loc = std::locale())
    {
        return basic_message<CharType>(context, id).str(loc, domain);
    }

    /// Translate plural form according to locale \a loc in domain \a domain in context \a context
    template<typename CharType>
    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())
    {
        return basic_message<CharType>(context, s, p, n).str(loc, domain);
    }

    /// \cond INTERNAL

    template<>
    struct BOOST_LOCALE_DECL base_message_format<char> : public std::locale::facet {
        base_message_format(size_t refs = 0) : std::locale::facet(refs) {}
        ~base_message_format();
        static std::locale::id id;
    };

    template<>
    struct BOOST_LOCALE_DECL base_message_format<wchar_t> : public std::locale::facet {
        base_message_format(size_t refs = 0) : std::locale::facet(refs) {}
        ~base_message_format();
        static std::locale::id id;
    };

#ifdef BOOST_LOCALE_ENABLE_CHAR16_T

    template<>
    struct BOOST_LOCALE_DECL base_message_format<char16_t> : public std::locale::facet {
        base_message_format(size_t refs = 0) : std::locale::facet(refs) {}
        ~base_message_format();
        static std::locale::id id;
    };

#endif

#ifdef BOOST_LOCALE_ENABLE_CHAR32_T

    template<>
    struct BOOST_LOCALE_DECL base_message_format<char32_t> : public std::locale::facet {
        base_message_format(size_t refs = 0) : std::locale::facet(refs) {}
        ~base_message_format();
        static std::locale::id id;
    };

#endif

    /// \endcond
    /// @}

    namespace as {
        /// \cond INTERNAL
        namespace detail {
            struct set_domain {
                std::string domain_id;
            };
            template<typename CharType>
            std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const set_domain& dom)
            {
                int id = std::use_facet<message_format<CharType>>(out.getloc()).domain(dom.domain_id);
                ios_info::get(out).domain_id(id);
                return out;
            }
        } // namespace detail
        /// \endcond

        /// \addtogroup manipulators
        ///
        /// @{

        /// Manipulator for switching message domain in ostream,
        ///
        /// \note The returned object throws std::bad_cast if the I/O stream does not have \ref message_format facet
        /// installed
        inline
#ifdef BOOST_LOCALE_DOXYGEN
          unspecified_type
#else
          detail::set_domain
#endif
          domain(const std::string& id)
        {
            detail::set_domain tmp = {id};
            return tmp;
        }
        /// @}
    } // namespace as
}}    // namespace boost::locale

#ifdef BOOST_MSVC
#    pragma warning(pop)
#endif

#endif