QtSpell 1.0.1
Spell checking for Qt text widgets
Codetable.cpp
1/* QtSpell - Spell checking for Qt text widgets.
2 * Copyright (c) 2014-2022 Sandro Mani
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#include "Codetable.hpp"
20#include <QCoreApplication>
21#include <QDir>
22#include <QFile>
23#include <QXmlStreamReader>
24#include <QtDebug>
25#include <libintl.h>
26
27#define ISO_639_DOMAIN "iso_639"
28#define ISO_3166_DOMAIN "iso_3166"
29
30namespace QtSpell {
31
33{
34 static Codetable codetable;
35 return &codetable;
36}
37
38void Codetable::lookup(const QString &language_code, QString &language_name, QString &country_name, QString& extra) const
39{
40 QStringList parts = language_code.split("_");
41 language_name = language_code;
42 country_name = "";
43 if(parts.isEmpty()) {
44 return;
45 }
46 if(parts.size() >= 1) {
47 language_name = m_languageTable.contains(parts[0]) ? m_languageTable.value(parts[0]) : parts[0];
48 }
49 if(parts.size() >= 2) {
50 country_name = m_countryTable.contains(parts[1]) ? m_countryTable.value(parts[1]) : parts[1];
51 }
52 if(parts.size() > 2) {
53 extra = QStringList(parts.mid(2)).join("_");
54 }
55}
56
57Codetable::Codetable()
58{
59#ifdef Q_OS_WIN32
60 QDir dataDir = QDir(QString("%1/../share").arg(QCoreApplication::applicationDirPath()));
61#else
62 QDir dataDir = QDir(ISO_CODES_PREFIX "/share").absolutePath();
63#endif
64
65 bindtextdomain(ISO_639_DOMAIN, dataDir.absoluteFilePath("locale").toLocal8Bit().data());
66 bind_textdomain_codeset(ISO_639_DOMAIN, "UTF-8");
67
68 bindtextdomain(ISO_3166_DOMAIN, dataDir.absoluteFilePath("locale").toLocal8Bit().data());
69 bind_textdomain_codeset(ISO_3166_DOMAIN, "UTF-8");
70
71 parse(dataDir, "iso_639.xml", parseIso639Elements, m_languageTable);
72 parse(dataDir, "iso_3166.xml", parseIso3166Elements, m_countryTable);
73}
74
75void Codetable::parseIso639Elements(const QXmlStreamReader &xml, QMap<QString, QString> &table)
76{
77 if(xml.name() == QStringLiteral("iso_639_entry") ){
78 QString name = xml.attributes().value("name").toString();
79 QString code = xml.attributes().value("iso_639_1_code").toString();
80 if(!name.isEmpty() && !code.isEmpty()){
81 name = QString::fromUtf8(dgettext(ISO_639_DOMAIN, name.toUtf8().constData()));
82 table.insert(code, name);
83 }
84 }
85}
86
87void Codetable::parseIso3166Elements(const QXmlStreamReader &xml, QMap<QString, QString> &table)
88{
89 if(xml.name() == QStringLiteral("iso_3166_entry") ){
90 QString name = xml.attributes().value("name").toString();
91 QString code = xml.attributes().value("alpha_2_code").toString();
92 if(!name.isEmpty() && !code.isEmpty()){
93 name = QString::fromUtf8(dgettext(ISO_3166_DOMAIN, name.toUtf8().constData()));
94 table.insert(code, name);
95 }
96 }
97}
98
99void Codetable::parse(const QDir& dataDir, const QString& basename, const parser_t& parser, QMap<QString, QString>& table)
100{
101 QString filename = QDir(QDir(dataDir.filePath("xml")).filePath("iso-codes")).absoluteFilePath(basename);
102 QFile file(filename);
103 if(!file.open(QIODevice::ReadOnly)){
104 qWarning() << "Failed to open " << file.fileName() << " for reading";
105 return;
106 }
107
108 QXmlStreamReader xml(&file);
109 while(!xml.atEnd() && !xml.hasError()){
110 if(xml.readNext() == QXmlStreamReader::StartElement){
111 parser(xml, table);
112 }
113 }
114}
115
116} // QtSpell
Class for translating locale identifiers into human readable strings.
Definition Codetable.hpp:34
static Codetable * instance()
Get codetable instance.
Definition Codetable.cpp:32
void lookup(const QString &language_code, QString &language_name, QString &country_name, QString &extra) const
Looks up the language and country name for the specified language code. If no matching entries are fo...
Definition Codetable.cpp:38
QtSpell namespace.
Definition Checker.cpp:77