Fcitx
i18nstring.h
1 /*
2  * SPDX-FileCopyrightText: 2015-2015 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #ifndef _FCITX_UTILS_I18NSTRING_H_
8 #define _FCITX_UTILS_I18NSTRING_H_
9 
10 #include <string>
11 #include <unordered_map>
12 #include <fcitx-utils/fcitxutils_export.h>
13 
14 namespace fcitx {
15 class FCITXUTILS_EXPORT I18NString {
16 public:
17  I18NString() = default;
18  virtual ~I18NString() = default;
19 
20  void set(const std::string &str, const std::string &locale = "") {
21  if (!locale.empty()) {
22  map_[locale] = str;
23  } else {
24  default_ = str;
25  }
26  }
27 
28  void clear() {
29  default_.clear();
30  map_.clear();
31  }
32 
33  const std::string &match(const std::string &locale = "system") const;
34 
35  bool operator==(const I18NString &other) const {
36  return other.default_ == default_ && other.map_ == map_;
37  }
38 
39  bool operator!=(const I18NString &other) const {
40  return !operator==(other);
41  }
42 
43  const std::string &defaultString() const { return default_; }
44  const std::unordered_map<std::string, std::string> &
45  localizedStrings() const {
46  return map_;
47  }
48 
49 protected:
50  std::string default_;
51  std::unordered_map<std::string, std::string> map_;
52 };
53 } // namespace fcitx
54 
55 #endif // _FCITX_UTILS_I18NSTRING_H_
Definition: action.cpp:17