1 /*******************************************************************
2 
3 Part of the Fritzing project - http://fritzing.org
4 Copyright (c) 2007-2015 Fachhochschule Potsdam - http://fh-potsdam.de
5 
6 Fritzing is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 Fritzing 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
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with Fritzing.  If not, see <http://www.gnu.org/licenses/>.
18 
19 ********************************************************************
20 
21 $Revision: 6904 $:
22 $Author: irascibl@gmail.com $:
23 $Date: 2013-02-26 16:26:03 +0100 (Di, 26. Feb 2013) $
24 
25 ********************************************************************/
26 
27 #include "translatorlistmodel.h"
28 #include "../debugdialog.h"
29 
30 static QVariant emptyVariant;
31 QHash<QString, QString> TranslatorListModel::m_languages;
32 QList<QLocale *> TranslatorListModel::m_localeList;
33 
34 // More languages written in their own language can be found
35 // at http://www.mozilla.com/en-US/firefox/all.html
36 
37 // recipe for translating from mozilla strings into source code via windows:
38 //		1. copy the string from the mozilla page into wordpad and save it as a unicode text file
39 //		2. open that unicode text file as a binary file (e.g. in msvc)
40 //		3. ignore the first two bytes (these are a signal that says "I'm unicode")
41 //		4. initialize an array of ushort using the rest of the byte pairs
42 //		5. don't forget to reverse the order of the bytes in each pair.
43 
44 // to add a new language to fritzing, find its two-letter language code: http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
45 // then in the translations.pri file, add a new line translations/fritzing_XX.ts where you substitute the two letters for "XX".
46 // If the language has multiple dialects, then you will need to add _YY to distinguish
47 
48 
TranslatorListModel(QFileInfoList & fileInfoList,QObject * parent)49 TranslatorListModel::TranslatorListModel(QFileInfoList & fileInfoList, QObject* parent)
50 : QAbstractListModel(parent)
51 {
52 
53     if (m_languages.count() == 0) {
54         m_languages.insert("english", tr("English - %1").arg("English"));
55         m_languages.insert("german", tr("German - %1").arg("Deutsch"));
56         m_languages.insert("hungarian", tr("Hungarian - %1").arg("Magyar"));
57         m_languages.insert("estonian", tr("Estonian - %1").arg("Eesti keel"));
58         m_languages.insert("dutch", tr("Dutch - %1").arg("Nederlands"));
59         m_languages.insert("italian", tr("Italian - %1").arg("Italiano"));
60         m_languages.insert("polish", tr("Polish - %1").arg("Polski"));
61 		m_languages.insert("swedish", tr("Swedish - %1").arg("Svenska"));
62 		m_languages.insert("galician", tr("Galician - %1").arg("Galego"));
63         m_languages.insert("indonesian", tr("Indonesian - %1").arg("Bahasa Indonesia"));
64         m_languages.insert("danish", tr("Danish - %1").arg("Dansk"));
65 
66         ushort t1[] = { 0x65e5, 0x672c, 0x8a9e, 0 };
67 		m_languages.insert("japanese", tr("Japanese - %1").arg(QString::fromUtf16(t1)));
68 
69 		ushort t2[] = { 0x0420, 0x0443, 0x0441, 0x0441, 0x043a, 0x0438, 0x0439, 0 };
70         m_languages.insert("russian", tr("Russian - %1").arg(QString::fromUtf16(t2)));
71 
72  		ushort t3[] = { 0x05e2, 0x05d1, 0x05e8, 0x05d9, 0x05ea, 0 };
73         m_languages.insert("hebrew", tr("Hebrew - %1").arg(QString::fromUtf16(t3)));
74 
75 		ushort t4[] = { 0x0639, 0x0631, 0x0628, 0x064a, 0 };
76         m_languages.insert("arabic", tr("Arabic - %1").arg(QString::fromUtf16(t4)));
77 
78 		ushort t5[] = { 0x0939, 0x093f, 0x0928, 0x094d, 0x0926, 0x0940, 0x0020, 0x0028, 0x092d, 0x093e, 0x0930, 0x0924, 0x0029, 0 };
79         m_languages.insert("hindi", tr("Hindi - %1").arg(QString::fromUtf16(t5)));
80 
81 		ushort t6[] = { 0x4e2d, 0x6587, 0x0020, 0x0028, 0x7b80, 0x4f53, 0x0029, 0 };
82         m_languages.insert("chinese_china", tr("Chinese (Simplified) - %1").arg(QString::fromUtf16(t6)));
83 
84         ushort t7[] = { 0x6b63, 0x9ad4, 0x4e2d, 0x6587, 0x0020, 0x0028, 0x7e41, 0x9ad4, 0x0029, 0 };
85         m_languages.insert("chinese_taiwan", tr("Chinese (Traditional) - %1").arg(QString::fromUtf16(t7)));
86 
87 		ushort t8[] = { 0x010C, 0x65, 0x0161, 0x74, 0x69, 0x6E, 0x61, 0 };
88         m_languages.insert("czech", tr("Czech - %1").arg(QString::fromUtf16(t8)));
89 
90 		ushort t9[] = { 0x52,  0x6F, 0x6D, 0xE2, 0x6E, 0x0103, 0 };
91 		m_languages.insert("romanian", tr("Romanian - %1").arg(QString::fromUtf16(t9)));
92 
93 		ushort t10[] = { 0x0E44, 0x0E17, 0x0E22, 0 };
94 		m_languages.insert("thai", tr("Thai - %1").arg(QString::fromUtf16(t10)));
95 
96 		ushort t11[] = { 0x0395, 0x03BB, 0x03BB, 0x03B7, 0x03BD, 0x03B9, 0x03BA, 0x03AC, 0};
97 		m_languages.insert("greek", tr("Greek - %1").arg(QString::fromUtf16(t11)));
98 
99 		ushort t12[] = { 0x0411, 0x044A, 0x043B, 0x0433, 0x0430, 0x0440, 0x0441, 0x043A, 0x0438, 0 };
100 		m_languages.insert("bulgarian", tr("Bulgarian - %1").arg(QString::fromUtf16(t12)));
101 
102 		ushort t13[] = { 0xD55C,  0xAD6D,  0xC5B4, 0};
103 		m_languages.insert("korean", tr("Korean - %1").arg(QString::fromUtf16(t13)));
104 
105 		ushort t14[] = { 0x53, 0x6C, 0x6F, 0x76, 0x65, 0x6E, 0x010D, 0x69, 0x6E, 0x61, 0};
106 		m_languages.insert("slovak", tr("Slovak - %1").arg(QString::fromUtf16(t14)));
107 
108         ushort t15[] = { 0x09AC,  0x09BE,   0x0982,   0x09B2,   0x09BE, 0 };
109         m_languages.insert("bengali", tr("Bengali - %1").arg(QString::fromUtf16(t15)));
110 
111         ushort t16[] = { 0x0641, 0x0627, 0x0631, 0x0633, 0x06CC, 0 };
112         m_languages.insert("persian", tr("Persian - %1").arg(QString::fromUtf16(t16)));
113 
114         ushort t17[] = {0x53, 0x6C, 0x6F, 0x76, 0x65, 0x6E, 0x0161, 0x010D, 0x69, 0x6E, 0x61, 0 };
115         m_languages.insert("slovenian", tr("Slovenian - %1").arg(QString::fromUtf16(t17)));
116 
117         ushort t18[] = { 0x092E,  0x0930,  0x093E,  0x0920,  0x0940, 0 };
118         m_languages.insert("marathi", tr("Marathi - %1").arg(QString::fromUtf16(t18)));
119 
120         ushort t19[] = {0x0443,  0x043A,  0x0440,  0x0430,  0x0457,  0x043D,  0x0441,  0x044C,  0x043A,  0x0430,  0x0020,  0x043C,  0x043E,  0x0432,  0x0430, 0};
121         m_languages.insert("ukrainian", tr("Ukrainian - %1").arg(QString::fromUtf16(t19)));
122 
123         ushort t20[] = { 0x46, 0x72, 0x61, 0x6E, 0xE7, 0x61, 0x69, 0x73, 0 };
124         m_languages.insert("french", tr("French - %1").arg(QString::fromUtf16(t20)));
125 
126         ushort t21[] = { 0x45, 0x73, 0x70, 0x61, 0xF1, 0x6F, 0x6C, 0 };
127         m_languages.insert("spanish", tr("Spanish - %1").arg(QString::fromUtf16(t21)));
128 
129         ushort t22[] = { 0x50, 0x6F, 0x72, 0x74, 0x75, 0x67, 0x75, 0xEA, 0x73, 0x20, 0x28, 0x45, 0x75, 0x72, 0x6F, 0x70, 0x65, 0x75, 0x29, 0 };
130         m_languages.insert("portuguese_portugal", tr("Portuguese (European)- %1").arg(QString::fromUtf16(t22)));
131 
132         ushort t23[] = { 0x50, 0x6F, 0x72, 0x74, 0x75, 0x67, 0x75, 0xEA, 0x73, 0x20, 0x28, 0x64, 0x6F, 0x20, 0x42, 0x72, 0x61, 0x73, 0x69, 0x6C, 0x29, 0 };
133         m_languages.insert("portuguese_brazil", tr("Portuguese (Brazilian) - %1").arg(QString::fromUtf16(t23)));
134 
135         ushort t24[] = { 0x54, 0xFC, 0x72, 0x6B, 0xE7, 0x65, 0 };
136         m_languages.insert("turkish", tr("Turkish - %1").arg(QString::fromUtf16(t24)));
137 
138         ushort t25[] = { 0x043C, 0x0430,  0x043A,  0x0435,  0x0434, 0x043E, 0x043D, 0x0441, 0x043A, 0x0438, 0x20, 0x0458, 0x0430, 0x0437, 0x0438, 0x043A, 0 };
139         m_languages.insert("macedonian", tr("Macedonian - %1").arg(QString::fromUtf16(t25)));
140 
141         ushort t26[] = { 0x0441, 0x0440, 0x043F, 0x0441, 0x043a, 0x0438, 0x0020, 0x0458, 0x20, 0x0435, 0x0437, 0x0438, 0x043A, 0 };
142         m_languages.insert("serbian", tr("Serbian - %1").arg(QString::fromUtf16(t26)));
143 
144         ushort t27[] = { 0x0627, 0x0631, 0x062f, 0x0648, 0 };
145         m_languages.insert("urdu", tr("Urdu - %1").arg(QString::fromUtf16(t27)));
146 
147 	}
148 
149 	if (m_localeList.count() == 0) {
150 		foreach (QFileInfo fileinfo, fileInfoList) {
151 			QString name = fileinfo.completeBaseName();
152 			name.replace("fritzing_", "");
153 			QStringList names = name.split("_");
154 			if (names.count() > 1) {
155 				name = names[0] + "_" + names[1].toUpper();
156 			}
157 			QLocale * locale = new QLocale(name);
158 			m_localeList.append(locale);
159 		}
160 	}
161 }
162 
163 
~TranslatorListModel()164 TranslatorListModel::~TranslatorListModel() {
165 }
166 
cleanup()167 void TranslatorListModel::cleanup() {
168 	foreach (QLocale * locale, m_localeList) {
169 		delete locale;
170 	}
171 	m_localeList.clear();
172 }
173 
data(const QModelIndex & index,int role) const174 QVariant TranslatorListModel::data ( const QModelIndex & index, int role) const
175 {
176 	if (role == Qt::DisplayRole && index.row() >= 0 && index.row() < m_localeList.count()) {
177 		QString languageString = QLocale::languageToString(m_localeList.at(index.row())->language());
178 		QString countryString = QLocale::countryToString(m_localeList.at(index.row())->country());
179 
180         //DebugDialog::debug(QString("language %1 %2").arg(languageString).arg(countryString));
181 
182 		// QLocale::languageToString() only returns an English string,
183 		// so put it through a language-dependent hash table.
184 		QString trLanguageString = m_languages.value(languageString.toLower(), "");
185 		if (trLanguageString.isEmpty()) {
186 			trLanguageString = m_languages.value(languageString.toLower() + "_" + countryString.toLower(), "");
187 		}
188 
189 		if (trLanguageString.isEmpty()) {
190 			foreach (QString key, m_languages.keys()) {
191 				if (key.startsWith(languageString.toLower())) {
192 					return m_languages.value(key);
193 				}
194 			}
195 
196 			return languageString;
197 		}
198 
199 		return trLanguageString;
200 	}
201 
202 	return emptyVariant;
203 
204 }
205 
rowCount(const QModelIndex & parent) const206 int TranslatorListModel::rowCount ( const QModelIndex & parent) const
207 {
208 	Q_UNUSED(parent);
209 
210 	return m_localeList.count();
211 }
212 
locale(int index)213 const QLocale * TranslatorListModel::locale( int index)
214 {
215 	if (index < 0 || index >= m_localeList.count()) return NULL;
216 
217 	return m_localeList.at(index);
218 }
219 
findIndex(const QString & language)220 int TranslatorListModel::findIndex(const QString & language) {
221 	int ix = 0;
222 	foreach (QLocale * locale, m_localeList) {
223 		//DebugDialog::debug(QString("find index %1 %2").arg(language).arg(locale->name()));
224 		if (language.compare(locale->name()) == 0) return ix;
225 		ix++;
226 	}
227 
228 	ix = 0;
229 	foreach (QLocale * locale, m_localeList) {
230 		if (locale->name().startsWith("en")) return ix;
231 		ix++;
232 	}
233 
234 	return 0;
235 
236 }
237 
238