|
1 | 1 | #include <unistd.h>
|
2 | 2 | #include <algorithm>
|
3 | 3 | #include <atomic>
|
4 |
| -#include <cctype> |
5 | 4 | #include <filesystem>
|
6 |
| -#include <sstream> |
7 | 5 | #include <thread>
|
8 | 6 |
|
9 | 7 | #include <fcitx-utils/i18n.h>
|
@@ -49,7 +47,45 @@ Fcitx::~Fcitx() {
|
49 | 47 | teardown();
|
50 | 48 | }
|
51 | 49 |
|
| 50 | +// Collect all ~/Library/fcitx5/share/locale/*/LC_MESSAGES/*.mo |
| 51 | +std::set<std::string> getPluginDomains(const fs::path &localedir) { |
| 52 | + std::set<std::string> ret; |
| 53 | + try { |
| 54 | + for (const auto &entry : fs::directory_iterator(localedir)) { |
| 55 | + if (!entry.is_directory()) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + fs::path lcMessagesPath = entry.path() / "LC_MESSAGES"; |
| 59 | + try { |
| 60 | + for (const auto &file : |
| 61 | + fs::directory_iterator(lcMessagesPath)) { |
| 62 | + if (file.path().extension() == ".mo") { |
| 63 | + ret.insert(file.path().stem()); |
| 64 | + } |
| 65 | + } |
| 66 | + } catch (const std::exception &e) { |
| 67 | + // LC_MESSAGES not exist. |
| 68 | + } |
| 69 | + } |
| 70 | + } catch (const std::exception &e) { |
| 71 | + // localedir not exist. |
| 72 | + } |
| 73 | + return ret; |
| 74 | +} |
| 75 | + |
| 76 | +// Must be executed before creating fcitx instance, i.e. loading addons, because |
| 77 | +// addons register compile-time domain path, and only 1st call of registerDomain |
| 78 | +// counts. The .mo files must exist. |
| 79 | +void setupI18N() { |
| 80 | + fs::path home{getenv("HOME")}; |
| 81 | + fs::path localedir = home / "Library" / "fcitx5" / "share" / "locale"; |
| 82 | + for (const auto &domain : getPluginDomains(localedir)) { |
| 83 | + fcitx::registerDomain(domain.c_str(), localedir.c_str()); |
| 84 | + } |
| 85 | +} |
| 86 | + |
52 | 87 | void Fcitx::setup() {
|
| 88 | + setupI18N(); |
53 | 89 | setupInstance();
|
54 | 90 | frontend_ =
|
55 | 91 | dynamic_cast<fcitx::MacosFrontend *>(addonMgr().addon("macosfrontend"));
|
@@ -113,19 +149,6 @@ void Fcitx::setupEnv() {
|
113 | 149 |
|
114 | 150 | fcitx::registerDomain(FCITX_GETTEXT_DOMAIN,
|
115 | 151 | (app_contents_path / "share" / "locale").c_str());
|
116 |
| - |
117 |
| - // Register text domains of well-known addons. |
118 |
| - fs::path localedir = user_prefix / "share" / "locale"; |
119 |
| - |
120 |
| - const char *addon_names[] = {"fcitx5-anthy", "fcitx5-chinese-addons", |
121 |
| - "fcitx5-hallelujah", "fcitx5-libthai", |
122 |
| - "fcitx5-lua", "fcitx5-rime", |
123 |
| - "fcitx5-skk", "fcitx5-unikey", |
124 |
| - "fcitx5-chewing", "fcitx5-hangul", |
125 |
| - "fcitx5-sayura", "fcitx5-bamboo"}; |
126 |
| - for (const auto addon : addon_names) { |
127 |
| - fcitx::registerDomain(addon, localedir.c_str()); |
128 |
| - } |
129 | 152 | }
|
130 | 153 |
|
131 | 154 | void Fcitx::setupInstance() {
|
|
0 commit comments