mirror of
https://github.com/Qv2ray/Qv2ray.git
synced 2025-05-20 02:40:20 +08:00
cmake: translations: some minor improvements
This commit is contained in:
parent
310307513b
commit
e00e743dc9
@ -86,6 +86,7 @@ set(QV2RAY_SOURCES
|
||||
src/common/LogHighlighter.cpp
|
||||
src/common/QJsonModel.cpp
|
||||
src/common/QvHelpers.cpp
|
||||
src/common/QvTranslator.cpp
|
||||
src/components/autolaunch/QvAutoLaunch.cpp
|
||||
src/components/geosite/QvGeositeReader.cpp
|
||||
src/components/icmping/win/ICMPPinger.cpp
|
||||
|
79
src/common/QvTranslator.cpp
Normal file
79
src/common/QvTranslator.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "QvTranslator.hpp"
|
||||
|
||||
#include "base/Qv2rayLog.hpp"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
#include <QStandardPaths>
|
||||
#include <QString>
|
||||
#include <QStringBuilder>
|
||||
#include <QTranslator>
|
||||
#include <memory>
|
||||
|
||||
using namespace Qv2ray::base;
|
||||
|
||||
namespace Qv2ray::common
|
||||
{
|
||||
std::optional<QStringList> QvTranslator::getAvailableLanguages()
|
||||
{
|
||||
if (!this->translationDir)
|
||||
return std::nullopt;
|
||||
|
||||
auto languages = QDir(this->translationDir.value()).entryList(QStringList{ "*.qm" }, QDir::Hidden | QDir::Files);
|
||||
|
||||
if (languages.empty())
|
||||
return std::nullopt;
|
||||
|
||||
std::transform(languages.begin(), languages.end(), languages.begin(), [](QString &fileName) { return fileName.replace(".qm", ""); });
|
||||
LOG(MODULE_UI, "Found translations: " + languages.join(" "))
|
||||
return languages;
|
||||
}
|
||||
|
||||
void QvTranslator::reloadTranslation(const QString &code)
|
||||
{
|
||||
if (!translationDir)
|
||||
return;
|
||||
|
||||
QTranslator *translatorNew = new QTranslator();
|
||||
translatorNew->load(code + ".qm", this->translationDir.value());
|
||||
|
||||
this->pTranslator.reset(translatorNew);
|
||||
}
|
||||
|
||||
std::optional<QString> QvTranslator::deduceTranslationDir()
|
||||
{
|
||||
// path searching list.
|
||||
QStringList searchPaths = {
|
||||
// 1st: application dir
|
||||
QApplication::applicationDirPath() + "/translations",
|
||||
#ifdef QV2RAY_TRANSLATION_PATH
|
||||
// 2nd: platform-specific dir
|
||||
QString(QV2RAY_TRANSLATION_PATH),
|
||||
#endif
|
||||
};
|
||||
// 3rd: standard path dirs
|
||||
searchPaths << QStandardPaths::locateAll(QStandardPaths::DataLocation, "translations", QStandardPaths::LocateDirectory);
|
||||
//
|
||||
// iterate through the paths
|
||||
for (auto path : searchPaths)
|
||||
{
|
||||
DEBUG(MODULE_UI, "Testing for translation path: " + path)
|
||||
if (QvTranslator::testTranslationDir(path))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
// sadly, none match
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool QvTranslator::testTranslationDir(const QString &targetDir)
|
||||
{
|
||||
const auto translations = QDir(targetDir).entryList(QStringList{ "*.qm" }, QDir::Hidden | QDir::Files);
|
||||
|
||||
/// @todo: add some debug traces
|
||||
|
||||
return !translations.empty();
|
||||
}
|
||||
} // namespace Qv2ray::common
|
@ -1,9 +1,5 @@
|
||||
#pragma once
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
#include <QStandardPaths>
|
||||
#include <QString>
|
||||
#include <QStringBuilder>
|
||||
#include <QTranslator>
|
||||
#include <memory>
|
||||
|
||||
@ -12,16 +8,31 @@ namespace Qv2ray::common
|
||||
class QvTranslator
|
||||
{
|
||||
public:
|
||||
QvTranslator()
|
||||
explicit QvTranslator()
|
||||
{
|
||||
this->reloadTranslationDirs();
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief search and deduce a directory for translation file
|
||||
* @return (if any) the deduced path
|
||||
*
|
||||
* @author DuckSoft <realducksoft@gmail.com>
|
||||
* @todo add some debug output
|
||||
*/
|
||||
static std::optional<QString> deduceTranslationDir();
|
||||
/**
|
||||
* @brief get the available languages.
|
||||
* @return (if available) languages (zh_CN, en_US, ...)
|
||||
*/
|
||||
std::optional<QStringList> getAvailableLanguages();
|
||||
/**
|
||||
* @brief reload the translation from file
|
||||
* @param code eg: en_US, zh_CN, ...
|
||||
*/
|
||||
void reloadTranslation(const QString &);
|
||||
inline bool isTranslationAvailable()
|
||||
inline bool isTranslationAvailable() const
|
||||
{
|
||||
return this->translationDir.has_value();
|
||||
};
|
||||
@ -31,91 +42,15 @@ namespace Qv2ray::common
|
||||
};
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief test the translation directory
|
||||
* @param targetDir the directory to judge
|
||||
* @return if the translation directory seems good to use
|
||||
*/
|
||||
static bool testTranslationDir(const QString &);
|
||||
|
||||
public:
|
||||
std::unique_ptr<QTranslator> pTranslator;
|
||||
std::optional<QString> translationDir;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief get the available languages.
|
||||
* @return (if available) languages (zh_CN, en_US, ...)
|
||||
*/
|
||||
std::optional<QStringList> QvTranslator::getAvailableLanguages()
|
||||
{
|
||||
if (!this->translationDir)
|
||||
return std::nullopt;
|
||||
|
||||
auto languages = QDir(this->translationDir.value()).entryList(QStringList{ "*.qm" }, QDir::Hidden | QDir::Files);
|
||||
|
||||
if (languages.empty())
|
||||
return std::nullopt;
|
||||
|
||||
std::transform(languages.begin(), languages.end(), languages.begin(), [](QString &fileName) { return fileName.replace(".qm", ""); });
|
||||
|
||||
return languages;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief reload the translation from file
|
||||
* @param code eg: en_US, zh_CN, ...
|
||||
*/
|
||||
void QvTranslator::reloadTranslation(const QString &code)
|
||||
{
|
||||
if (!translationDir)
|
||||
return;
|
||||
|
||||
QTranslator *translatorNew = new QTranslator();
|
||||
translatorNew->load(code + ".qm", this->translationDir.value());
|
||||
|
||||
this->pTranslator.reset(translatorNew);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief search and deduce a directory for translation file
|
||||
* @return (if any) the deduced path
|
||||
*
|
||||
* @author DuckSoft <realducksoft@gmail.com>
|
||||
* @todo add some debug output
|
||||
*/
|
||||
std::optional<QString> QvTranslator::deduceTranslationDir()
|
||||
{
|
||||
// path searching list.
|
||||
auto searchPaths = { // 1st: application dir
|
||||
QApplication::applicationDirPath() + "/translations",
|
||||
#ifdef QV2RAY_TRANSLATION_PATH
|
||||
// 2nd: platform-specific dir
|
||||
QString(QV2RAY_TRANSLATION_PATH),
|
||||
#endif
|
||||
// 3rd: standard path dirs
|
||||
QStandardPaths::locate(QStandardPaths::DataLocation, "translations", QStandardPaths::LocateDirectory)
|
||||
};
|
||||
|
||||
// iterate through the paths
|
||||
for (auto path : searchPaths)
|
||||
{
|
||||
if (QvTranslator::testTranslationDir(path))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
// sadly, none match
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief test the translation directory
|
||||
* @param targetDir the directory to judge
|
||||
* @return if the translation directory seems good to use
|
||||
*/
|
||||
bool QvTranslator::testTranslationDir(const QString &targetDir)
|
||||
{
|
||||
const auto translations = QDir(targetDir).entryList(QStringList{ "*.qm" }, QDir::Hidden | QDir::Files);
|
||||
|
||||
/// @todo: add some debug traces
|
||||
|
||||
return !translations.empty();
|
||||
}
|
||||
} // namespace Qv2ray::common
|
||||
|
Loading…
Reference in New Issue
Block a user