File indexing completed on 2024-04-21 15:38:42

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2008 by Bjoern Erik Nilsen & Fredrik Berg Kjoelstad*
0003  *   bjoern.nilsen@bjoernen.com & fredrikbk@hotmail.com                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "languagehandler.h"
0021 
0022 #include "logger.h"
0023 #include "src/foundation/preferencestool.h"
0024 #include "src/config.h"
0025 
0026 #include <assert.h>
0027 #include <QDebug>
0028 #include <QDir>
0029 #include <QTranslator>
0030 #include <QLocale>
0031 #include <QLatin1String>
0032 
0033 
0034 LanguageHandler::LanguageHandler(QObject *parent, QApplication *stApp, const char *name) 
0035         : QObject(parent), languagesMenu(0) {
0036     qmPath = QString(stopmotion::translationsDirectory);
0037     activeAction = 0;
0038     translator = new QTranslator(this);
0039 
0040     // Get system locale.
0041     QString locale = QLocale::system().name().toLower();
0042     if (locale == QLatin1String("nb_no"))
0043         locale = QLatin1String("no_nb");
0044     else if (locale == QLatin1String("nn_no"))
0045         locale = QLatin1String("no_nn");
0046     else if (locale == QLatin1String("se_no"))
0047         locale = QLatin1String("no_se");
0048     else
0049         locale.truncate(2);
0050 
0051     // Put together a translation file based on the qmPath or keep
0052     // it empty if the locale is english.
0053     const bool englishLocale = (locale == QLatin1String("en"));
0054     const QString prefix = qmPath + QLatin1Char('/') + QLatin1String("stopmotion_");
0055     QString translationFile = englishLocale ? QString() : prefix + locale;
0056 
0057     if (!englishLocale && !QFile::exists(translationFile + QLatin1String(".qm"))) {
0058         // Was not able to find a translation file for the locale, so use the
0059         // language saved in the preferences file, or use English as fall-back.
0060         const QByteArray localeArray = locale.toLatin1();
0061         const char *localePtr = localeArray.constData();
0062         Preference languagePref("language", localePtr);
0063         if (languagePref.get()) {
0064             translationFile = prefix + QLatin1String(languagePref.get());
0065             if (!QFile::exists(translationFile + QLatin1String(".qm")))
0066                 translationFile = QString();
0067         } else {
0068             translationFile = QString();
0069         }
0070     } else {
0071         PreferencesTool::get()->setPreference("language", locale.toLatin1().constData());
0072     }
0073 
0074     if (!translationFile.isEmpty()) {
0075         Logger::get().logDebug("Loading translator: ");
0076         Logger::get().logDebug(translationFile.toLatin1().constData());
0077         translator->load(translationFile);
0078     }
0079 
0080     stApp->installTranslator(translator);
0081     setObjectName(name);
0082 }
0083 
0084 
0085 QMenu* LanguageHandler::createLanguagesMenu(QMenu *parent)
0086 {
0087     assert(parent);
0088     // For the .po files. findtr isn't as intelligent as luptate
0089     tr("English");
0090 
0091     languagesMenu = parent->addMenu(tr("&Translation"));
0092     connect(languagesMenu, SIGNAL(triggered(QAction *)), this, SLOT(changeLanguage(QAction *)));
0093 
0094     QDir dir(qmPath);
0095     QStringList fileNames = dir.entryList(QStringList("stopmotion_*.qm"));
0096 
0097     //English is a special case (base language)
0098     QAction *langAct = languagesMenu->addAction("&1 English");
0099     langAct->setCheckable(true);
0100     locales.insert(langAct, "en");
0101 
0102     int num = 2;
0103     for (int i = 0; i < fileNames.size(); ++i) {
0104         QTranslator translator;
0105         translator.load(fileNames[i], qmPath);
0106 
0107         QString language = translator.translate("LanguageHandler", "English", 
0108             "This should be translated to the name of the "
0109             "language you are translating to, in that language. "
0110             "Example: English = Deutsch (Deutsch is \"German\" "
0111             "in German)");
0112 
0113         // Checks that the minimum requirement for accepting a string is covered.
0114         // The minimum requirement is that the menu option string (English) is translated.
0115         if (language != "") {
0116             langAct = languagesMenu->addAction(QString("&%1 %2").arg(num++).arg(language));
0117             langAct->setCheckable(true);
0118             langAct->setChecked(false);
0119 
0120             QString locale = fileNames[i];
0121             locale = locale.mid(locale.indexOf('_') + 1);
0122             locale.truncate(locale.indexOf('.'));
0123             locales.insert(langAct, locale);
0124         }
0125     }
0126 
0127     Preference languagePref("language", "en");
0128     activeAction = locales.key(QString(languagePref.get()));
0129     if (activeAction != 0) {
0130         activeAction->setChecked(true);
0131     } else {
0132         Logger::get().logWarning("Something wrong with the locale!");
0133     }
0134     return languagesMenu;
0135 }
0136 
0137 
0138 void LanguageHandler::changeLanguage(QAction *action)
0139 {
0140     if (activeAction != 0) {
0141         activeAction->setChecked(false);
0142     }
0143     action->setChecked(true);
0144     activeAction = action;
0145     
0146     QString locale = locales[action];
0147     if (locale != "en") {
0148         translator->load("stopmotion_" + locale, qmPath);
0149     }
0150     else {
0151         translator->load("");
0152     }
0153     
0154     PreferencesTool::get()->setPreference("language", locale.toLatin1().constData());
0155     emit languageChanged();
0156 }
0157