Skip to content

Commit

Permalink
Implement translations #19
Browse files Browse the repository at this point in the history
 * Look for and apply any locale override requests on startup
 * Properly set and install translation files
 * Use compile defined path via cmake to know from where to load baked translation files
 * Fallback to using applicationDirPath() if failing to load from above defined path for translation files
  • Loading branch information
SneWs committed Nov 25, 2024
1 parent 753c678 commit 9426eb2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 20 deletions.
21 changes: 13 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ if (WIN32)
add_definitions(-DWINDOWS_BUILD)
endif (WIN32)

get_filename_component(FULL_LIBDIR_PATH
"${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/tail-tray" ABSOLUTE)

add_definitions(-DTRANSLATIONS_DIR="${FULL_LIBDIR_PATH}")

message(STATUS "Full path of translations install path: ${FULL_LIBDIR_PATH}")

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Network)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Network)

Expand Down Expand Up @@ -76,17 +83,15 @@ else ()
qt_add_executable(tail-tray
MANUAL_FINALIZATION
${PROJECT_SOURCES}

)

endif ()

qt_standard_project_setup(I18N_TRANSLATED_LANGUAGES en_US sv_SE)

qt_add_translations(tail-tray TS_FILES
tail_tray_sv_SE.ts
tail_tray_en_US.ts
translations/tail_tray_sv_SE.ts
translations/tail_tray_en_US.ts
)
qt_standard_project_setup(I18N_TRANSLATED_LANGUAGES en_US sv_SE)

target_link_libraries(tail-tray PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Network)

Expand All @@ -105,9 +110,9 @@ if (UNIX AND NOT APPLE)
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/applications
)

install(FILES tail_tray_en_US.ts tail_tray_sv_SE.ts
DESTINATION ${CMAKE_INSTALL_LIBDIR}/tail-tray
)
install(FILES "${CMAKE_BINARY_DIR}/tail_tray_en_US.qm"
"${CMAKE_BINARY_DIR}/tail_tray_sv_SE.qm"
DESTINATION "${FULL_LIBDIR_PATH}")

install(FILES icons/tailscale.png
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor/128x128/apps
Expand Down
50 changes: 38 additions & 12 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QStyleFactory>
#endif

int main(int argc, char *argv[])
int main(int argc, char** argv)
{
QCoreApplication::setOrganizationName("grenangen");
QCoreApplication::setOrganizationDomain("grenangen.se");
Expand All @@ -21,27 +21,53 @@ int main(int argc, char *argv[])
#endif
QApplication::setQuitOnLastWindowClosed(false);

// Locale setup
QLocale locale = QLocale::C;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--locale") == 0) {
locale = QLocale(argv[++i]);
}
}

// No valid/or none given for locale at startup
if (locale == QLocale::C) {
locale = QLocale::system();
}
else {
// Set as default
QLocale::setDefault(locale);
}

qDebug() << "Will use locale: " << locale.name() << "for application";

// Setup and load translations
QString translationDirRoot = QApplication::applicationDirPath();
#if defined(TRANSLATIONS_DIR)
qDebug() << "Using TRANSLATION_DIR from cmake compile definition!";
translationDirRoot = TRANSLATIONS_DIR;
#endif

qDebug() << "Translation dir: " << translationDirRoot;

QTranslator translator;

QLocale locale = QLocale::system();
QDir dir = QApplication::applicationDirPath();
auto didLoad = translator.load(locale, "tail_tray", "_", dir.absolutePath());
// Try to load
auto didLoad = translator.load(locale, "tail_tray", "_", translationDirRoot);
if (didLoad)
QCoreApplication::installTranslator(&translator);
else {
// Load from default install din
#if !defined(WINDOWS_BUILD)
// /usr/local/lib/tail-tray
dir = QDir("/usr/local/lib/tail-tray");
didLoad = translator.load(locale, "tail_tray", "_", dir.absolutePath());
// Load from fallback, eg same path as the binary is located at
didLoad = translator.load(locale, "tail_tray", "_",
QApplication::applicationDirPath());

if (didLoad) {
QCoreApplication::installTranslator(&translator);
}
#endif
else {
qDebug() << "Failed to load translations from all known locations";
}
}

qDebug() << "Tail tray loaded translations from" << dir.absolutePath();

MainWindow w;
w.hide();

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 9426eb2

Please sign in to comment.