Verify that we're actually using a UTF-8 locale and switch to en_US.UTF-8 if not

Should fix #1587 and #1675, but I was never able to reproduce either of
them so maybe not.
This commit is contained in:
Thomas Goyne 2014-05-06 18:16:36 -07:00
parent 432640c045
commit 77a2c1bb16
1 changed files with 24 additions and 9 deletions

View File

@ -68,7 +68,7 @@
#include <boost/format.hpp>
#include <boost/interprocess/streams/bufferstream.hpp>
#include <boost/locale.hpp>
#include <locale>
#include <wx/clipbrd.h>
#include <wx/msgdlg.h>
#include <wx/stackwalk.h>
@ -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<wchar_t, char, std::mbstate_t>;
int result = std::codecvt_base::error;
if (std::has_facet<codecvt>(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<codecvt>(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());