Skip to content

Commit 29f5647

Browse files
committed
Reset locale after suggest
Fixes #85 The reason we do this is because upper-/lowercasing requires a UTF-8 locale. However, some locales, e.g. nn_NO.UTF-8, also change things like number formatting, so when changing the global process locale, other programs called by divvun-checker will *next time around* start using that locale. This change simply reverts to the original locale after one round of suggest. A better change might to use facets so the locale change doesn't affect numbers.
1 parent 0d4ff7d commit 29f5647

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/suggest.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,16 +1097,19 @@ void Suggest::mk_errs(Sentence& sentence) {
10971097
}
10981098

10991099
vector<Err> Suggest::run_errs(std::istream& is) {
1100+
std::locale old_locale = std::locale();
11001101
try {
1101-
auto _old = std::locale::global(std::locale(""));
1102+
old_locale = std::locale::global(std::locale("")); // Would prefer C.UTF-8, but that doesn't always exist
11021103
}
11031104
catch (const std::runtime_error& e) {
11041105
std::cerr
11051106
<< "divvun-suggest: WARNING: Couldn't set global locale \"\" "
11061107
"(locale-specific native environment): "
11071108
<< e.what() << std::endl;
11081109
}
1109-
return run_sentence(is, FlushOn::Nul).errs;
1110+
auto result = run_sentence(is, FlushOn::Nul).errs;
1111+
std::locale::global(old_locale);
1112+
return result;
11101113
}
11111114

11121115

@@ -1221,8 +1224,9 @@ RunState Suggest::run_cg(std::istream& is, std::ostream& os) {
12211224
}
12221225

12231226
void Suggest::run(std::istream& is, std::ostream& os, RunMode mode) {
1227+
std::locale old_locale = std::locale();
12241228
try {
1225-
auto _old = std::locale::global(std::locale(""));
1229+
old_locale = std::locale::global(std::locale("")); // Would prefer C.UTF-8, but that doesn't always exist
12261230
}
12271231
catch (const std::runtime_error& e) {
12281232
std::cerr
@@ -1244,6 +1248,7 @@ void Suggest::run(std::istream& is, std::ostream& os, RunMode mode) {
12441248
;
12451249
break;
12461250
}
1251+
std::locale::global(old_locale);
12471252
}
12481253

12491254
SortedMsgLangs sortMessageLangs(const MsgMap& msgs, const string& prefer) {

0 commit comments

Comments
 (0)