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 <QCheckBox>
#include <QIcon> #include <QIcon>
#include <QFormLayout> #include <QFormLayout>
#include <QDirIterator>
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
TapeConfigurationPage::TapeConfigurationPage(QWidget *parent) TapeConfigurationPage::TapeConfigurationPage(QWidget *parent)
@ -147,8 +148,14 @@ QGroupBox *TapeConfigurationPage::LangGroup()
guiLabel = new QLabel(tr("GUI language")); guiLabel = new QLabel(tr("GUI language"));
langCombo = new QComboBox; langCombo = new QComboBox;
QDir dir(qApp->translationsPath()); QStringList fileNames;
const QStringList fileNames = dir.entryList(QStringList("valentina_*.qm")); 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) 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"); const QString trPath = QStringLiteral("/translations");
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
Q_UNUSED(locale)
QDir dir(QApplication::applicationDirPath() + trPath); QDir dir(QApplication::applicationDirPath() + trPath);
if (dir.exists()) if (dir.exists())
{ {
@ -411,7 +415,17 @@ QString MApplication::translationsPath() const
return QApplication::applicationDirPath() + "/../../valentina/bin" + trPath; return QApplication::applicationDirPath() + "/../../valentina/bin" + trPath;
} }
#elif defined(Q_OS_MAC) #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()) if (dirBundle.exists())
{ {
return dirBundle.absolutePath(); return dirBundle.absolutePath();
@ -429,13 +443,14 @@ QString MApplication::translationsPath() const
} }
} }
#else // Unix #else // Unix
Q_UNUSED(locale)
QDir dir1(QApplication::applicationDirPath() + trPath); QDir dir1(QApplication::applicationDirPath() + trPath);
if (dir1.exists()) if (dir1.exists())
{ {
return dir1.absolutePath(); return dir1.absolutePath();
} }
QDir dir2(QApplication::applicationDirPath() + "/../../valentina/bin" + trPath); QDir dir2(QApplication::applicationDirPath() + QLatin1Literal("/../../valentina/bin") + trPath);
if (dir2.exists()) if (dir2.exists())
{ {
return dir2.absolutePath(); return dir2.absolutePath();

View File

@ -71,7 +71,7 @@ public:
virtual void OpenSettings() Q_DECL_OVERRIDE; virtual void OpenSettings() Q_DECL_OVERRIDE;
VTapeSettings *TapeSettings(); VTapeSettings *TapeSettings();
virtual QString translationsPath() const Q_DECL_OVERRIDE; virtual QString translationsPath(const QString &locale = QString()) const Q_DECL_OVERRIDE;
QString diagramsPath() const; QString diagramsPath() const;
void ShowDataBase(); 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"); const QString trPath = QStringLiteral("/translations");
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
Q_UNUSED(locale)
return QApplication::applicationDirPath() + trPath; return QApplication::applicationDirPath() + trPath;
#elif defined(Q_OS_MAC) #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()) if (dirBundle.exists())
{ {
return dirBundle.absolutePath(); return dirBundle.absolutePath();
@ -388,6 +402,7 @@ QString VApplication::translationsPath() const
} }
} }
#else // Unix #else // Unix
Q_UNUSED(locale)
QDir dir(QApplication::applicationDirPath() + trPath); QDir dir(QApplication::applicationDirPath() + trPath);
if (dir.exists()) if (dir.exists())
{ {

View File

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

View File

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

View File

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

View File

@ -56,7 +56,13 @@ public:
virtual ~VAbstractApplication() Q_DECL_OVERRIDE; virtual ~VAbstractApplication() Q_DECL_OVERRIDE;
virtual const VTranslateVars *TrVars()=0; 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); void LoadTranslation(const QString &locale);