Engauge Digitizer  2
TranslatorContainer.cpp
Go to the documentation of this file.
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 = static_cast<QLocale::Language> (settings.value (SETTINGS_LOCALE_LANGUAGE,
18  QVariant (localeDefault.language())).toInt());
19  QLocale::Country country = static_cast<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  QString localeName = locale.name().toLower();
34 
35  // Basic translators, like buttons in QWizard
36  m_translatorGeneric = new QTranslator;
37  m_translatorGeneric->load ("qt_" + localeName,
38  QLibraryInfo::location (QLibraryInfo::TranslationsPath));
39  QApplication::installTranslator (m_translatorGeneric);
40 
41  // Engauge-specific translators. As documented in engauge.pro, the country-specific engauge_XX_YY locale is loaded
42  // if available, otherwise engauge_XX is loaded if available
43  //
44  // In OSX, QDir::currentPath points to /Users/?/Library/Containers/Digitizer/Data and
45  // QCoreApplication::applicationDirPath points to ../Engauge Digitizer.app/Contents/MacOS (which we want)
46  QString delimiters ("._");
47  m_translatorEngauge = new QTranslator;
48  // Handle usual translations like es_es
49  bool rtn = m_translatorEngauge->load ("engauge_" + localeName,
50  qmDirectory(),
51  delimiters);
52  if (!rtn) {
53  // Handle country-specific translations like fa_IR. Transifex capitalizes the last two characters
54  QString localeNameUpper = QString ("%1%2")
55  .arg (localeName.left (localeName.length() - 2))
56  .arg (localeName.right (2).toUpper ());
57  m_translatorEngauge->load ("engauge_" + localeNameUpper,
58  qmDirectory(),
59  delimiters);
60  }
61  QApplication::installTranslator (m_translatorEngauge);
62  }
63 }
64 
66 {
67 #if defined(OSX_DEBUG) || defined(OSX_RELEASE)
68  return QCoreApplication::applicationDirPath () + "/../Resources/translations";
69 #else
70  return QCoreApplication::applicationDirPath () + "/translations";
71 #endif
72 }
const QString SETTINGS_DIGITIZER
TranslatorContainer(QApplication &app)
Single constructor. Argument is needed so object is not optimized away in main() in Windows...
const QString SETTINGS_LOCALE_LANGUAGE
const QString SETTINGS_LOCALE_COUNTRY
const QString SETTINGS_ENGAUGE
const QString SETTINGS_GROUP_MAIN_WINDOW
static QString qmDirectory()
Platform dependent directory containing qm translation files.