Skip to content

Commit a512464

Browse files
committed
fix no i18n for fresh-installed plugin
1 parent 8b9a08e commit a512464

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

src/fcitx.cpp

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#include <unistd.h>
22
#include <algorithm>
33
#include <atomic>
4-
#include <cctype>
54
#include <filesystem>
6-
#include <sstream>
75
#include <thread>
86

97
#include <fcitx-utils/i18n.h>
@@ -49,7 +47,45 @@ Fcitx::~Fcitx() {
4947
teardown();
5048
}
5149

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+
5287
void Fcitx::setup() {
88+
setupI18N();
5389
setupInstance();
5490
frontend_ =
5591
dynamic_cast<fcitx::MacosFrontend *>(addonMgr().addon("macosfrontend"));
@@ -113,19 +149,6 @@ void Fcitx::setupEnv() {
113149

114150
fcitx::registerDomain(FCITX_GETTEXT_DOMAIN,
115151
(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-
}
129152
}
130153

131154
void Fcitx::setupInstance() {

0 commit comments

Comments
 (0)