BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
localization.cpp
1 /*
2  Copyright (c) 2012, BuildmLearn Contributors listed at http://buildmlearn.org/people/
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 
11  * Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  * Neither the name of the BuildmLearn nor the names of its
16  contributors may be used to endorse or promote products derived from
17  this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #include "miscellaneous/localization.h"
32 
33 #include "definitions/definitions.h"
34 #include "miscellaneous/settings.h"
35 #include "miscellaneous/application.h"
36 
37 #include <QPointer>
38 #include <QTranslator>
39 #include <QDir>
40 #include <QFileInfoList>
41 #include <QLocale>
42 
43 
44 QPointer<Localization> Localization::s_instance;
45 
46 Localization::Localization(QObject *parent)
47  : QObject(parent) {
48 }
49 
50 Localization::~Localization() {
51  qDebug("Destroying Localization instance.");
52 }
53 
55  if (s_instance.isNull()) {
56  s_instance = new Localization(qApp);
57  }
58 
59  return s_instance;
60 }
61 
63  return qApp->settings()->value(APP_CFG_GEN,
64  "language",
65  QLocale::system().name()).toString();
66 }
67 
69  QTranslator *qt_translator = new QTranslator(qApp);
70  QTranslator *app_translator = new QTranslator(qApp);
71  QString desired_localization = desiredLanguage();
72 
73  if (app_translator->load(QString("buildmlearn-toolkit-%1.qm").arg(desired_localization),
74  APP_LANG_PATH,
75  "-")) {
76  Application::installTranslator(app_translator);
77  qDebug("Application localization '%s' loaded successfully.",
78  qPrintable(desired_localization));
79  }
80  else {
81  qWarning("Application localization '%s' was not loaded.",
82  qPrintable(desired_localization));
83 
84  desired_localization = DEFAULT_LOCALE;
85  }
86 
87  if (qt_translator->load(QString("qt-%1.qm").arg(desired_localization),
88  APP_LANG_PATH,
89  "-")) {
90  Application::installTranslator(qt_translator);
91  qDebug("Qt localization '%s' loaded successfully.",
92  qPrintable(desired_localization));
93  }
94  else {
95  qWarning("Qt localization '%s' was not loaded.",
96  qPrintable(desired_localization));
97  }
98 
99  m_loadedLanguage = desired_localization;
100  QLocale::setDefault(QLocale(desired_localization));
101 }
102 
104  QList<Language> languages;
105  QDir file_dir(APP_LANG_PATH);
106  QTranslator translator;
107 
108  // Iterate all found language files.
109  foreach (const QFileInfo &file, file_dir.entryInfoList(QStringList() << "buildmlearn-toolkit-*.qm",
110  QDir::Files,
111  QDir::Name)) {
112  if (translator.load(file.absoluteFilePath())) {
113  Language new_language;
114  new_language.m_name = translator.translate("QObject", "LANG_NAME");
115  new_language.m_code = translator.translate("QObject", "LANG_ABBREV");
116  new_language.m_author = translator.translate("QObject", "LANG_AUTHOR");
117  new_language.m_email = translator.translate("QObject", "LANG_EMAIL");
118 
119  languages << new_language;
120  }
121  }
122  return languages;
123 }
124 
static Localization * instance()
Singleton getter.
Localization facilities.
Definition: localization.h:48
QList< Language > installedLanguages()
Gets list of installed languages.
QString desiredLanguage()
Access to code of language that should be loaded according to settings.
Representation of single localization.
Definition: localization.h:40
void load()
Loads currently active language.