From 1a0134a9de7d168288a8dbc0d1f157794314d90a Mon Sep 17 00:00:00 2001
From: con <qtc-committer@nokia.com>
Date: Fri, 11 Sep 2009 14:37:17 +0200
Subject: [PATCH] Use systems preferred language on Mac.

And ensure that the menu items don't vanish from the application menu
when they are translated.
---
 src/app/app.pro                       |  2 +-
 src/app/main.cpp                      | 38 ++++++++++++++++++++++++++-
 src/plugins/coreplugin/mainwindow.cpp |  9 +++++--
 3 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/src/app/app.pro b/src/app/app.pro
index 69f5aa6fdce..b1830edff2f 100644
--- a/src/app/app.pro
+++ b/src/app/app.pro
@@ -17,7 +17,7 @@ win32 {
 } else:macx {
     CONFIG(debug, debug|release):LIBS *= -lExtensionSystem_debug -lAggregation_debug
     else:LIBS *= -lExtensionSystem -lAggregation
-
+    LIBS += -framework CoreFoundation
     ICON = qtcreator.icns
     QMAKE_INFO_PLIST = Info.plist
     FILETYPES.files = profile.icns prifile.icns
diff --git a/src/app/main.cpp b/src/app/main.cpp
index 599459eb05c..f6a0dc45b3c 100644
--- a/src/app/main.cpp
+++ b/src/app/main.cpp
@@ -48,6 +48,20 @@
 
 #ifdef Q_OS_MAC
 #  include <sys/resource.h>
+#  include <CoreFoundation/CoreFoundation.h>
+
+// Helper function CoreFoundation -> Qt
+static QString stringFromCFString(CFStringRef value) {
+    QString retVal;
+    CFIndex maxLength = 2 * CFStringGetLength(value) + 1/*zero term*/; // max UTF8
+    char *cstring = new char[maxLength];
+    if (CFStringGetCString(CFStringRef(value), cstring, maxLength, kCFStringEncodingUTF8)) {
+         retVal = QString::fromUtf8(cstring);
+    }
+    delete cstring;
+    return retVal;
+}
+
 #endif
 
 enum { OptionIndent = 4, DescriptionIndent = 24 };
@@ -216,7 +230,29 @@ int main(int argc, char **argv)
 
     QTranslator translator;
     QTranslator qtTranslator;
-    const QString &locale = QLocale::system().name();
+    QString locale = QLocale::system().name();
+#ifdef Q_OS_MAC
+    // because QLocale's system locale is basically useless on the Mac.
+    // Try to get the real system setting via core foundation
+    CFLocaleRef maclocale = CFLocaleCopyCurrent();
+    CFTypeRef value = CFLocaleGetValue(maclocale, kCFLocaleLanguageCode);
+    QString preferredLanguage = stringFromCFString(CFStringRef(value));
+    if (!preferredLanguage.isEmpty())
+        locale = preferredLanguage;
+    CFRelease(maclocale);
+    CFArrayRef languages = (CFArrayRef)CFPreferencesCopyValue(
+             CFSTR("AppleLanguages"),
+             kCFPreferencesAnyApplication,
+             kCFPreferencesCurrentUser,
+             kCFPreferencesAnyHost);
+//    CFShow(languages);
+    if (CFArrayGetCount(languages) > 0) {
+        QString preferredLanguage = stringFromCFString(CFStringRef(CFArrayGetValueAtIndex(languages, 0)));
+        if (!preferredLanguage.isEmpty())
+            locale = preferredLanguage;
+    }
+    CFRelease(languages);
+#endif
     const QString &creatorTrPath = QCoreApplication::applicationDirPath()
                         + QLatin1String(SHARE_PATH "/translations");
     if (translator.load(QLatin1String("qtcreator_") + locale, creatorTrPath)) {
diff --git a/src/plugins/coreplugin/mainwindow.cpp b/src/plugins/coreplugin/mainwindow.cpp
index e42d5c03423..af8576b8ac4 100644
--- a/src/plugins/coreplugin/mainwindow.cpp
+++ b/src/plugins/coreplugin/mainwindow.cpp
@@ -160,7 +160,7 @@ MainWindow::MainWindow() :
     QCoreApplication::setOrganizationName(QLatin1String("Nokia"));
     QSettings::setDefaultFormat(QSettings::IniFormat);
     QString baseName = qApp->style()->objectName();
-#ifdef Q_WS_X11    
+#ifdef Q_WS_X11
     if (baseName == QLatin1String("windows")) {
         // Sometimes we get the standard windows 95 style as a fallback
         // e.g. if we are running on a KDE4 desktop
@@ -325,7 +325,7 @@ bool MainWindow::init(QString *errorMessage)
 
 void MainWindow::modeChanged(Core::IMode *mode)
 {
-    if (mode == m_outputMode) {        
+    if (mode == m_outputMode) {
         int idx = OutputPaneManager::instance()->m_widgetComboBox->itemData(OutputPaneManager::instance()->m_widgetComboBox->currentIndex()).toInt();
         IOutputPane *out = OutputPaneManager::instance()->m_pageMap.value(idx);
         if (out && out->canFocus())
@@ -678,6 +678,7 @@ void MainWindow::registerDefaultActions()
     cmd = am->registerAction(m_optionsAction, Constants::OPTIONS, m_globalContext);
 #ifdef Q_WS_MAC
     cmd->setDefaultKeySequence(QKeySequence("Ctrl+,"));
+    cmd->action()->setMenuRole(QAction::PreferencesRole);
 #endif
     mtools->addAction(cmd, Constants::G_DEFAULT_THREE);
     connect(m_optionsAction, SIGNAL(triggered()), this, SLOT(showOptionsDialog()));
@@ -735,7 +736,11 @@ void MainWindow::registerDefaultActions()
     cmd = am->registerAction(tmpaction, Constants::ABOUT_QTCREATOR, m_globalContext);
     mhelp->addAction(cmd, Constants::G_HELP_ABOUT);
     tmpaction->setEnabled(true);
+#ifdef Q_WS_MAC
+    cmd->action()->setMenuRole(QAction::ApplicationSpecificRole);
+#endif
     connect(tmpaction, SIGNAL(triggered()), this,  SLOT(aboutQtCreator()));
+
     //About Plugins Action
     tmpaction = new QAction(tr("About &Plugins..."), this);
     cmd = am->registerAction(tmpaction, Constants::ABOUT_PLUGINS, m_globalContext);
-- 
GitLab