Changeset View
Changeset View
Standalone View
Standalone View
src/generator/translatedcountrylist.cpp
Show All 20 Lines | |||||
21 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | 21 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
23 | * OTHER DEALINGS IN THE SOFTWARE. | 23 | * OTHER DEALINGS IN THE SOFTWARE. | ||
24 | */ | 24 | */ | ||
25 | 25 | | |||
26 | 26 | | |||
27 | #include "translatedcountrylist.h" | 27 | #include "translatedcountrylist.h" | ||
28 | 28 | | |||
29 | #include <QFile> | 29 | #include <QDebug> | ||
30 | #include <QDirIterator> | 30 | #include <QDirIterator> | ||
31 | #include <QFile> | ||||
31 | #include <QXmlStreamReader> | 32 | #include <QXmlStreamReader> | ||
32 | 33 | | |||
33 | TranslatedCountries::TranslationCountryMap TranslatedCountries::parseFile(QIODevice *device) | 34 | static void parseFile(QIODevice *device, const QString &lang, TranslatedCountries::TranslationCountryMap &map) | ||
34 | { | 35 | { | ||
35 | TranslationCountryMap map; | | |||
36 | QXmlStreamReader reader(device); | 36 | QXmlStreamReader reader(device); | ||
37 | const QStringList blacklist = { QLatin1String("eu"), QLatin1String("un"), QLatin1String("zz") }; | 37 | const QStringList blacklist = { QLatin1String("eu"), QLatin1String("un"), QLatin1String("zz") }; | ||
38 | while(!reader.atEnd()) { | 38 | while(!reader.atEnd()) { | ||
39 | QXmlStreamReader::TokenType type = reader.readNext(); | 39 | QXmlStreamReader::TokenType type = reader.readNext(); | ||
40 | switch(type) { | 40 | switch(type) { | ||
41 | case QXmlStreamReader::StartElement: | 41 | case QXmlStreamReader::StartElement: | ||
42 | if (reader.name() == QLatin1String("territory")) { | 42 | if (reader.name() == QLatin1String("territory")) { | ||
43 | const auto territory = reader.attributes().value(QLatin1String("type")).toString().toLower(); | 43 | const auto territory = reader.attributes().value(QLatin1String("type")).toString().toLower(); | ||
44 | if (territory.size() != 2 || !territory.at(0).isLetter() || !territory.at(1).isLetter() || blacklist.contains(territory)) { | 44 | if (territory.size() != 2 || !territory.at(0).isLetter() || !territory.at(1).isLetter() || blacklist.contains(territory)) { | ||
45 | break; | 45 | break; | ||
46 | } | 46 | } | ||
47 | const auto name = reader.readElementText(); | 47 | const auto name = reader.readElementText(); | ||
48 | if (!name.isEmpty()) { | 48 | if (!name.isEmpty()) { | ||
49 | map.insert(name, territory); | 49 | map.push_back({name, territory, lang}); | ||
50 | } | 50 | } | ||
51 | } | 51 | } | ||
52 | break; | 52 | break; | ||
53 | default: | 53 | default: | ||
54 | break; | 54 | break; | ||
55 | } | 55 | } | ||
56 | } | 56 | } | ||
57 | return map; | | |||
58 | } | 57 | } | ||
59 | 58 | | |||
60 | TranslatedCountries::TranslationCountryMap TranslatedCountries::parseFilePath(const QString &filePath) | 59 | static void parseFilePath(const QString &filePath, TranslatedCountries::TranslationCountryMap &map) | ||
61 | { | 60 | { | ||
62 | QFile f(filePath); | 61 | QFile f(filePath); | ||
63 | bool success = f.open(QIODevice::ReadOnly); | 62 | bool success = f.open(QIODevice::ReadOnly); | ||
64 | if (!success) { | 63 | if (!success) { | ||
65 | return {}; | 64 | qWarning() << "Failed to open" << f.fileName() << f.errorString(); | ||
65 | return; | ||||
66 | } | 66 | } | ||
67 | return parseFile(&f); | 67 | return parseFile(&f, QFileInfo(filePath).baseName(), map); | ||
68 | } | 68 | } | ||
69 | 69 | | |||
70 | TranslatedCountries::TranslationCountryMap TranslatedCountries::parseFilesRecursive(const QString &directoryPath) | 70 | TranslatedCountries::TranslationCountryMap TranslatedCountries::parseFilesRecursive(const QString &directoryPath) | ||
71 | { | 71 | { | ||
72 | QDirIterator it(directoryPath, {QStringLiteral("*.xml")}, QDir::NoDotAndDotDot | QDir::Files, QDirIterator::Subdirectories); | 72 | QDirIterator it(directoryPath, {QStringLiteral("*.xml")}, QDir::NoDotAndDotDot | QDir::Files, QDirIterator::Subdirectories); | ||
73 | TranslationCountryMap map; | 73 | TranslationCountryMap map; | ||
74 | while (it.hasNext()) { | 74 | while (it.hasNext()) { | ||
75 | it.next(); | 75 | it.next(); | ||
76 | QString path = it.filePath(); | 76 | QString path = it.filePath(); | ||
77 | map.unite(parseFilePath(path)); | 77 | parseFilePath(path, map); | ||
78 | } | 78 | } | ||
79 | return map; | 79 | return map; | ||
80 | } | 80 | } |