Skip to content

Commit e3be2ff

Browse files
committed
Add a new function to get locale
1 parent eb502fc commit e3be2ff

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/lib/fcitx/instance.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,13 @@ void InstanceArgument::printUsage() const {
185185

186186
InstancePrivate::InstancePrivate(Instance *q) : QPtrHolder<Instance>(q) {
187187
#ifdef ENABLE_KEYBOARD
188-
auto locale = getEnvironment("LC_ALL");
189-
if (!locale) {
190-
locale = getEnvironment("LC_CTYPE");
191-
}
192-
if (!locale) {
193-
locale = getEnvironment("LANG");
194-
}
195-
if (!locale) {
196-
locale = "C";
197-
}
198-
assert(locale.has_value());
188+
const auto locale = getLocale();
189+
assert(!locale.empty());
199190
xkbContext_.reset(xkb_context_new(XKB_CONTEXT_NO_FLAGS));
200191
if (xkbContext_) {
201192
xkb_context_set_log_level(xkbContext_.get(), XKB_LOG_LEVEL_CRITICAL);
202193
xkbComposeTable_.reset(xkb_compose_table_new_from_locale(
203-
xkbContext_.get(), locale->data(), XKB_COMPOSE_COMPILE_NO_FLAGS));
194+
xkbContext_.get(), locale.data(), XKB_COMPOSE_COMPILE_NO_FLAGS));
204195
if (!xkbComposeTable_) {
205196
FCITX_INFO()
206197
<< "Trying to fallback to compose table for en_US.UTF-8";
@@ -224,6 +215,18 @@ InstancePrivate::watchEvent(EventType type, EventWatcherPhase phase,
224215
}
225216

226217
#ifdef ENABLE_KEYBOARD
218+
std::string InstancePrivate::getLocale() {
219+
const char *const variableList[] = {"LC_ALL", "LC_CTYPE", "LANG"};
220+
for (const char *variable : variableList) {
221+
const auto locale = getEnvironment(variable);
222+
if (locale.has_value()) {
223+
return locale.value();
224+
}
225+
}
226+
227+
return "C";
228+
}
229+
227230
xkb_keymap *InstancePrivate::keymap(const std::string &display,
228231
const std::string &layout,
229232
const std::string &variant) {

src/lib/fcitx/instance_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ class InstancePrivate : public QPtrHolder<Instance> {
133133
watchEvent(EventType type, EventWatcherPhase phase, EventHandler callback);
134134

135135
#ifdef ENABLE_KEYBOARD
136+
static std::string getLocale();
137+
136138
xkb_keymap *keymap(const std::string &display, const std::string &layout,
137139
const std::string &variant);
138140
#endif

0 commit comments

Comments
 (0)