From 494d88f53ff1ac13c1deec927721f5858efa5285 Mon Sep 17 00:00:00 2001 From: Jaroslav Rohel Date: Thu, 19 Sep 2024 21:46:20 +0200 Subject: [PATCH] setlocale: If locale setting fails, try using C.UTF-8 as fallback --- dnf5/main.cpp | 7 +++++-- dnf5daemon-client/main.cpp | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/dnf5/main.cpp b/dnf5/main.cpp index a709afc39..0f7c6e97c 100644 --- a/dnf5/main.cpp +++ b/dnf5/main.cpp @@ -957,8 +957,11 @@ static void print_new_leaves(Context & context) { } static void set_locale() { - auto * locale = setlocale(LC_ALL, ""); - if (locale) { + if (setlocale(LC_ALL, "")) { + return; + } + if (setlocale(LC_ALL, "C.UTF-8")) { + std::cerr << "Failed to set locale, defaulting to \"C.UTF-8\"" << std::endl; return; } std::cerr << "Failed to set locale, defaulting to \"C\"" << std::endl; diff --git a/dnf5daemon-client/main.cpp b/dnf5daemon-client/main.cpp index 7ac80bae5..bab0243b5 100644 --- a/dnf5daemon-client/main.cpp +++ b/dnf5daemon-client/main.cpp @@ -217,10 +217,23 @@ static void add_commands(Context & context) { } // namespace dnfdaemon::client + +static void set_locale() { + if (setlocale(LC_ALL, "")) { + return; + } + if (setlocale(LC_ALL, "C.UTF-8")) { + std::cerr << "Failed to set locale, defaulting to \"C.UTF-8\"" << std::endl; + return; + } + std::cerr << "Failed to set locale, defaulting to \"C\"" << std::endl; +} + + int main(int argc, char * argv[]) { std::unique_ptr connection; - setlocale(LC_ALL, ""); + set_locale(); dnfdaemon::client::Context context;