Engauge Digitizer  2
TranslatorContainer.cpp
1 #include <QApplication>
2 #include <QDir>
3 #include <QLibraryInfo>
4 #include <QSettings>
5 #include <QTranslator>
6 #include "Settings.h"
7 #include "TranslatorContainer.h"
8 
9 TranslatorContainer::TranslatorContainer(QApplication & /* app */)
10 {
11  QLocale localeDefault;
12 
13  QSettings settings (SETTINGS_ENGAUGE, SETTINGS_DIGITIZER);
14  settings.beginGroup(SETTINGS_GROUP_MAIN_WINDOW);
15 
16  // Get the locale settings outside of the settings retrieval methods in MainWindow
17  QLocale::Language language = (QLocale::Language) settings.value (SETTINGS_LOCALE_LANGUAGE,
18  QVariant (localeDefault.language())).toInt();
19  QLocale::Country country = (QLocale::Country) settings.value (SETTINGS_LOCALE_COUNTRY,
20  QVariant (localeDefault.country())).toInt();
21  QLocale locale (language,
22  country);
23 
24  settings.endGroup();
25 
26  // For some reason, some built-in strings get translated into German by the first call to installTranslator,
27  // when the locale is english. So we skip translation for unless user has either
28  // 1) default language is not english
29  // 2) default language and selected language are not the same
30  if ((localeDefault.name().toLower() != "en_us") ||
31  (localeDefault.name().toLower() != locale.name().toLower())) {
32 
33  // Basic translators, like buttons in QWizard
34  m_translatorGeneric = new QTranslator;
35  m_translatorGeneric->load ("qt_" + locale.name().toLower(),
36  QLibraryInfo::location (QLibraryInfo::TranslationsPath));
37  QApplication::installTranslator (m_translatorGeneric);
38 
39  // Engauge-specific translators. As documented in engauge.pro, the country-specific engauge_XX_YY locale is loaded
40  // if available, otherwise engauge_XX is loaded if available
41  //
42  // In OSX, QDir::currentPath points to /Users/?/Library/Containers/Digitizer/Data and
43  // QCoreApplication::applicationDirPath points to ../Engauge Digitizer.app/Contents/MacOS (which we want)
44  QString delimiters ("._");
45  m_translatorEngauge = new QTranslator;
46 #if defined(OSX_DEBUG) || defined(OSX_RELEASE)
47  m_translatorEngauge->load ("engauge_" + locale.name().toLower(),
48  QCoreApplication::applicationDirPath () + "/../Resources/translations",
49  delimiters);
50 #else
51  m_translatorEngauge->load ("engauge_" + locale.name().toLower(),
52  QCoreApplication::applicationDirPath () + "/translations",
53  delimiters);
54 #endif
55  QApplication::installTranslator (m_translatorEngauge);
56  }
57 }
TranslatorContainer(QApplication &app)
Single constructor. Argument is needed so object is not optimized away in main() in Windows...