Skip to content

Commit

Permalink
Merge pull request #40034 from akirashirosawa/akirashirosawa-fix-getO…
Browse files Browse the repository at this point in the history
…SXSystemLang

make System language option work on macOS (fix getOSXSystemLang)
  • Loading branch information
ZhilkinSerg authored Apr 30, 2020
2 parents 9ea340e + cf7d422 commit a67e503
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/translations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,22 @@ std::string getOSXSystemLang()
return "en_US";
}

const char *lang_code_raw = CFStringGetCStringPtr(
reinterpret_cast<CFStringRef>( CFArrayGetValueAtIndex( langs, 0 ) ),
kCFStringEncodingUTF8 );
if( !lang_code_raw ) {
return "en_US";
CFStringRef lang = static_cast<CFStringRef>( CFArrayGetValueAtIndex( langs, 0 ) );
const char *lang_code_raw_fast = CFStringGetCStringPtr( lang, kCFStringEncodingUTF8 );
std::string lang_code;
if( lang_code_raw_fast ) { // fast way, probably it's never works
lang_code = lang_code_raw_fast;
} else { // fallback to slow way
CFIndex length = CFStringGetLength( lang ) + 1;
std::vector<char> lang_code_raw_slow( length, '\0' );
bool success = CFStringGetCString( lang, lang_code_raw_slow.data(), length, kCFStringEncodingUTF8 );
if( !success ) {
return "en_US";
}
lang_code = lang_code_raw_slow.data();
}

// Convert to the underscore format expected by gettext
std::string lang_code( lang_code_raw );
std::replace( lang_code.begin(), lang_code.end(), '-', '_' );

/**
Expand Down

0 comments on commit a67e503

Please sign in to comment.