Skip to content

Commit

Permalink
Initial translation support (from issue #63)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenstorp committed Sep 30, 2014
1 parent 05dcc7f commit 9cb1883
Show file tree
Hide file tree
Showing 4 changed files with 393 additions and 15 deletions.
54 changes: 41 additions & 13 deletions src/openambit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,41 @@ set ( openambit_HDRS
)

set ( openambit_SRCS
confirmbetadialog.cpp
devicemanager.cpp
logentry.cpp
logstore.cpp
main.cpp
mainwindow.cpp
devicemanager.cpp
settingsdialog.cpp
settings.cpp
logstore.cpp
logentry.cpp
udevlistener.cpp
confirmbetadialog.cpp
settingsdialog.cpp
single_application.cpp
udevlistener.cpp
)

set ( openambit_UIS
confirmbetadialog.ui
mainwindow.ui
settingsdialog.ui
confirmbetadialog.ui
)

set ( openambit_RSCS
resources.qrc
)

set ( openambit_MOCS
mainwindow.h
confirmbetadialog.h
devicemanager.h
settingsdialog.h
settings.h
logstore.h
udevlistener.h
confirmbetadialog.h
mainwindow.h
settings.h
settingsdialog.h
single_application.h
udevlistener.h
)

set (FILES_TO_TRANSLATE ${openambit_SRCS} ${openambit_UIS} ${openambit_HDRS} ${openambit_MOCS})

set ( APP_ICON ${PROJECT_SOURCE_DIR}/icons/icon_disconnected.png )

set ( CMAKE_INSTALL_UDEVRULESDIR /lib/udev/rules.d
Expand All @@ -76,13 +78,38 @@ mark_as_advanced ( CMAKE_INSTALL_UDEVRULESDIR )

add_subdirectory("${PROJECT_SOURCE_DIR}/movescount")

######### Translations
file (GLOB TRANSLATIONS_FILES translations/*.ts)

option (UPDATE_TRANSLATIONS "Update source translation translations/*.ts")
if (UPDATE_TRANSLATIONS)
qt4_create_translation(QM_FILES ${FILES_TO_TRANSLATE} ${TRANSLATIONS_FILES})
else (UPDATE_TRANSLATIONS)
qt4_add_translation(QM_FILES ${TRANSLATIONS_FILES})
endif (UPDATE_TRANSLATIONS)

# Create translations QRC file - ts.qrc
set(TRANSLATIONS_QRC "${CMAKE_CURRENT_BINARY_DIR}/ts.qrc")
file(WRITE ${TRANSLATIONS_QRC} "<RCC>\n\t<qresource prefix=\"/translations\">")
foreach(QM_FILE ${QM_FILES})
get_filename_component(QM_FILE_NAME ${QM_FILE} NAME)
file(APPEND ${TRANSLATIONS_QRC} "\n\t\t<file alias=\"${QM_FILE_NAME}\">${QM_FILE_NAME}</file>")
endforeach()
file(APPEND ${TRANSLATIONS_QRC} "\n\t</qresource>\n</RCC>")
list(APPEND openambit_RSCS ${TRANSLATIONS_QRC})

# prevent the generated files from being deleted during make clean
set_directory_properties(PROPERTIES CLEAN_NO_CUSTOM true)

######### Translations

QT4_WRAP_UI(UIS ${openambit_UIS})
QT4_ADD_RESOURCES(RSCS ${openambit_RSCS})
QT4_WRAP_CPP(MOCS ${openambit_MOCS})

add_definitions( -DAPP_VERSION="${OPENAMBIT_VERSION}" )

add_executable ( openambit ${openambit_SRCS} ${UIS} ${RSCS} ${TRS} ${MOCS} )
add_executable ( openambit ${openambit_SRCS} ${UIS} ${RSCS} ${MOCS} )

target_link_libraries ( openambit ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTNETWORK_LIBRARY} ${LIBAMBIT_LIBS} ${UDEV_LIBS} ${ZLIB_LIBRARY} ${QJSON_LIBRARIES} )

Expand All @@ -96,3 +123,4 @@ install ( FILES ${OPENAMBIT_SOURCE_DIR}/deployment/openambit.desktop
install ( FILES ${APP_ICON}
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor/64x64/apps
RENAME openambit.png )

38 changes: 38 additions & 0 deletions src/openambit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
#include <unistd.h>
#include "mainwindow.h"
#include <QSettings>
#include <QTranslator>
#include <QLibraryInfo>

#include "single_application.h"

static void initTranslations(void);

int main(int argc, char *argv[])
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
Expand All @@ -51,6 +55,18 @@ int main(int argc, char *argv[])
QCoreApplication::setOrganizationName("Openambit");
QCoreApplication::setApplicationName("Openambit");

// Handle forced localisation / translation
Q_FOREACH(QString argu, a.arguments()) {
const static QString localeParam = "-locale:";
if (argu.startsWith(localeParam)) {
QLocale::setDefault(QLocale(argu.mid(sizeof(localeParam))));
break;
}
}

// Initialize translations
initTranslations();

MainWindow w;

// Connect single application message bus
Expand All @@ -60,3 +76,25 @@ int main(int argc, char *argv[])

return a.exec();
}

static void initTranslations(void)
{
QLocale locale;
QTranslator *qtTranslator = new QTranslator();
qtTranslator->load("qt_" + locale.name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
if (qtTranslator->isEmpty()) {
delete qtTranslator;
}
else {
qApp->installTranslator(qtTranslator);
}

QTranslator *openambitTranslator = new QTranslator();
openambitTranslator->load(":/translations/openambit_" + locale.name());
if (openambitTranslator->isEmpty()) {
delete openambitTranslator;
}
else {
qApp->installTranslator(openambitTranslator);
}
}
6 changes: 4 additions & 2 deletions src/openambit/movescount/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ set ( openambit_SRCS
${openambit_SRCS}
${CMAKE_CURRENT_SOURCE_DIR}/movescount.cpp
${CMAKE_CURRENT_SOURCE_DIR}/movescountjson.cpp
${CMAKE_CURRENT_SOURCE_DIR}/movescountlogchecker.cpp
${CMAKE_CURRENT_SOURCE_DIR}/movescountlogdirentry.cpp
${CMAKE_CURRENT_SOURCE_DIR}/movescountxml.cpp
${CMAKE_CURRENT_SOURCE_DIR}/movescountlogchecker.cpp
PARENT_SCOPE
)

set ( openambit_MOCS
${openambit_MOCS}
${CMAKE_CURRENT_SOURCE_DIR}/movescount.h
${CMAKE_CURRENT_SOURCE_DIR}/movescountjson.h
${CMAKE_CURRENT_SOURCE_DIR}/movescountlogchecker.h
${CMAKE_CURRENT_SOURCE_DIR}/movescountlogdirentry.h
${CMAKE_CURRENT_SOURCE_DIR}/movescountxml.h
${CMAKE_CURRENT_SOURCE_DIR}/movescountlogchecker.h
PARENT_SCOPE
)

set (FILES_TO_TRANSLATE ${FILES_TO_TRANSLATE} ${openambit_SRCS} ${openambit_MOCS} PARENT_SCOPE)
Loading

0 comments on commit 9cb1883

Please sign in to comment.