The Translatator class added, all translations moved to /usr/<local>/share/lxde/trans...
authorAlexander Sokolov <sokoloff.a@gmail.com>
Tue, 8 Oct 2013 17:50:24 +0000 (21:50 +0400)
committerAlexander Sokolov <sokoloff.a@gmail.com>
Tue, 8 Oct 2013 17:50:24 +0000 (21:50 +0400)
13 files changed:
CMakeLists.txt
aboutdialog/lxqtaboutdialog.cpp
aboutdialog/technicalinfo.cpp
addplugindialog/lxqtaddplugindialog.cpp
cmake/create_portable_headers.cmake
cmake/lxqt-config.cmake.in
lxqtlibtranslate.h [deleted file]
lxqtpowermanager.cpp
lxqtscreensaver.cpp
lxqttranslator.cpp [new file with mode: 0644]
lxqttranslator.h [new file with mode: 0644]
translatorsinfo/translatorsinfo.cpp
translatorsinfo/translatorsinfo.h

index bfcc129..36f65b1 100644 (file)
@@ -16,7 +16,7 @@ set(PUB_HDRS
     addplugindialog/lxqtaddplugindialog.h
     lxqtscreensaver.h
     lxqtapplication.h
-    lxqtlibtranslate.h
+    lxqttranslator.h
     lxqtprogramfinder.h
 
     configdialog/lxqtconfigdialog.h
@@ -38,7 +38,7 @@ set(PUBLIC_CLASSES
     LxQtAddPluginDialog
     LxQtScreenSaver
     LxQtApplication
-    LxQtLibTranslate
+    LxQtTranslator
     LxQtProgramFinder
 
     LxQtConfigDialog
@@ -67,6 +67,7 @@ set(SRCS
     lxqtsettings.cpp
     lxqtscreensaver.cpp
     lxqtapplication.cpp
+    lxqttranslator.cpp
     lxqtprogramfinder.cpp
 
     configdialog/lxqtconfigdialog.cpp
@@ -147,11 +148,11 @@ message(STATUS "")
 #************************************************
 # Build config.cmake files
 #************************************************
-set(LXQT_SHARE_DIR ${CMAKE_INSTALL_PREFIX}/share/lxqt/)
+set(LXQT_SHARE_DIR ${CMAKE_INSTALL_PREFIX}/share/lxqt)
+set(LXQT_TRANSLATIONS_DIR ${LXQT_SHARE_DIR}/translations)
 include(cmake/FindInstallConfigPath.cmake)
 
 set(LXQT_INCLUDE_DIR ${CMAKE_INSTALL_PREFIX}/include/lxqt)
-
 configure_file(
     ${CMAKE_CURRENT_SOURCE_DIR}/cmake/lxqt-config.cmake.in
     ${CMAKE_CURRENT_BINARY_DIR}/lxqt-config.cmake
@@ -163,11 +164,12 @@ configure_file(
 include_directories(${LXQT_INCLUDE_DIR})
 
 set(APP_SHARE_DIR ${CMAKE_INSTALL_PREFIX}/share/liblxqt)
-add_definitions(-DTRANSLATIONS_DIR=\"${APP_SHARE_DIR}\")
+
 
 # create the portable headers *******************
 include(create_portable_headers)
 create_portable_headers(PORTABLE_HEADERS ${PUBLIC_CLASSES})
+check_portable_headers("${PUB_HDRS}")
 #************************************************
 
 qt4_wrap_cpp(MOCS ${MOCS})
@@ -193,7 +195,7 @@ target_link_libraries(lxqt
     ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY}
     ${QT_QTDBUS_LIBRARY}
     ${X11_X11_LIB}
-    qtxdg
+    ${QTXDG_LIBRARY}
 )
 
 set_target_properties(lxqt PROPERTIES
@@ -204,7 +206,7 @@ set_target_properties(lxqt PROPERTIES
 install(TARGETS lxqt DESTINATION lib${LIB_SUFFIX})
 install(FILES ${PUB_HDRS}  DESTINATION ${LXQT_INCLUDE_DIR})
 install(FILES ${PORTABLE_HEADERS} DESTINATION ${LXQT_INCLUDE_DIR})
-install(FILES ${QM_FILES} DESTINATION share/liblxqt)
+install(FILES ${QM_FILES} DESTINATION ${LXQT_TRANSLATIONS_DIR})
 
 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lxqt-config.cmake DESTINATION share/cmake/lxqt)
 install(FILES cmake/lxqt_use.cmake DESTINATION share/cmake/lxqt)
index 484c4bb..8397e11 100644 (file)
@@ -28,7 +28,7 @@
 #include "lxqtaboutdialog.h"
 #include "ui_lxqtaboutdialog.h"
 #include "lxqtaboutdialog_p.h"
-#include "lxqtlibtranslate.h"
+#include "lxqttranslator.h"
 #include "technicalinfo.h"
 #include "translatorsinfo/translatorsinfo.h"
 #include <QDebug>
@@ -39,7 +39,7 @@ using namespace LxQt;
 
 AboutDialogPrivate::AboutDialogPrivate()
 {
-    libTranslate("liblxqt");
+    Translator::translateLibrary("liblxqt");
     setupUi(this);
 
     QString css="<style TYPE='text/css'> "
index 8be623e..5570194 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "technicalinfo.h"
 #include <qtxdg/xdgdirs.h>
+#include "lxqttranslator.h"
 
 using namespace LxQt;
 
@@ -154,7 +155,7 @@ TechnicalInfo::TechnicalInfo()
     TechInfoTable *table;
 
     // ******************************************
-    table = newTable("LXDE-Qt Desktop Toolbox - Technical Info");
+    table = newTable("LXDE-Qt Desktop Toolbox - Technical Info<p>");
 #ifdef DEBUG
     QString buildType("Debug");
 #else
@@ -166,7 +167,7 @@ TechnicalInfo::TechnicalInfo()
     table->add("Build type",           buildType);
     table->add("System Configuration", LXQT_ETC_XDG_DIR);
     table->add("Share Directory",      LXQT_SHARE_DIR);
-    table->add("Translations",         TRANSLATIONS_DIR);
+    table->add("Translations",         Translator::translationSearchPaths().join("<br>\n"));
 
 
     // ******************************************
@@ -178,7 +179,7 @@ TechnicalInfo::TechnicalInfo()
     table->add("Xdg Data Dirs",        xdgDirs.dataDirs().join(":"));
     table->add("Xdg Cache Home",       xdgDirs.cacheHome(false));
     table->add("Xdg Runtime Home",     xdgDirs.runtimeDir());
-    table->add("Xdg Autostart Dirs",   xdgDirs.autostartDirs().join(":"));
+    table->add("Xdg Autostart Dirs",   xdgDirs.autostartDirs().join("<br>\n"));
     table->add("Xdg Autostart Home",   xdgDirs.autostartHome(false));
 
 }
index d34746b..b817c9a 100644 (file)
@@ -40,7 +40,7 @@
 #include <QAbstractTextDocumentLayout>
 
 #include <QLineEdit>
-#include "lxqtlibtranslate.h"
+#include "lxqttranslator.h"
 
 using namespace LxQt;
 
@@ -152,7 +152,7 @@ AddPluginDialog::AddPluginDialog(const QStringList& desktopFilesDirs,
     ui(new Ui::AddPluginDialog),
     mTimerId(0)
 {
-    libTranslate("librazorqt");
+    Translator::translateLibrary("liblxqt");
     ui->setupUi(this);
 
     mPlugins = PluginInfo::search(desktopFilesDirs, serviceType, nameFilter);
index 6d49726..39b9e21 100644 (file)
@@ -28,3 +28,28 @@ function(create_portable_headers outfiles)
     set(${outfiles} ${${outfiles}} PARENT_SCOPE)
 endfunction()
 
+
+function(check_portable_headers)
+    file(GLOB links ${CMAKE_CURRENT_BINARY_DIR}/portableHeders/*)
+
+    foreach(f ${links})
+        file(READ ${f} content)
+
+        set(found False)
+        foreach(line ${content})
+            string(REGEX MATCH "#include \"(.*)\"" v ${line})
+            set(hFile ${CMAKE_MATCH_1})
+
+            string(REGEX MATCH ";.*${hFile};" v ";${ARGN};")
+            if(NOT v)
+                set(found True)
+            endif()
+        endforeach()
+
+        if(found)
+            message(FATAL_ERROR "Incorrect portable header: '${f}'")
+        endif()
+    endforeach()
+
+endfunction()
+
index 8480b99..863b3a8 100644 (file)
@@ -12,6 +12,8 @@
 #
 #  LXQT_SHARE_DIR     - This allows to install and read the configs from non-standard locations
 #
+#  LXQT_TRANSLATIONS_DIR - The default translations directory
+#
 #  LXQT_ETC_XDG_DIR   - XDG standards expects system-wide configuration files in the
 #                        /etc/xdg/razor location. Unfortunately QSettings we are using internally
 #                        can be overriden in the Qt compilation time to use different path for
@@ -55,6 +57,7 @@ set(LXQT_PATCH_VERSION @LXQT_PATCH_VERSION@)
 set(LXQT_VERSION       @LXQT_VERSION@)
 
 set(LXQT_SHARE_DIR     @LXQT_SHARE_DIR@)
+set(LXQT_TRANSLATIONS_DIR @LXQT_TRANSLATIONS_DIR@)
 set(LXQT_ETC_XDG_DIR   @LXQT_ETC_XDG_DIR@)
 set(LXQT_INCLUDE_DIR   @LXQT_INCLUDE_DIR@)
 set(LXQT_LIBRARY       lxqt)
diff --git a/lxqtlibtranslate.h b/lxqtlibtranslate.h
deleted file mode 100644 (file)
index f51fc77..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-/* BEGIN_COMMON_COPYRIGHT_HEADER
- * (c)LGPL2+
- *
- * Razor - a lightweight, Qt based, desktop toolset
- * http://razor-qt.org
- *
- * Copyright: 2011 Razor team
- * Authors:
- *   Alexander Sokoloff <sokoloff.a@gmail.com>
- *
- * This program or 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 2.1 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; if not, write to the
- * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301 USA
- *
- * END_COMMON_COPYRIGHT_HEADER */
-
-#ifndef LIBTRANSLATE_H
-#define LIBTRANSLATE_H
-
-#include <QTranslator>
-#include <QCoreApplication>
-#include <QLocale>
-#include <QSet>
-#include <QDebug>
-
-namespace LxQt
-{
-
-inline void libTranslate(const QString &name)
-{
-    static QSet<QString> loadedLibs;
-
-    if (loadedLibs.contains(name))
-        return;
-
-    loadedLibs.insert(name);
-
-    QString locale = QLocale::system().name();
-    QTranslator *translator = new QTranslator(qApp);
-    translator->load(QString("%1/%2_%3.qm").arg(TRANSLATIONS_DIR, name, locale));
-
-    QCoreApplication::installTranslator(translator);
-}
-
-} // namecpase LxQt
-#endif // LIBTRANSLATE_H
index aa4596d..6290058 100644 (file)
@@ -33,7 +33,7 @@
 #include <QApplication>
 #include <QDesktopWidget>
 #include <QtDebug>
-#include "lxqtlibtranslate.h"
+#include "lxqttranslator.h"
 
 using namespace LxQt;
 
@@ -83,7 +83,7 @@ PowerManager::PowerManager(QObject * parent, bool skipWarning)
     : QObject(parent),
         m_skipWarning(skipWarning)
 {
-    libTranslate("librazorqt");
+    Translator::translateLibrary("liblxqt");
     m_power = new Power(this);
 //    connect(m_power, SIGNAL(suspendFail()), this, SLOT(suspendFailed()));
 //    connect(m_power, SIGNAL(hibernateFail()), this, SLOT(hibernateFailed()));
index 7373c53..84a6a36 100644 (file)
 
 #include <qtxdg/xdgicon.h>
 #include "lxqtscreensaver.h"
-#include "lxqtlibtranslate.h"
+#include "lxqttranslator.h"
 
 using namespace LxQt;
 
 ScreenSaver::ScreenSaver(QObject * parent)
     : QObject(parent)
 {
-    LxQt::libTranslate("librazorqt");
+    Translator::translateLibrary("liblxqt");
     m_xdgProcess = new QProcess(this);
     connect(m_xdgProcess, SIGNAL(finished(int,QProcess::ExitStatus)),
             this, SLOT(xdgProcess_finished(int,QProcess::ExitStatus)));
diff --git a/lxqttranslator.cpp b/lxqttranslator.cpp
new file mode 100644 (file)
index 0000000..0d85a2f
--- /dev/null
@@ -0,0 +1,106 @@
+#include "lxqttranslator.h"
+#include <QTranslator>
+#include <QLocale>
+#include <QDebug>
+#include <QCoreApplication>
+#include <QLibraryInfo>
+#include <QStringList>
+#include <XdgDirs>
+#include <QFileInfo>
+
+using namespace LxQt;
+
+/************************************************
+
+ ************************************************/
+QStringList *getSearchPaths()
+{
+    static QStringList *searchPath = 0;
+
+    if (searchPath == 0)
+    {
+        searchPath = new QStringList();
+        *searchPath << XdgDirs::dataHome(false) + "/lxqt/translations";
+        *searchPath << XdgDirs::dataDirs("/lxqt/translations");
+        *searchPath << "/usr/local/share/lxqt/translations";
+        *searchPath << "/usr/share/lxqt/translations";
+    }
+
+    return searchPath;
+}
+
+
+/************************************************
+
+ ************************************************/
+QStringList LxQt::Translator::translationSearchPaths()
+{
+    return *(getSearchPaths());
+}
+
+
+/************************************************
+
+ ************************************************/
+void Translator::setTranslationSearchPaths(const QStringList &paths)
+{
+    QStringList *p = getSearchPaths();
+    p->clear();
+    *p << paths;
+}
+
+
+/************************************************
+
+ ************************************************/
+bool translate(const QString &name)
+{
+    QString locale = QLocale::system().name();
+
+    QStringList *paths = getSearchPaths();
+    foreach(QString path, *paths)
+    {
+        QTranslator *appTranslator = new QTranslator(qApp);
+        bool ok = appTranslator->load(name + "_" + locale, path);
+        if (ok)
+        {
+            QCoreApplication::installTranslator(appTranslator);
+            return ok;
+        }
+    }
+    return false;
+}
+
+
+/************************************************
+
+ ************************************************/
+bool Translator::translateApplication(const QString &applicationName)
+{
+    QString locale = QLocale::system().name();
+    QTranslator *qtTranslator = new QTranslator(qApp);
+    qtTranslator->load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
+    qApp->installTranslator(qtTranslator);
+
+
+    if (!applicationName.isEmpty())
+        return translate(applicationName);
+    else
+        return translate(QFileInfo(QCoreApplication::applicationFilePath()).baseName());
+}
+
+
+/************************************************
+
+ ************************************************/
+bool Translator::translateLibrary(const QString &libraryName)
+{
+    static QSet<QString> loadedLibs;
+
+    if (loadedLibs.contains(libraryName))
+        return true;
+
+    loadedLibs.insert(libraryName);
+
+    return translate(libraryName);
+}
diff --git a/lxqttranslator.h b/lxqttranslator.h
new file mode 100644 (file)
index 0000000..d5b3918
--- /dev/null
@@ -0,0 +1,69 @@
+/* BEGIN_COMMON_COPYRIGHT_HEADER
+ * (c)LGPL2+
+ *
+ * Razor - a lightweight, Qt based, desktop toolset
+ * http://razor-qt.org
+ *
+ * Copyright: 2013 LXDE-Qt team
+ * Authors:
+ *   Alexander Sokoloff <sokoloff.a@gmail.com>
+ *
+ * This program or 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 2.1 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; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ *
+ * END_COMMON_COPYRIGHT_HEADER */
+
+#ifndef LXQTTRANSLATOR_H
+#define LXQTTRANSLATOR_H
+
+#include <QStringList>
+
+namespace LxQt
+{
+/**
+  The Translator class provides internationalization support for application and librarioes.
+ **/
+class Translator
+{
+public:
+    /**
+      Returns a list of paths that the application will search translations files.
+     **/
+    static QStringList translationSearchPaths();
+
+    /**
+      Sets the list of directories to search translations. All existing paths
+      will be deleted and the path list will consist of the paths given in paths.
+     **/
+    static void setTranslationSearchPaths(const QStringList &paths);
+
+    /**
+      Loads translations for application. If applicationName is not specified,
+      then basename of QCoreApplication::applicationFilePath() is used.
+      Returns true if the translation is successfully loaded; otherwise returns false.
+    **/
+    static bool translateApplication(const QString &applicationName = "");
+
+    /**
+      Loads translations for application. If applicationName is not specified,
+      then basename of QCoreApplication::applicationFilePath() is used.
+      Returns true if the translation is successfully loaded; otherwise returns false.
+    **/
+    static bool translateLibrary(const QString &libraryName = "");
+
+};
+
+} // namespace LxQt
+#endif // LXQTTRANSLATOR_H
index b0ec342..62f0ee4 100644 (file)
@@ -370,7 +370,7 @@ TranslatorsInfo::~TranslatorsInfo()
 QString TranslatorsInfo::asHtml() const
 {
     QString ret;
-    foreach(Translator *translator, mItems)
+    foreach(TranslatorPerson *translator, mItems)
     {
         ret += "<li>" + translator->asHtml() + "</li>";
     }
@@ -383,11 +383,11 @@ QString TranslatorsInfo::asHtml() const
 void TranslatorsInfo::process(const QString &lang, const QString &englishName, const QString &nativeName, const QString &contact)
 {
     QString key = QString("%1:%2:%3").arg(englishName, nativeName, contact);
-    Translator *translator = mItems.value(key);
+    TranslatorPerson *translator = mItems.value(key);
 
     if (!translator)
     {
-        translator = new Translator(englishName, nativeName, contact);
+        translator = new TranslatorPerson(englishName, nativeName, contact);
         mItems.insert(key, translator);
     }
 
@@ -395,7 +395,7 @@ void TranslatorsInfo::process(const QString &lang, const QString &englishName, c
 }
 
 
-Translator::Translator(const QString &englishName, const QString &nativeName, const QString &contact)
+TranslatorPerson::TranslatorPerson(const QString &englishName, const QString &nativeName, const QString &contact)
 {
     mEnglishName = englishName;
 
@@ -421,7 +421,7 @@ Translator::Translator(const QString &englishName, const QString &nativeName, co
 }
 
 
-void Translator::addLanguage(QString langId)
+void TranslatorPerson::addLanguage(QString langId)
 {
     static QMap<QString, QString> mLanguagesList;
     if (mLanguagesList.isEmpty())
@@ -436,7 +436,7 @@ void Translator::addLanguage(QString langId)
 }
 
 
-QString Translator::asHtml()
+QString TranslatorPerson::asHtml()
 {
     QString ret(mInfo);
     ret += " - " + mLanguages.join(", ");
index 0e61cb1..985a41d 100644 (file)
 namespace LxQt
 {
 
-class Translator
+class TranslatorPerson
 {
 public:
-    Translator(const QString &englishName, const QString &nativeName, const QString &contact);
+    TranslatorPerson(const QString &englishName, const QString &nativeName, const QString &contact);
 
     QString englishName() const { return mEnglishName; }
     QString nativeName() const { return mNativeName; }
@@ -66,7 +66,7 @@ public:
     QString asHtml() const;
 
 private:
-    QMap<QString, Translator*> mItems;
+    QMap<QString, TranslatorPerson*> mItems;
     void process(const QString &lang, const QString &englishName, const QString &nativeName, const QString &contact);
 };