Skip to content

Commit 2ccc37c

Browse files
authored
Add a new function to get current locale (#1405)
1 parent 3d2c230 commit 2ccc37c

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/lib/fcitx/instance.cpp

Lines changed: 3 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 = getCurrentLocale();
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";

src/lib/fcitx/misc_p.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,24 @@ static inline bool hasTwoKeyboardInCurrentGroup(Instance *instance) {
227227
return false;
228228
}
229229

230-
static inline std::string getCurrentLanguage() {
231-
for (const char *vars : {"LC_ALL", "LC_MESSAGES", "LANG"}) {
232-
auto lang = getEnvironment(vars);
230+
static inline std::string
231+
getEnvVariableValue(const std::vector<std::string> &variableList,
232+
const char *defaultValue) {
233+
for (const auto &vars : variableList) {
234+
auto lang = getEnvironment(vars.c_str());
233235
if (lang && !lang->empty()) {
234236
return std::move(*lang);
235237
}
236238
}
237-
return "";
239+
return defaultValue;
240+
}
241+
242+
static inline std::string getCurrentLanguage() {
243+
return getEnvVariableValue({"LC_ALL", "LC_MESSAGES", "LANG"}, "");
244+
}
245+
246+
static inline std::string getCurrentLocale() {
247+
return getEnvVariableValue({"LC_ALL", "LC_CTYPE", "LANG"}, "C");
238248
}
239249

240250
static inline std::string stripLanguage(const std::string &lc) {

0 commit comments

Comments
 (0)