gnu_gettext.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  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)
  7. //
  8. #ifndef BOOST_LOCLAE_GNU_GETTEXT_HPP
  9. #define BOOST_LOCLAE_GNU_GETTEXT_HPP
  10. #include <boost/locale/message.hpp>
  11. #include <boost/function.hpp>
  12. #include <stdexcept>
  13. namespace boost {
  14. namespace locale {
  15. /// \addtogroup message
  16. /// @{
  17. ///
  18. /// \brief This namespace holds classes that provide GNU Gettext message catalogs support.
  19. ///
  20. namespace gnu_gettext {
  21. ///
  22. /// \brief This structure holds all information required for creating gnu-gettext message catalogs,
  23. ///
  24. /// The user is expected to set its parameters to load these catalogs correctly. This structure
  25. /// also allows providing functions for charset conversion. Note, you need to provide them,
  26. /// so this structure is not useful for wide characters without subclassing and it will also
  27. /// ignore gettext catalogs that use a charset different from \a encoding.
  28. ///
  29. struct messages_info {
  30. messages_info() :
  31. language("C"),
  32. locale_category("LC_MESSAGES")
  33. {
  34. }
  35. std::string language; ///< The language we load the catalog for, like "ru", "en", "de"
  36. std::string country; ///< The country we load the catalog for, like "US", "IL"
  37. std::string variant; ///< Language variant, like "euro" so it would look for catalog like de_DE\@euro
  38. std::string encoding; ///< Required target charset encoding. Ignored for wide characters.
  39. ///< For narrow, should specify the correct encoding required for this catalog
  40. std::string locale_category; ///< Locale category, is set by default to LC_MESSAGES, but may be changed
  41. ///
  42. /// \brief This type represents GNU Gettext domain name for the messages.
  43. ///
  44. /// It consists of two parameters:
  45. ///
  46. /// - name - the name of the domain - used for opening the file name
  47. /// - encoding - the encoding of the keys in the sources, default - UTF-8
  48. ///
  49. struct domain {
  50. std::string name; ///< The name of the domain
  51. std::string encoding; ///< The character encoding for the domain
  52. domain() {}
  53. ///
  54. /// Create a domain object from the name that can hold an encoding after symbol "/"
  55. /// such that if n is "hello/cp1255" then the name would be "hello" and "encoding" would
  56. /// be "cp1255" and if n is "hello" then the name would be the same but encoding would be
  57. /// "UTF-8"
  58. ///
  59. domain(std::string const &n)
  60. {
  61. size_t pos = n.find("/");
  62. if(pos==std::string::npos) {
  63. name = n;
  64. encoding = "UTF-8";
  65. }
  66. else {
  67. name = n.substr(0,pos);
  68. encoding = n.substr(pos+1);
  69. }
  70. }
  71. ///
  72. /// Check whether two objects are equivalent, only names are compared, encoding is ignored
  73. ///
  74. bool operator==(domain const &other) const
  75. {
  76. return name==other.name;
  77. }
  78. ///
  79. /// Check whether two objects are distinct, only names are compared, encoding is ignored
  80. ///
  81. bool operator!=(domain const &other) const
  82. {
  83. return !(*this==other);
  84. }
  85. };
  86. typedef std::vector<domain> domains_type; ///< Type that defines a list of domains that are loaded
  87. ///< The first one is the default one
  88. domains_type domains; ///< Message domains - application name, like my_app. So files named my_app.mo
  89. ///< would be loaded
  90. std::vector<std::string> paths; ///< Paths to search files in. Under MS Windows it uses encoding
  91. ///< parameter to convert them to wide OS specific paths.
  92. ///
  93. /// The callback for custom file system support. This callback should read the file named \a file_name
  94. /// encoded in \a encoding character set into std::vector<char> and return it.
  95. ///
  96. /// - If the file does not exist, it should return an empty vector.
  97. /// - If a error occurs during file read it should throw a error.
  98. ///
  99. /// \note The user should support only the encodings the locales are created for. So if the user
  100. /// uses only one encoding or the file system is encoding agnostic, he may ignore the \a encoding parameter.
  101. ///
  102. typedef function<
  103. std::vector<char>(
  104. std::string const &file_name,
  105. std::string const &encoding
  106. )
  107. > callback_type;
  108. ///
  109. /// The callback for handling custom file systems, if it is empty, the real OS file-system
  110. /// is being used.
  111. ///
  112. callback_type callback;
  113. };
  114. ///
  115. /// Create a message_format facet using GNU Gettext catalogs. It uses \a info structure to get
  116. /// information about where to read them from and uses it for character set conversion (if needed)
  117. ///
  118. template<typename CharType>
  119. message_format<CharType> *create_messages_facet(messages_info const &info);
  120. /// \cond INTERNAL
  121. template<>
  122. BOOST_LOCALE_DECL message_format<char> *create_messages_facet(messages_info const &info);
  123. template<>
  124. BOOST_LOCALE_DECL message_format<wchar_t> *create_messages_facet(messages_info const &info);
  125. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  126. template<>
  127. BOOST_LOCALE_DECL message_format<char16_t> *create_messages_facet(messages_info const &info);
  128. #endif
  129. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  130. template<>
  131. BOOST_LOCALE_DECL message_format<char32_t> *create_messages_facet(messages_info const &info);
  132. #endif
  133. /// \endcond
  134. } // gnu_gettext
  135. /// @}
  136. } // locale
  137. } // boost
  138. #endif
  139. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4