Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ build
*.user
doc
.vscode

# AI doc
GEMINI.md
IFLOW.md
72 changes: 69 additions & 3 deletions qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include <QLocale>
#include <QMessageBox>
#include <QTranslator>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <mtp/log.h>
#if QT_VERSION >= 0x050000
# include <QGuiApplication>
Expand Down Expand Up @@ -54,6 +57,26 @@ int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Q_INIT_RESOURCE(android_file_transfer);

#ifdef Q_OS_MACOS
// On macOS, when launched from Finder, environment variables are often not set
// This causes QLocale::system() to default to en_US even if system language is Chinese
if (qgetenv("LANG").isEmpty() && qgetenv("LC_ALL").isEmpty()) {
// Check if user has a locale preference file first
QFile prefsFile(QDir::home().filePath(".android-file-transfer-locale-prefs"));
if (prefsFile.exists() && prefsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&prefsFile);
QString preferredLocale = in.readLine().trimmed();
if (!preferredLocale.isEmpty()) {
qputenv("LANG", preferredLocale.toUtf8());
qputenv("LC_ALL", preferredLocale.toUtf8());
}
prefsFile.close();
}
// If no preference file, let Qt detect system locale naturally
// Don't force any specific language - let the fallback mechanism handle it
}
#endif

QCoreApplication::setApplicationName("aft-linux-qt");
QCoreApplication::setOrganizationDomain("whoozle.github.io");
Expand All @@ -64,14 +87,57 @@ int main(int argc, char *argv[])
#endif

QTranslator qtTranslator;
QString localeName = QLocale::system().name();

#ifdef Q_OS_MACOS
// On macOS, check for user preference file first
QFile prefsFile(QDir::home().filePath(".android-file-transfer-locale-prefs"));
if (prefsFile.exists() && prefsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&prefsFile);
QString preferredLocale = in.readLine().trimmed();
if (!preferredLocale.isEmpty()) {
// Extract just the locale part (e.g., "zh_CN.UTF-8" -> "zh_CN")
localeName = preferredLocale.split(".").first();
}
prefsFile.close();
}
// Note: When launched from Finder with no preference file, we let Qt detect system locale naturally
// The fallback mechanism in translation loading will handle unknown languages
#endif

qtTranslator.load("qt_" + QLocale::system().name(),
qtTranslator.load("qt_" + localeName,
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
app.installTranslator(&qtTranslator);

QTranslator aTranslator;
aTranslator.load(":/android-file-transfer-linux_" + QLocale::system().name());
app.installTranslator(&aTranslator);

// Try to load translation with multiple fallback strategies
bool translationLoaded = false;
QStringList localeAttempts;

// Add all possible locale formats to try
localeAttempts << localeName; // Original: "zh_CN"
localeAttempts << localeName.replace("_", "-"); // Hyphen: "zh-CN"
localeAttempts << localeName.split("-").first().split("_").first(); // Language only: "zh"

// Also try some common variations for Chinese
if (localeName.startsWith("zh")) {
localeAttempts << "zh-CN"; // Simplified Chinese
localeAttempts << "zh-TW"; // Traditional Chinese
localeAttempts << "zh_CN"; // Underscore variant
}

for (const QString& attempt : localeAttempts) {
QString resourcePath = ":/android-file-transfer-linux_" + attempt;
if (aTranslator.load(resourcePath)) {
translationLoaded = true;
break;
}
}

if (translationLoaded) {
app.installTranslator(&aTranslator);
}

MainWindow w;

Expand Down
2 changes: 1 addition & 1 deletion qt/translations/android-file-transfer-linux_zh-CN.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding = "utf-8" ?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN">
<context>
Expand Down