1 /*
2   This file is part of the Grantlee template system.
3 
4   Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
5 
6   This library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Lesser General Public
8   License as published by the Free Software Foundation; either version
9   2.1 of the Licence, or (at your option) any later version.
10 
11   This library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Lesser General Public License for more details.
15 
16   You should have received a copy of the GNU Lesser General Public
17   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #include "mainwindow.h"
22 
23 #include <QApplication>
24 #include <QLibraryInfo>
25 #include <QTranslator>
26 
27 #include <grantlee_templates.h>
28 
29 #include "grantlee_paths.h"
30 
31 #define TEMPLATE_DIR GRANTLEE_TEMPLATE_PATH "/linguist/"
32 
getLocalizer()33 static QSharedPointer<Grantlee::AbstractLocalizer> getLocalizer()
34 {
35   QSharedPointer<Grantlee::QtLocalizer> localizer
36       = QSharedPointer<Grantlee::QtLocalizer>(new Grantlee::QtLocalizer);
37   localizer->setAppTranslatorPrefix("contacts_");
38   localizer->setAppTranslatorPath(qApp->applicationDirPath());
39 
40   QStringList locales = QStringList() << "en_US"
41                                       << "en_GB"
42                                       << "de_DE"
43                                       << "fr_FR";
44   if (!locales.contains(QLocale::system().name()))
45     locales.append(QLocale::system().name());
46   Q_FOREACH (const QString &localeName, locales) {
47     QTranslator *qtTranslator = new QTranslator;
48     qtTranslator->load("qt_" + localeName,
49                        QLibraryInfo::location(QLibraryInfo::TranslationsPath));
50     qtTranslator->setObjectName("qt_" + localeName);
51     localizer->installTranslator(qtTranslator, localeName);
52 
53     QTranslator *myappTranslator = new QTranslator;
54     myappTranslator->load("contacts_" + localeName + ".qm",
55                           qApp->applicationDirPath());
56     myappTranslator->setObjectName("contacts_" + localeName);
57     localizer->installTranslator(myappTranslator, localeName);
58   }
59 
60   return localizer.staticCast<Grantlee::AbstractLocalizer>();
61 }
62 
initLocalizer()63 template <> void AppMainWindow<Grantlee::QtLocalizer>::initLocalizer()
64 {
65   m_localizer = getLocalizer();
66 }
67 
main(int argc,char * argv[])68 int main(int argc, char *argv[])
69 {
70   QApplication app(argc, argv);
71 
72   AppMainWindow<Grantlee::QtLocalizer> win(TEMPLATE_DIR);
73   win.show();
74 
75   return app.exec();
76 }
77