generator.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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_LOCALE_GENERATOR_HPP
  9. #define BOOST_LOCALE_GENERATOR_HPP
  10. #include <boost/locale/config.hpp>
  11. #include <boost/cstdint.hpp>
  12. #ifdef BOOST_MSVC
  13. # pragma warning(push)
  14. # pragma warning(disable : 4275 4251 4231 4660)
  15. #endif
  16. #include <string>
  17. #include <locale>
  18. #include <memory>
  19. #include <boost/locale/hold_ptr.hpp>
  20. namespace boost {
  21. template<typename Type>
  22. class shared_ptr;
  23. ///
  24. /// \brief This is the main namespace that encloses all localization classes
  25. ///
  26. namespace locale {
  27. class localization_backend;
  28. class localization_backend_manager;
  29. static const uint32_t nochar_facet = 0; ///< Unspecified character category for character independent facets
  30. static const uint32_t char_facet = 1 << 0; ///< 8-bit character facets
  31. static const uint32_t wchar_t_facet = 1 << 1; ///< wide character facets
  32. static const uint32_t char16_t_facet = 1 << 2; ///< C++0x char16_t facets
  33. static const uint32_t char32_t_facet = 1 << 3; ///< C++0x char32_t facets
  34. static const uint32_t character_first_facet = char_facet; ///< First facet specific for character type
  35. static const uint32_t character_last_facet = char32_t_facet; ///< Last facet specific for character type
  36. static const uint32_t all_characters = 0xFFFF; ///< Special mask -- generate all
  37. typedef uint32_t character_facet_type; ///<type that specifies the character type that locales can be generated for
  38. static const uint32_t convert_facet = 1 << 0; ///< Generate conversion facets
  39. static const uint32_t collation_facet = 1 << 1; ///< Generate collation facets
  40. static const uint32_t formatting_facet= 1 << 2; ///< Generate numbers, currency, date-time formatting facets
  41. static const uint32_t parsing_facet = 1 << 3; ///< Generate numbers, currency, date-time formatting facets
  42. static const uint32_t message_facet = 1 << 4; ///< Generate message facets
  43. static const uint32_t codepage_facet = 1 << 5; ///< Generate character set conversion facets (derived from std::codecvt)
  44. static const uint32_t boundary_facet = 1 << 6; ///< Generate boundary analysis facet
  45. static const uint32_t per_character_facet_first = convert_facet; ///< First facet specific for character
  46. static const uint32_t per_character_facet_last = boundary_facet; ///< Last facet specific for character
  47. static const uint32_t calendar_facet = 1 << 16; ///< Generate boundary analysis facet
  48. static const uint32_t information_facet = 1 << 17; ///< Generate general locale information facet
  49. static const uint32_t non_character_facet_first = calendar_facet; ///< First character independent facet
  50. static const uint32_t non_character_facet_last = information_facet;///< Last character independent facet
  51. static const uint32_t all_categories = 0xFFFFFFFFu; ///< Generate all of them
  52. typedef uint32_t locale_category_type; ///< a type used for more fine grained generation of facets
  53. ///
  54. /// \brief the major class used for locale generation
  55. ///
  56. /// This class is used for specification of all parameters required for locale generation and
  57. /// caching. This class const member functions are thread safe if locale class implementation is thread safe.
  58. ///
  59. class BOOST_LOCALE_DECL generator {
  60. public:
  61. ///
  62. /// Create new generator using global localization_backend_manager
  63. ///
  64. generator();
  65. ///
  66. /// Create new generator using specific localization_backend_manager
  67. ///
  68. generator(localization_backend_manager const &);
  69. ~generator();
  70. ///
  71. /// Set types of facets that should be generated, default all
  72. ///
  73. void categories(locale_category_type cats);
  74. ///
  75. /// Get types of facets that should be generated, default all
  76. ///
  77. locale_category_type categories() const;
  78. ///
  79. /// Set the characters type for which the facets should be generated, default all supported
  80. ///
  81. void characters(character_facet_type chars);
  82. ///
  83. /// Get the characters type for which the facets should be generated, default all supported
  84. ///
  85. character_facet_type characters() const;
  86. ///
  87. /// Add a new domain of messages that would be generated. It should be set in order to enable
  88. /// messages support.
  89. ///
  90. /// Messages domain has following format: "name" or "name/encoding"
  91. /// where name is the base name of the "mo" file where the catalog is stored
  92. /// without ".mo" extension. For example for file \c /usr/share/locale/he/LC_MESSAGES/blog.mo
  93. /// it would be \c blog.
  94. ///
  95. /// You can optionally specify the encoding of the keys in the sources by adding "/encoding_name"
  96. /// For example blog/cp1255.
  97. ///
  98. /// If not defined all keys are assumed to be UTF-8 encoded.
  99. ///
  100. /// \note When you select a domain for the program using dgettext or message API, you
  101. /// do not specify the encoding part. So for example if the provided
  102. /// domain name was "blog/windows-1255" then for translation
  103. /// you should use dgettext("blog","Hello")
  104. ///
  105. ///
  106. void add_messages_domain(std::string const &domain);
  107. ///
  108. /// Set default message domain. If this member was not called, the first added messages domain is used.
  109. /// If the domain \a domain is not added yet it is added.
  110. ///
  111. void set_default_messages_domain(std::string const &domain);
  112. ///
  113. /// Remove all added domains from the list
  114. ///
  115. void clear_domains();
  116. ///
  117. /// Add a search path where dictionaries are looked in.
  118. ///
  119. /// \note
  120. ///
  121. /// - Under the Windows platform the path is treated as a path in the locale's encoding so
  122. /// if you create locale "en_US.windows-1251" then path would be treated as cp1255,
  123. /// and if it is en_US.UTF-8 it is treated as UTF-8. File name is always opened with
  124. /// a wide file name as wide file names are the native file name on Windows.
  125. ///
  126. /// - Under POSIX platforms all paths passed as-is regardless of encoding as narrow
  127. /// encodings are the native encodings for POSIX platforms.
  128. ///
  129. ///
  130. void add_messages_path(std::string const &path);
  131. ///
  132. /// Remove all added paths
  133. ///
  134. void clear_paths();
  135. ///
  136. /// Remove all cached locales
  137. ///
  138. void clear_cache();
  139. ///
  140. /// Turn locale caching ON
  141. ///
  142. void locale_cache_enabled(bool on);
  143. ///
  144. /// Get locale cache option
  145. ///
  146. bool locale_cache_enabled() const;
  147. ///
  148. /// Check if by default ANSI encoding is selected or UTF-8 onces. The default is false.
  149. ///
  150. bool use_ansi_encoding() const;
  151. ///
  152. /// Select ANSI encodings as default system encoding rather then UTF-8 by default
  153. /// under Windows.
  154. ///
  155. /// The default is the most portable and most powerful encoding, UTF-8, but the user
  156. /// can select "system" one if dealing with legacy applications
  157. ///
  158. void use_ansi_encoding(bool enc);
  159. ///
  160. /// Generate a locale with id \a id
  161. ///
  162. std::locale generate(std::string const &id) const;
  163. ///
  164. /// Generate a locale with id \a id. Use \a base as a locale to which all facets are added,
  165. /// instead of std::locale::classic().
  166. ///
  167. std::locale generate(std::locale const &base,std::string const &id) const;
  168. ///
  169. /// Shortcut to generate(id)
  170. ///
  171. std::locale operator()(std::string const &id) const
  172. {
  173. return generate(id);
  174. }
  175. ///
  176. /// Set backend specific option
  177. ///
  178. void set_option(std::string const &name,std::string const &value);
  179. ///
  180. /// Clear backend specific options
  181. ///
  182. void clear_options();
  183. private:
  184. void set_all_options(shared_ptr<localization_backend> backend,std::string const &id) const;
  185. generator(generator const &);
  186. void operator=(generator const &);
  187. struct data;
  188. hold_ptr<data> d;
  189. };
  190. }
  191. }
  192. #ifdef BOOST_MSVC
  193. #pragma warning(pop)
  194. #endif
  195. #endif
  196. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4