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 <locale>
11 #include <string>
12 #include <unordered_map>
13 #include "fcitxutils_export.h"
14 
15 namespace fcitx {
16 class FCITXUTILS_EXPORT I18NString {
17 public:
I18NString()18     I18NString() {}
~I18NString()19     virtual ~I18NString() {}
20 
21     void set(const std::string &str, const std::string &locale = "") {
22         if (!locale.empty()) {
23             map_[locale] = str;
24         } else {
25             default_ = str;
26         }
27     }
28 
clear()29     void clear() {
30         default_.clear();
31         map_.clear();
32     }
33 
34     const std::string &match(const std::string &locale = "system") const;
35 
36     bool operator==(const I18NString &other) const {
37         return other.default_ == default_ && other.map_ == map_;
38     }
39 
40     bool operator!=(const I18NString &other) const {
41         return !operator==(other);
42     }
43 
defaultString()44     const std::string &defaultString() const { return default_; }
45     const std::unordered_map<std::string, std::string> &
localizedStrings()46     localizedStrings() const {
47         return map_;
48     }
49 
50 protected:
51     std::string default_;
52     std::unordered_map<std::string, std::string> map_;
53 };
54 } // namespace fcitx
55 
56 #endif // _FCITX_UTILS_I18NSTRING_H_
57