mirror of
https://github.com/arcan1s/qtadds-language.git
synced 2025-04-24 07:27:20 +00:00
update
This commit is contained in:
parent
dfa17ea90f
commit
335e73572e
12
config.h
Normal file
12
config.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#ifndef LANGUAGE_CONFIG_H
|
||||||
|
#define LANGUAGE_CONFIG_H
|
||||||
|
|
||||||
|
|
||||||
|
// put your languages here comma separated
|
||||||
|
// the first language is default language
|
||||||
|
#define LANGUAGES "en"
|
||||||
|
// language key in the configuration file
|
||||||
|
#define LANGUAGE_KEY "LANGUAGE"
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* LANGUAGE_CONFIG_H */
|
114
language.cpp
Normal file
114
language.cpp
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
* Copyright (C) 2014 Evgeniy Alekseev *
|
||||||
|
* *
|
||||||
|
* This library is free software; you can redistribute it and/or *
|
||||||
|
* modify it under the terms of the GNU Lesser General Public *
|
||||||
|
* License as published by the Free Software Foundation; either *
|
||||||
|
* version 3.0 of the License, or (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* This library is distributed in the hope that it will be useful, *
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
|
||||||
|
* Lesser General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU Lesser General Public *
|
||||||
|
* License along with this library. *
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "language.h"
|
||||||
|
|
||||||
|
#include <QFile>
|
||||||
|
#include <QLocale>
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
|
||||||
|
Language::Language()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QString Language::checkLanguage(const QString language)
|
||||||
|
{
|
||||||
|
QStringList availableLanguages = getAvailableLanguages();
|
||||||
|
if (availableLanguages.count() == 0) return QString();
|
||||||
|
for (int i=0; i<availableLanguages.count(); i++)
|
||||||
|
if (language == availableLanguages[i])
|
||||||
|
return availableLanguages[i];
|
||||||
|
for (int i=0; i<availableLanguages.count(); i++)
|
||||||
|
if (language.contains(availableLanguages[i] + QChar('_')))
|
||||||
|
return availableLanguages[i];
|
||||||
|
|
||||||
|
return availableLanguages[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QString Language::defineLanguage(const QString configPath, const QString options)
|
||||||
|
{
|
||||||
|
QMap<QString, QString> optionsDict = parseOptions(options);
|
||||||
|
if (optionsDict.contains(QString(LANGUAGE_KEY)))
|
||||||
|
if (getAvailableLanguages().contains(optionsDict[QString(LANGUAGE_KEY)]))
|
||||||
|
return optionsDict[QString(LANGUAGE_KEY)];
|
||||||
|
|
||||||
|
QString language;
|
||||||
|
language = defineLanguageFromFile(configPath);
|
||||||
|
if (language.isEmpty())
|
||||||
|
language = defineLanguageFromLocale();
|
||||||
|
language = checkLanguage(language);
|
||||||
|
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QString Language::defineLanguageFromFile(const QString configPath)
|
||||||
|
{
|
||||||
|
QMap<QString, QString> settings;
|
||||||
|
if (configPath.isEmpty())
|
||||||
|
return QString("");
|
||||||
|
QFile configFile(configPath);
|
||||||
|
QString fileStr;
|
||||||
|
if (!configFile.open(QIODevice::ReadOnly))
|
||||||
|
return QString("");
|
||||||
|
while (true) {
|
||||||
|
fileStr = QString(configFile.readLine()).trimmed();
|
||||||
|
if ((fileStr.isEmpty()) && (!configFile.atEnd())) continue;
|
||||||
|
if ((fileStr[0] == QChar('#')) && (!configFile.atEnd())) continue;
|
||||||
|
if ((fileStr[0] == QChar(';')) && (!configFile.atEnd())) continue;
|
||||||
|
if (fileStr.contains(QChar('=')))
|
||||||
|
settings[fileStr.split(QChar('='))[0]] = fileStr.split(QChar('='))[1];
|
||||||
|
if (configFile.atEnd()) break;
|
||||||
|
}
|
||||||
|
configFile.close();
|
||||||
|
|
||||||
|
if (settings.contains(QString(LANGUAGE_KEY)))
|
||||||
|
return settings[QString(LANGUAGE_KEY)];
|
||||||
|
else
|
||||||
|
return QString("");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QString Language::defineLanguageFromLocale()
|
||||||
|
{
|
||||||
|
return QLocale::system().name();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QStringList Language::getAvailableLanguages()
|
||||||
|
{
|
||||||
|
return QString(LANGUAGES).split(QChar(','));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QMap<QString, QString> Language::parseOptions(const QString options)
|
||||||
|
{
|
||||||
|
QMap<QString, QString> optionsDict;
|
||||||
|
for (int i=0; i<options.split(QChar(',')).count(); i++) {
|
||||||
|
if (options.split(QChar(','))[i].split(QChar('=')).count() < 2)
|
||||||
|
continue;
|
||||||
|
optionsDict[options.split(QChar(','))[i].split(QChar('='))[0]] =
|
||||||
|
options.split(QChar(','))[i].split(QChar('='))[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return optionsDict;
|
||||||
|
}
|
42
language.h
Normal file
42
language.h
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
* Copyright (C) 2014 Evgeniy Alekseev *
|
||||||
|
* *
|
||||||
|
* This library is free software; you can redistribute it and/or *
|
||||||
|
* modify it under the terms of the GNU Lesser General Public *
|
||||||
|
* License as published by the Free Software Foundation; either *
|
||||||
|
* version 3.0 of the License, or (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* This library is distributed in the hope that it will be useful, *
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
|
||||||
|
* Lesser General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU Lesser General Public *
|
||||||
|
* License along with this library. *
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
#ifndef LANGUAGE_H
|
||||||
|
#define LANGUAGE_H
|
||||||
|
|
||||||
|
#include <QStringList>
|
||||||
|
|
||||||
|
|
||||||
|
class Language : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit Language();
|
||||||
|
static QString checkLanguage(const QString language);
|
||||||
|
static QString defineLanguage(const QString configPath,
|
||||||
|
const QString options = QString("OPTIONS"));
|
||||||
|
static QString defineLanguageFromFile(const QString configPath);
|
||||||
|
static QString defineLanguageFromLocale();
|
||||||
|
static QStringList getAvailableLanguages();
|
||||||
|
|
||||||
|
private:
|
||||||
|
static QMap<QString, QString> parseOptions(const QString options);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* LANGUAGE_H */
|
Loading…
Reference in New Issue
Block a user