diff --git a/src/main.cpp b/src/main.cpp index a885ad7f2..c8d0dbfca 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,7 +68,7 @@ #include #include #include - +#include #include #include #include @@ -119,14 +119,29 @@ bool AegisubApp::OnInit() { // be created now (void)wxLog::GetActiveTarget(); -#ifdef __APPLE__ - // When launched from Finder, boost::locale fails to get the correct locale - // and falls back to "C", which of course doesn't support unicode - std::locale::global(boost::locale::generator().generate("en_US.UTF-8")); -#else - // Set the global locale to the utf-8 version of the current locale - std::locale::global(boost::locale::generator().generate("")); -#endif + { + // Try to get the UTF-8 version of the current locale + auto locale = boost::locale::generator().generate(""); + + // Check if we actually got a UTF-8 locale + using codecvt = std::codecvt; + int result = std::codecvt_base::error; + if (std::has_facet(locale)) { + wchar_t test[] = L"\xFFFE"; + char buff[8]; + auto mb = std::mbstate_t(); + const wchar_t* from_next; + char* to_next; + result = std::use_facet(locale).out(mb, + test, std::end(test), from_next, + buff, std::end(buff), to_next); + } + + // If we didn't get a UTF-8 locale, force it to a known one + if (result != std::codecvt_base::ok) + locale = boost::locale::generator().generate("en_US.UTF-8"); + std::locale::global(locale); + } boost::filesystem::path::imbue(std::locale());