diff --git a/LICENSE b/COPYING similarity index 100% rename from LICENSE rename to COPYING diff --git a/config.h b/config.h new file mode 100644 index 0000000..9bc2d9f --- /dev/null +++ b/config.h @@ -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 */ diff --git a/language.cpp b/language.cpp new file mode 100644 index 0000000..c42eb67 --- /dev/null +++ b/language.cpp @@ -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 +#include + +#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 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 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 Language::parseOptions(const QString options) +{ + QMap optionsDict; + for (int i=0; i