win_backend.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #define BOOST_LOCALE_SOURCE
  9. #include <boost/locale/localization_backend.hpp>
  10. #include <boost/locale/gnu_gettext.hpp>
  11. #include <boost/locale/info.hpp>
  12. #include "all_generator.hpp"
  13. #include "win_backend.hpp"
  14. #include <boost/locale/util.hpp>
  15. #include "../util/gregorian.hpp"
  16. #include "../util/locale_data.hpp"
  17. #include "api.hpp"
  18. #include <algorithm>
  19. #include <iterator>
  20. namespace boost {
  21. namespace locale {
  22. namespace impl_win {
  23. class winapi_localization_backend : public localization_backend {
  24. public:
  25. winapi_localization_backend() :
  26. invalid_(true)
  27. {
  28. }
  29. winapi_localization_backend(winapi_localization_backend const &other) :
  30. localization_backend(),
  31. paths_(other.paths_),
  32. domains_(other.domains_),
  33. locale_id_(other.locale_id_),
  34. invalid_(true)
  35. {
  36. }
  37. virtual winapi_localization_backend *clone() const
  38. {
  39. return new winapi_localization_backend(*this);
  40. }
  41. void set_option(std::string const &name,std::string const &value)
  42. {
  43. invalid_ = true;
  44. if(name=="locale")
  45. locale_id_ = value;
  46. else if(name=="message_path")
  47. paths_.push_back(value);
  48. else if(name=="message_application")
  49. domains_.push_back(value);
  50. }
  51. void clear_options()
  52. {
  53. invalid_ = true;
  54. locale_id_.clear();
  55. paths_.clear();
  56. domains_.clear();
  57. }
  58. void prepare_data()
  59. {
  60. if(!invalid_)
  61. return;
  62. invalid_ = false;
  63. if(locale_id_.empty()) {
  64. real_id_ = util::get_system_locale(true); // always UTF-8
  65. lc_ = winlocale(real_id_);
  66. }
  67. else {
  68. lc_=winlocale(locale_id_);
  69. real_id_ = locale_id_;
  70. }
  71. util::locale_data d;
  72. d.parse(real_id_);
  73. if(!d.utf8) {
  74. lc_ = winlocale();
  75. // Make it C as non-UTF8 locales are not supported
  76. }
  77. }
  78. virtual std::locale install(std::locale const &base,
  79. locale_category_type category,
  80. character_facet_type type = nochar_facet)
  81. {
  82. prepare_data();
  83. switch(category) {
  84. case convert_facet:
  85. return create_convert(base,lc_,type);
  86. case collation_facet:
  87. return create_collate(base,lc_,type);
  88. case formatting_facet:
  89. return create_formatting(base,lc_,type);
  90. case parsing_facet:
  91. return create_parsing(base,lc_,type);
  92. case calendar_facet:
  93. {
  94. util::locale_data inf;
  95. inf.parse(real_id_);
  96. return util::install_gregorian_calendar(base,inf.country);
  97. }
  98. case message_facet:
  99. {
  100. gnu_gettext::messages_info minf;
  101. std::locale tmp=util::create_info(std::locale::classic(),real_id_);
  102. boost::locale::info const &inf=std::use_facet<boost::locale::info>(tmp);
  103. minf.language = inf.language();
  104. minf.country = inf.country();
  105. minf.variant = inf.variant();
  106. minf.encoding = inf.encoding();
  107. std::copy(domains_.begin(),domains_.end(),std::back_inserter<gnu_gettext::messages_info::domains_type>(minf.domains));
  108. minf.paths = paths_;
  109. switch(type) {
  110. case char_facet:
  111. return std::locale(base,gnu_gettext::create_messages_facet<char>(minf));
  112. case wchar_t_facet:
  113. return std::locale(base,gnu_gettext::create_messages_facet<wchar_t>(minf));
  114. default:
  115. return base;
  116. }
  117. }
  118. case information_facet:
  119. return util::create_info(base,real_id_);
  120. case codepage_facet:
  121. return util::create_utf8_codecvt(base,type);
  122. default:
  123. return base;
  124. }
  125. }
  126. private:
  127. std::vector<std::string> paths_;
  128. std::vector<std::string> domains_;
  129. std::string locale_id_;
  130. std::string real_id_;
  131. bool invalid_;
  132. winlocale lc_;
  133. };
  134. localization_backend *create_localization_backend()
  135. {
  136. return new winapi_localization_backend();
  137. }
  138. } // impl win
  139. } // locale
  140. } // boost
  141. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4