Very often case in Mac OS is to have translations inside an app bundle. And

searching these files require iterate through subdirectories.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2015-11-17 16:20:36 +02:00
parent ce80ae4523
commit cdec047f58
8 changed files with 68 additions and 18 deletions

View file

@ -40,6 +40,7 @@
#include <QCheckBox>
#include <QIcon>
#include <QFormLayout>
#include <QDirIterator>
//---------------------------------------------------------------------------------------------------------------------
TapeConfigurationPage::TapeConfigurationPage(QWidget *parent)
@ -147,8 +148,14 @@ QGroupBox *TapeConfigurationPage::LangGroup()
guiLabel = new QLabel(tr("GUI language"));
langCombo = new QComboBox;
QDir dir(qApp->translationsPath());
const QStringList fileNames = dir.entryList(QStringList("valentina_*.qm"));
QStringList fileNames;
QDirIterator it(qApp->translationsPath(), QStringList() << QStringList("valentina_*.qm"), QDir::Files,
QDirIterator::Subdirectories);
while (it.hasNext())
{
it.next();
fileNames.append(it.fileName());
}
for (int i = 0; i < fileNames.size(); ++i)
{

View file

@ -397,10 +397,14 @@ VTapeSettings *MApplication::TapeSettings()
}
//---------------------------------------------------------------------------------------------------------------------
QString MApplication::translationsPath() const
/**
* @brief translationsPath This function is implementation of the method VAbstractApplication::translationsPath.
*/
QString MApplication::translationsPath(const QString &locale) const
{
const QString trPath = QStringLiteral("/translations");
#ifdef Q_OS_WIN
Q_UNUSED(locale)
QDir dir(QApplication::applicationDirPath() + trPath);
if (dir.exists())
{
@ -411,7 +415,17 @@ QString MApplication::translationsPath() const
return QApplication::applicationDirPath() + "/../../valentina/bin" + trPath;
}
#elif defined(Q_OS_MAC)
QDir dirBundle(QApplication::applicationDirPath() + QStringLiteral("/../Resources") + trPath);
QString mainPath;
if (locale.isEmpty())
{
mainPath = QApplication::applicationDirPath() + QLatin1Literal("/../Resources") + trPath;
}
else
{
mainPath = QApplication::applicationDirPath() + QLatin1Literal("/../Resources") + trPath + QLatin1Literal("/")
+ locale + QLatin1Literal(".lproj");
}
QDir dirBundle(mainPath);
if (dirBundle.exists())
{
return dirBundle.absolutePath();
@ -429,13 +443,14 @@ QString MApplication::translationsPath() const
}
}
#else // Unix
Q_UNUSED(locale)
QDir dir1(QApplication::applicationDirPath() + trPath);
if (dir1.exists())
{
return dir1.absolutePath();
}
QDir dir2(QApplication::applicationDirPath() + "/../../valentina/bin" + trPath);
QDir dir2(QApplication::applicationDirPath() + QLatin1Literal("/../../valentina/bin") + trPath);
if (dir2.exists())
{
return dir2.absolutePath();

View file

@ -71,7 +71,7 @@ public:
virtual void OpenSettings() Q_DECL_OVERRIDE;
VTapeSettings *TapeSettings();
virtual QString translationsPath() const Q_DECL_OVERRIDE;
virtual QString translationsPath(const QString &locale = QString()) const Q_DECL_OVERRIDE;
QString diagramsPath() const;
void ShowDataBase();

View file

@ -364,13 +364,27 @@ bool VApplication::notify(QObject *receiver, QEvent *event)
}
//---------------------------------------------------------------------------------------------------------------------
QString VApplication::translationsPath() const
/**
* @brief translationsPath This function is implementation of the method VAbstractApplication::translationsPath.
*/
QString VApplication::translationsPath(const QString &locale) const
{
const QString trPath = QStringLiteral("/translations");
#ifdef Q_OS_WIN
Q_UNUSED(locale)
return QApplication::applicationDirPath() + trPath;
#elif defined(Q_OS_MAC)
QDir dirBundle(QApplication::applicationDirPath() + QStringLiteral("/../Resources") + trPath);
QString mainPath;
if (locale.isEmpty())
{
mainPath = QApplication::applicationDirPath() + QLatin1Literal("/../Resources") + trPath;
}
else
{
mainPath = QApplication::applicationDirPath() + QLatin1Literal("/../Resources") + trPath + QLatin1Literal("/")
+ locale + QLatin1Literal(".lproj");
}
QDir dirBundle(mainPath);
if (dirBundle.exists())
{
return dirBundle.absolutePath();
@ -388,6 +402,7 @@ QString VApplication::translationsPath() const
}
}
#else // Unix
Q_UNUSED(locale)
QDir dir(QApplication::applicationDirPath() + trPath);
if (dir.exists())
{

View file

@ -61,7 +61,7 @@ public:
void InitOptions();
virtual QString translationsPath() const Q_DECL_OVERRIDE;
virtual QString translationsPath(const QString &locale = QString()) const Q_DECL_OVERRIDE;
QString TapeFilePath() const;
QTimer *getAutoSaveTimer() const;

View file

@ -41,6 +41,7 @@
#include <QCheckBox>
#include <QIcon>
#include <QVBoxLayout>
#include <QDirIterator>
//---------------------------------------------------------------------------------------------------------------------
ConfigurationPage::ConfigurationPage(QWidget *parent)
@ -155,8 +156,14 @@ QGroupBox *ConfigurationPage::LangGroup()
QLabel *guiLabel = new QLabel(tr("GUI language"));
langCombo = new QComboBox;
QDir dir(qApp->translationsPath());
const QStringList fileNames = dir.entryList(QStringList("valentina_*.qm"));
QStringList fileNames;
QDirIterator it(qApp->translationsPath(), QStringList() << QStringList("valentina_*.qm"), QDir::Files,
QDirIterator::Subdirectories);
while (it.hasNext())
{
it.next();
fileNames.append(it.fileName());
}
for (int i = 0; i < fileNames.size(); ++i)
{

View file

@ -218,29 +218,29 @@ void VAbstractApplication::LoadTranslation(const QString &locale)
ClearTranslation();
qtTranslator = new QTranslator(this);
#if defined(Q_OS_WIN)
qtTranslator->load("qt_" + locale, translationsPath());
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
qtTranslator->load("qt_" + locale, translationsPath(locale));
#else
qtTranslator->load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
#endif
installTranslator(qtTranslator);
qtxmlTranslator = new QTranslator(this);
#if defined(Q_OS_WIN)
qtxmlTranslator->load("qtxmlpatterns_" + locale, translationsPath());
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
qtxmlTranslator->load("qtxmlpatterns_" + locale, translationsPath(locale));
#else
qtxmlTranslator->load("qtxmlpatterns_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
#endif
installTranslator(qtxmlTranslator);
appTranslator = new QTranslator(this);
appTranslator->load("valentina_" + locale, translationsPath());
appTranslator->load("valentina_" + locale, translationsPath(locale));
installTranslator(appTranslator);
const QString system = Settings()->GetPMSystemCode();
pmsTranslator = new QTranslator(this);
pmsTranslator->load("measurements_" + system + "_" + locale, translationsPath());
pmsTranslator->load("measurements_" + system + "_" + locale, translationsPath(locale));
installTranslator(pmsTranslator);
InitTrVars();//Very important do it after load QM files.

View file

@ -56,7 +56,13 @@ public:
virtual ~VAbstractApplication() Q_DECL_OVERRIDE;
virtual const VTranslateVars *TrVars()=0;
virtual QString translationsPath() const=0;
/**
* @brief translationsPath return path to the root directory that contain QM files.
* @param locale used only in Mac OS. If empty return path to the root directory. If not - return path to locale
* subdirectory inside an app bundle.
* @return path to a directory that contain QM files.
*/
virtual QString translationsPath(const QString &locale = QString()) const=0;
void LoadTranslation(const QString &locale);