1//
2// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3// Copyright (c) 2022-2023 Alexander Grund
4//
5// Distributed under the Boost Software License, Version 1.0.
6// https://www.boost.org/LICENSE_1_0.txt
7
8#include "boost/locale/icu/icu_backend.hpp"
9#include <boost/locale/gnu_gettext.hpp>
10#include <boost/locale/localization_backend.hpp>
11#include <boost/locale/util.hpp>
12#include "boost/locale/icu/all_generator.hpp"
13#include "boost/locale/icu/cdata.hpp"
14#include "boost/locale/shared/message.hpp"
15#include "boost/locale/util/make_std_unique.hpp"
16
17#include <unicode/ucnv.h>
18
19namespace boost { namespace locale { namespace impl_icu {
20 class icu_localization_backend : public localization_backend {
21 public:
22 icu_localization_backend() : invalid_(true), use_ansi_encoding_(false) {}
23 icu_localization_backend(const icu_localization_backend& other) :
24 localization_backend(), paths_(other.paths_), domains_(other.domains_), locale_id_(other.locale_id_),
25 invalid_(true), use_ansi_encoding_(other.use_ansi_encoding_)
26 {}
27 icu_localization_backend* clone() const override { return new icu_localization_backend(*this); }
28
29 void set_option(const std::string& name, const std::string& value) override
30 {
31 invalid_ = true;
32 if(name == "locale")
33 locale_id_ = value;
34 else if(name == "message_path")
35 paths_.push_back(x: value);
36 else if(name == "message_application")
37 domains_.push_back(x: value);
38 else if(name == "use_ansi_encoding")
39 use_ansi_encoding_ = value == "true";
40 }
41 void clear_options() override
42 {
43 invalid_ = true;
44 use_ansi_encoding_ = false;
45 locale_id_.clear();
46 paths_.clear();
47 domains_.clear();
48 }
49
50 void prepare_data()
51 {
52 if(!invalid_)
53 return;
54 invalid_ = false;
55
56 if(locale_id_.empty())
57 real_id_ = util::get_system_locale(/*utf8*/ use_utf8_on_windows: !use_ansi_encoding_);
58 else
59 real_id_ = locale_id_;
60
61 data_.set(real_id_);
62 }
63
64 std::locale install(const std::locale& base, category_t category, char_facet_t type) override
65 {
66 prepare_data();
67
68 switch(category) {
69 case category_t::convert: return create_convert(base, data_, type);
70 case category_t::collation: return create_collate(base, data_, type);
71 case category_t::formatting: return create_formatting(base, data_, type);
72 case category_t::parsing: return create_parsing(base, data_, type);
73 case category_t::codepage: return create_codecvt(base, encoding: data_.encoding(), type);
74 case category_t::message:
75 return detail::install_message_facet(in: base, type, data: data_.data(), domains: domains_, paths: paths_);
76 case category_t::boundary: return create_boundary(base, data_, type);
77 case category_t::calendar: return create_calendar(base, data_);
78 case category_t::information: return util::create_info(in: base, name: real_id_);
79 }
80 return base;
81 }
82
83 private:
84 std::vector<std::string> paths_;
85 std::vector<std::string> domains_;
86 std::string locale_id_;
87 std::string real_id_;
88 cdata data_;
89 bool invalid_;
90 bool use_ansi_encoding_;
91 };
92
93 std::unique_ptr<localization_backend> create_localization_backend()
94 {
95 return make_std_unique<icu_localization_backend>();
96 }
97
98}}} // namespace boost::locale::impl_icu
99

source code of boost/libs/locale/src/boost/locale/icu/icu_backend.cpp