Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

Windows: follow the system language (#16)

zzz14657206806150cc19e17b
diff --git a/desktop/app/cpp/app_helpers.cpp b/desktop/app/cpp/app_helpers.cpp
index 7eac2e4..4ee99dc 100644
--- a/desktop/app/cpp/app_helpers.cpp
+++ b/desktop/app/cpp/app_helpers.cpp
@@ -2,10 +2,15 @@
#include "app_helpers.h"
#include "video_view.h"
+#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
+#include <QtCore/QLibraryInfo>
+#include <QtCore/QLocale>
#include <QtCore/QMimeData>
#include <QtCore/QPointer>
+#include <QtCore/QStringList>
+#include <QtCore/QTranslator>
#include <QtCore/QUrl>
#include <QtCore/QtEnvironmentVariables>
#include <QtGui/QDrag>
@@ -173,4 +178,42 @@ bool start_external_drag(rust::Str file_path, rust::Str text_fallback)
return drag.exec(Qt::CopyAction) != Qt::IgnoreAction;
}
+bool install_system_translator(rust::Str dir)
+{
+ const QString folder = QString::fromUtf8(dir.data(), static_cast<qsizetype>(dir.size()));
+ const QLocale locale = QLocale::system();
+
+ // Qt's own strings: file dialogs, context menus, the standard buttons.
+ // windeployqt stages a merged `qt_<locale>.qm` in `translations/` beside the
+ // executable, while a build from source finds `qtbase_<locale>.qm` in Qt's
+ // own folder, so try both names in both places.
+ const QStringList qtDirs{
+ folder + QStringLiteral("/translations"),
+ QLibraryInfo::path(QLibraryInfo::TranslationsPath),
+ };
+ bool qtInstalled = false;
+ for (const QString &qtDir : qtDirs) {
+ for (const char *name : {"qtbase", "qt"}) {
+ auto *qt = new QTranslator(QCoreApplication::instance());
+ if (qt->load(locale, QString::fromLatin1(name), QStringLiteral("_"), qtDir)) {
+ QCoreApplication::installTranslator(qt);
+ qtInstalled = true;
+ break;
+ }
+ delete qt;
+ }
+ if (qtInstalled)
+ break;
+ }
+
+ // NectarLink's own strings, staged beside the executable. A locale with no
+ // translation (`nectarlink_es_ES.qm` missing) simply stays English.
+ auto *app = new QTranslator(QCoreApplication::instance());
+ if (!app->load(locale, QStringLiteral("nectarlink"), QStringLiteral("_"), folder)) {
+ delete app;
+ return false;
+ }
+ return QCoreApplication::installTranslator(app);
+}
+
diff --git a/desktop/app/cpp/app_helpers.h b/desktop/app/cpp/app_helpers.h
index 11d749e..527df11 100644
--- a/desktop/app/cpp/app_helpers.h
+++ b/desktop/app/cpp/app_helpers.h
@@ -37,5 +37,13 @@ void register_qml_app_engine(QQmlApplicationEngine &engine);
// so items dragged out of the Shelf can be dropped into Explorer or any app.
bool start_external_drag(rust::Str file_path, rust::Str text_fallback);
+// Installs the NectarLink and Qt translations for the system locale. `dir` is
+// the folder holding the executable, where windeployqt and the packaging
+// script stage the .qm files. Call after QGuiApplication is created and before
+// the QML engine loads, so that every qsTr() resolves on the first pass and no
+// retranslation is needed. Returns true when NectarLink's own translation was
+// found and installed.
+bool install_system_translator(rust::Str dir);
+
diff --git a/desktop/app/src/bridge/native.rs b/desktop/app/src/bridge/native.rs
index 9f07bb6..c65bc1d 100644
--- a/desktop/app/src/bridge/native.rs
+++ b/desktop/app/src/bridge/native.rs
@@ -33,5 +33,11 @@ pub mod ffi {
fn register_qml_app_engine(engine: Pin<&mut QQmlApplicationEngine>);
/// Starts a native OS drag-and-drop operation for a local file or text.
fn start_external_drag(file_path: &str, text_fallback: &str) -> bool;
+
+ /// Installs the NectarLink and Qt translations for the system locale,
+ /// looking in `dir` for the .qm files. Must run after the
+ /// `QGuiApplication` exists and before any QML engine loads. Returns
+ /// true when NectarLink's own translation was installed.
+ fn install_system_translator(dir: &str) -> bool;
}
}
diff --git a/desktop/app/src/main.rs b/desktop/app/src/main.rs
index f67f5ec..c9500ac 100644
--- a/desktop/app/src/main.rs
+++ b/desktop/app/src/main.rs
@@ -384,6 +384,19 @@ fn install_panic_logging() {
}));
}
+/// Installs the translation for the system locale. Call after the
+/// `QGuiApplication` exists and before any QML engine loads: the QML is
+/// compiled ahead of time, so `qsTr()` is only resolved when a component is
+/// built, and installing first means every string is right on the first pass
+/// and nothing has to be retranslated.
+fn install_system_translator() {
+ let Some(dir) = std::env::current_exe().ok().and_then(|exe| exe.parent().map(Path::to_path_buf)) else {
+ return;
+ };
+ let installed = bridge::native::ffi::install_system_translator(&dir.to_string_lossy());
+ tracing::debug!(installed, "system translator");
+}
+
/// The installer's or uninstaller's window: no core, no data folder, no log
/// file (removal deletes that folder).
fn run_setup_window(mode: setup::Mode) -> ExitCode {
@@ -404,6 +417,7 @@ fn run_setup_window(mode: setup::Mode) -> ExitCode {
app.as_mut().set_application_name(&QString::from("NectarLink Setup"));
app.as_mut().set_application_version(&QString::from(env!("CARGO_PKG_VERSION")));
}
+ install_system_translator();
bridge::native::ffi::load_bundled_fonts();
for size in [16, 20, 24, 32, 40, 48, 64, 256] {
bridge::native::ffi::add_app_icon_image(size, &mark::render(size as usize));
@@ -587,6 +601,7 @@ fn main() -> ExitCode {
app.as_mut().set_application_name(&QString::from("Nectarlink"));
app.as_mut().set_application_version(&QString::from(env!("CARGO_PKG_VERSION")));
}
+ install_system_translator();
bridge::native::ffi::keep_running_without_windows();
// Text falls back to system fonts if these are missing, so it's not fatal.
let fonts = bridge::native::ffi::load_bundled_fonts();
diff --git a/scripts/package.ps1 b/scripts/package.ps1
index 652fa8a..0183dc3 100644
--- a/scripts/package.ps1
+++ b/scripts/package.ps1
@@ -5,8 +5,8 @@
.DESCRIPTION
1. Builds the release app (unless -SkipBuild).
- 2. Stages it with the Qt libraries, plugins and QML modules it uses
- (windeployqt), the Microsoft C++ runtime, and the license notices.
+ 2. Stages it with the Qt libraries, plugins, QML modules and translations it
+ uses (windeployqt), the Microsoft C++ runtime, and the license notices.
3. Packs the folder into target\package\Nectarlink-<version>-x64-setup.exe
with NSIS (unless -StageOnly).
@@ -91,10 +91,28 @@ Copy-Item $exe $stage
Copy-Item $vcamDll $stage
Invoke-Native "windeployqt" {
- & $windeployqt --verbose 0 --release --qmldir (Join-Path $root 'desktop\app\qml') --no-translations `
+ & $windeployqt --verbose 0 --release --qmldir (Join-Path $root 'desktop\app\qml') `
--no-system-d3d-compiler --no-opengl-sw --no-compiler-runtime --dir $stage (Join-Path $stage 'nectarlink-desktop.exe')
}
+# Translations. windeployqt stages Qt's own .qm files (in translations/, so its
+# file dialogs and standard buttons follow the system language). NectarLink's
+# are compiled from the .ts files the repository keeps; they land beside the
+# executable, which is where the app looks for them.
+$translationSources = @()
+if (Test-Path (Join-Path $root 'desktop\app\i18n')) {
+ $translationSources = @(Get-ChildItem (Join-Path $root 'desktop\app\i18n') -Filter '*.ts')
+}
+if ($translationSources.Count -gt 0) {
+ $lrelease = Join-Path $QtRoot 'bin\lrelease.exe'
+ if (-not (Test-Path $lrelease)) { throw "lrelease not found under $QtRoot; pass -QtRoot" }
+ foreach ($source in $translationSources) {
+ $ts = $source.FullName
+ $qm = Join-Path $stage "$($source.BaseName).qm"
+ Invoke-Native "lrelease $($source.Name)" { & $lrelease $ts -qm $qm }
+ }
+}
+
# QtQuick.Dialogs brings every Controls style along. The file picker is
# Windows' own dialog; only Basic, Windows and FluentWinUI3 (the default
# style on Windows) can ever be loaded, so the rest is left out.