Skip to content

Commit 37c1f8c

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

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/lib/fcitx/instance.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,7 @@ 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-
}
188+
const auto locale = getLocale();
198189
assert(locale.has_value());
199190
xkbContext_.reset(xkb_context_new(XKB_CONTEXT_NO_FLAGS));
200191
if (xkbContext_) {
@@ -224,6 +215,20 @@ InstancePrivate::watchEvent(EventType type, EventWatcherPhase phase,
224215
}
225216

226217
#ifdef ENABLE_KEYBOARD
218+
std::optional<std::string> InstancePrivate::getLocale() {
219+
const std::vector<const char *> variableList = {"LC_ALL", "LC_CTYPE",
220+
"LANG"};
221+
222+
for (const char *variable : variableList) {
223+
const auto locale = getEnvironment(variable);
224+
if (locale) {
225+
return locale;
226+
}
227+
}
228+
229+
return "C";
230+
}
231+
227232
xkb_keymap *InstancePrivate::keymap(const std::string &display,
228233
const std::string &layout,
229234
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::optional<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)