Added missing linebreak.
[MUtilities.git] / src / Translation.cpp
blobe2cf73aef16ecd02934fbe3e8fe0d95d1ac38822
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
22 //MUtils
23 #include <MUtils/Translation.h>
25 //Qt
26 #include <QPair>
27 #include <QReadWriteLock>
28 #include <QMap>
29 #include <QStringList>
30 #include <QTranslator>
31 #include <QFileInfo>
32 #include <QApplication>
34 //////////////////////////////////////////////////////////////////////////////////
35 // TYPES
36 //////////////////////////////////////////////////////////////////////////////////
38 typedef QPair<QString,QString> translation_info_t;
39 typedef QPair<quint32,quint32> translation_data_t;
41 typedef QPair<translation_info_t, translation_data_t> translation_entry_t;
42 typedef QMap<QString, translation_entry_t> translation_store_t;
44 #define MAKE_ENTRY(NAME,PATH,SYSID,CNTRY) \
45 qMakePair(qMakePair((NAME),(PATH)),qMakePair((SYSID),(CNTRY)))
47 //////////////////////////////////////////////////////////////////////////////////
48 // TRANSLATIONS STORE
49 //////////////////////////////////////////////////////////////////////////////////
51 static QReadWriteLock g_translation_lock;
52 static QScopedPointer<translation_store_t> g_translation_data;
53 static QScopedPointer<QTranslator> g_translation_inst;
55 //////////////////////////////////////////////////////////////////////////////////
56 // CONSTANT
57 //////////////////////////////////////////////////////////////////////////////////
59 namespace MUtils
61 namespace Translation
63 const char *const DEFAULT_LANGID = "en";
67 //////////////////////////////////////////////////////////////////////////////////
68 // REGISTER TRANSLATION
69 //////////////////////////////////////////////////////////////////////////////////
71 bool MUtils::Translation::insert(const QString &langId, const QString &qmFile, const QString &langName, const quint32 &systemId, const quint32 &country)
73 QWriteLocker writeLockTranslations(&g_translation_lock);
75 const QString key = langId.simplified().toLower();
76 if(key.isEmpty() || qmFile.isEmpty() || langName.isEmpty() || (systemId < 1))
78 return false;
81 if(g_translation_data.isNull())
83 g_translation_data.reset(new translation_store_t());
86 if(g_translation_data->contains(key))
88 qWarning("Translation store already contains entry for '%s', going to replace!", MUTILS_UTF8(key));
91 g_translation_data->insert(key, MAKE_ENTRY(langName, qmFile, systemId, country));
92 return true;
95 //////////////////////////////////////////////////////////////////////////////////
96 // GET TRANSLATION INFO
97 //////////////////////////////////////////////////////////////////////////////////
99 int MUtils::Translation::enumerate(QStringList &list)
101 QReadLocker readLockTranslations(&g_translation_lock);
103 if(g_translation_data.isNull())
105 list.clear();
106 return -1;
109 list.swap(g_translation_data->keys());
110 return list.count();
113 QString MUtils::Translation::get_name(const QString &langId)
115 QReadLocker readLockTranslations(&g_translation_lock);
117 const QString key = langId.simplified().toLower();
118 if(key.isEmpty() || g_translation_data.isNull() || (!g_translation_data->contains(key)))
120 return QString();
123 return (*g_translation_data)[key].first.first;
126 quint32 MUtils::Translation::get_sysid(const QString &langId)
128 QReadLocker readLockTranslations(&g_translation_lock);
130 const QString key = langId.simplified().toLower();
131 if(key.isEmpty() || g_translation_data.isNull() || (!g_translation_data->contains(key)))
133 return 0;
136 return (*g_translation_data)[key].second.first;
139 quint32 MUtils::Translation::get_country(const QString &langId)
141 QReadLocker readLockTranslations(&g_translation_lock);
142 const QString key = langId.simplified().toLower();
143 if(key.isEmpty() || g_translation_data.isNull() || (!g_translation_data->contains(key)))
145 return 0;
148 return (*g_translation_data)[key].second.second;
151 //////////////////////////////////////////////////////////////////////////////////
152 // INSTALL TRANSLATION
153 //////////////////////////////////////////////////////////////////////////////////
155 bool MUtils::Translation::install_translator(const QString &langId)
157 QReadLocker readLockTranslations(&g_translation_lock);
159 const QString key = langId.simplified().toLower();
160 if(key.isEmpty() || g_translation_data.isNull() || (!g_translation_data->contains(key)))
162 return false;
165 const QString qmFile = (*g_translation_data)[key].first.second;
166 readLockTranslations.unlock();
167 return install_translator_from_file(qmFile);
170 bool MUtils::Translation::install_translator_from_file(const QString &qmFile)
172 QWriteLocker writeLock(&g_translation_lock);
174 if(g_translation_inst.isNull())
176 g_translation_inst.reset(new QTranslator());
179 if(qmFile.isEmpty())
181 QApplication::removeTranslator(g_translation_inst.data());
182 return true;
185 const QFileInfo qmFileInfo(qmFile);
186 if(!(qmFileInfo.exists() && qmFileInfo.isFile()))
188 qWarning("Translation file not found:\n\"%s\"", MUTILS_UTF8(qmFile));
189 return false;
192 const QString qmPath = QFileInfo(qmFile).canonicalFilePath();
193 if(!qmPath.isEmpty())
195 QApplication::removeTranslator(g_translation_inst.data());
196 if(g_translation_inst->load(qmPath))
198 QApplication::installTranslator(g_translation_inst.data());
199 return true;
203 qWarning("Failed to load translation:\n\"%s\"", MUTILS_UTF8(qmFile));
204 return false;