Skip to content

Conversation

@wang93wei
Copy link

@wang93wei wang93wei commented Oct 29, 2025

Summary
This PR enhances the locale detection mechanism on macOS to properly support Chinese language when the application is launched from
Finder. The previous implementation relied on user preference files, which has been replaced with a more robust system-based
language detection approach.

Problem
On macOS, when launching the Qt application from Finder, environment variables (LANG, LC_ALL) are often not set, causing
QLocale::system() to default to en_US even when the system language is set to Chinese. This resulted in the application
displaying in English instead of the user's preferred Chinese language.

Solution
Replaced the preference file-based approach with direct system language detection using macOS-specific commands:

  1. System Language Detection: Uses defaults command to read AppleLocale and AppleLanguages from system preferences
  2. Multiple Fallback Methods:
    • First attempts to read AppleLocale directly
    • Falls back to parsing AppleLanguages array if needed
    • Converts macOS language formats to Qt-compatible locale codes
  3. Cross-Platform Compatibility: All macOS-specific code is wrapped in #ifdef Q_OS_MACOS to ensure Linux systems remain unaffected

Key Changes

  • Removed: Dependency on ~/.android-file-transfer-locale-prefs file
  • Added: System-based language detection using macOS native commands
  • Enhanced: Better handling of Chinese language variants (Simplified/Traditional)
  • Maintained: All existing functionality for Linux and command-line launches

Testing

  • ✅ Chinese language loads correctly when launched from Finder on macOS
  • ✅ Existing command-line behavior preserved
  • ✅ Linux systems unaffected by macOS-specific changes
  • ✅ Proper fallback to English when system language is not supported

Files Modified

  • qt/main.cpp: Enhanced locale detection logic with system-based approach

Technical Details
The implementation uses QProcess to execute macOS defaults commands and QRegularExpression to parse the output, converting formats
like "zh-Hans-CN" to Qt's "zh_CN" format.

This commit resolves the issue where the Qt application would default to English
instead of Chinese when launched from Finder on macOS, even when the system
language is set to Chinese.

Key changes:
- Added macOS-specific locale detection when environment variables are empty
- Implemented fallback to Chinese locale when launched from Finder
- Added support for user locale preference file (~/.android-file-transfer-locale-prefs)
- Enhanced translation loading with multiple format support (zh_CN, zh-CN, zh)
- Added proper locale format handling for macOS vs translation file naming

Technical details:
- Uses #ifdef Q_OS_MACOS to ensure Linux compatibility
- Detects when LANG/LC_ALL environment variables are empty (Finder launch)
- Auto-detects Chinese locale if zh_CN translation is available
- Supports user preference override via preference file
- Implements fallback: localeName -> hyphen format -> language code

Testing:
- Verified Chinese language loads correctly from Finder
- Confirmed existing command-line behavior is preserved
- Tested preference file functionality
- Ensured Linux systems are unaffected by macOS-specific changes
@wang93wei wang93wei requested a review from whoozle November 3, 2025 19:49
@wang93wei wang93wei changed the title Fix macOS locale detection for Chinese language support Improve macOS Locale Detection for Chinese Language Support Nov 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants