Skip to content

Commit

Permalink
MACOSX: Detect system preferred language when starting bundle
Browse files Browse the repository at this point in the history
This reimplement getSystemLanguage() for MacOS X because
setlocale() only works if the application is started from the terminal.
Instead we use CFBundleCopyPreferredLocalizationsFromArray() which
requires the translations to be listed in the bundle plist file (this had
already been committed). This fixes bug #3394080.
  • Loading branch information
criezy committed Oct 3, 2011
1 parent 6269dcd commit e3d7606
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
47 changes: 47 additions & 0 deletions backends/platform/sdl/macosx/macosx.cpp
Expand Up @@ -118,4 +118,51 @@ bool OSystem_MacOSX::displayLogFile() {
return err != noErr;
}

Common::String OSystem_MacOSX::getSystemLanguage() const {
#if defined(USE_DETECTLANG) && defined(USE_TRANSLATION)
CFArrayRef availableLocalizations = CFBundleCopyBundleLocalizations(CFBundleGetMainBundle());
if (availableLocalizations) {
CFArrayRef preferredLocalizations = CFBundleCopyPreferredLocalizationsFromArray(availableLocalizations);
CFRelease(availableLocalizations);
if (preferredLocalizations) {
CFIndex localizationsSize = CFArrayGetCount(preferredLocalizations);
// Since we have a list of sorted preferred localization, I would like here to
// check that they are supported by the TranslationManager and take the first
// one that is supported. The listed localizations are taken from the Bundle
// plist file, so they should all be supported, unless the plist file is not
// synchronized with the translations.dat file. So this is not really a big
// issue. And because getSystemLanguage() is called from the constructor of
// TranslationManager (therefore before the instance pointer is set), calling
// TransMan here results in an infinite loop and creation of a lot of TransMan
// instances.
/*
for (CFIndex i = 0 ; i < localizationsSize ; ++i) {
CFStringRef language = (CFStringRef)CFArrayGetValueAtIndex(preferredLocalizations, i);
char buffer[10];
CFStringGetCString(language, buffer, 50, kCFStringEncodingASCII);
int32 languageId = TransMan.findMatchingLanguage(buffer);
if (languageId != -1) {
CFRelease(preferredLocalizations);
return TransMan.getLangById(languageId);
}
}
*/
if (localizationsSize > 0) {
CFStringRef language = (CFStringRef)CFArrayGetValueAtIndex(preferredLocalizations, 0);
char buffer[10];
CFStringGetCString(language, buffer, 50, kCFStringEncodingASCII);
CFRelease(preferredLocalizations);
return buffer;
}
CFRelease(preferredLocalizations);
}

}
// Falback to POSIX implementation
return OSystem_POSIX::getSystemLanguage();
#else // USE_DETECTLANG
return OSystem_POSIX::getSystemLanguage();
#endif // USE_DETECTLANG
}

#endif
2 changes: 2 additions & 0 deletions backends/platform/sdl/macosx/macosx.h
Expand Up @@ -32,6 +32,8 @@ class OSystem_MacOSX : public OSystem_POSIX {
virtual bool hasFeature(Feature f);

virtual bool displayLogFile();

virtual Common::String getSystemLanguage() const;

virtual void initBackend();
virtual void addSysArchivesToSearchSet(Common::SearchSet &s, int priority = 0);
Expand Down

0 comments on commit e3d7606

Please sign in to comment.