mirror of
https://github.com/yeongpin/cursor-free-vip.git
synced 2025-08-02 20:47:35 +08:00
refactor: Streamline System Language Detection Logic
- Consolidate language detection methods into a single unified approach - Improve cross-platform language detection for Windows and Unix-like systems - Simplify error handling and add more robust language code mapping - Remove platform-specific detection methods in favor of a more generic approach - Enhance language fallback mechanism with better default handling
This commit is contained in:
parent
5bbba05f69
commit
24b0a5c09e
104
main.py
104
main.py
@ -5,13 +5,12 @@ import sys
|
|||||||
import json
|
import json
|
||||||
from logo import print_logo
|
from logo import print_logo
|
||||||
from colorama import Fore, Style, init
|
from colorama import Fore, Style, init
|
||||||
import ctypes
|
|
||||||
from ctypes import windll
|
|
||||||
import locale
|
import locale
|
||||||
import platform
|
import platform
|
||||||
|
|
||||||
# 只在 Windows 系统上导入 windll
|
# 只在 Windows 系统上导入 windll
|
||||||
if platform.system() == 'Windows':
|
if platform.system() == 'Windows':
|
||||||
|
import ctypes
|
||||||
from ctypes import windll
|
from ctypes import windll
|
||||||
|
|
||||||
# 初始化colorama
|
# 初始化colorama
|
||||||
@ -38,21 +37,22 @@ class Translator:
|
|||||||
self.fallback_language = 'en' # Fallback language if translation is missing
|
self.fallback_language = 'en' # Fallback language if translation is missing
|
||||||
self.load_translations()
|
self.load_translations()
|
||||||
|
|
||||||
def _detect_system_language(self):
|
def detect_system_language(self):
|
||||||
"""检测系统语言"""
|
"""Detect system language and return corresponding language code"""
|
||||||
system = platform.system()
|
try:
|
||||||
|
system = platform.system()
|
||||||
if system == 'Windows':
|
|
||||||
return self._detect_windows_language()
|
if system == 'Windows':
|
||||||
elif system == 'Darwin': # macOS
|
return self._detect_windows_language()
|
||||||
return self._detect_macos_language()
|
else:
|
||||||
elif system == 'Linux':
|
return self._detect_unix_language()
|
||||||
return self._detect_linux_language()
|
|
||||||
else:
|
except Exception as e:
|
||||||
return 'en' # 默认英语
|
print(f"{Fore.YELLOW}{EMOJI['INFO']} Failed to detect system language: {e}{Style.RESET_ALL}")
|
||||||
|
return 'en'
|
||||||
|
|
||||||
def _detect_windows_language(self):
|
def _detect_windows_language(self):
|
||||||
"""检测 Windows 系统语言"""
|
"""Detect language on Windows systems"""
|
||||||
try:
|
try:
|
||||||
# 确保我们在 Windows 上
|
# 确保我们在 Windows 上
|
||||||
if not hasattr(ctypes, 'windll'):
|
if not hasattr(ctypes, 'windll'):
|
||||||
@ -64,50 +64,44 @@ class Translator:
|
|||||||
threadid = user32.GetWindowThreadProcessId(hwnd, 0)
|
threadid = user32.GetWindowThreadProcessId(hwnd, 0)
|
||||||
layout_id = user32.GetKeyboardLayout(threadid) & 0xFFFF
|
layout_id = user32.GetKeyboardLayout(threadid) & 0xFFFF
|
||||||
|
|
||||||
# 根据键盘布局 ID 判断语言
|
# Map language ID to our language codes
|
||||||
if layout_id == 0x0804:
|
language_map = {
|
||||||
return 'zh_CN' # 简体中文
|
0x0409: 'en', # English
|
||||||
elif layout_id == 0x0404:
|
0x0404: 'zh_tw', # Traditional Chinese
|
||||||
return 'zh_TW' # 繁体中文
|
0x0804: 'zh_cn', # Simplified Chinese
|
||||||
else:
|
}
|
||||||
return 'en' # 默认英语
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error detecting Windows language: {e}")
|
|
||||||
return 'en'
|
|
||||||
|
|
||||||
def _detect_macos_language(self):
|
|
||||||
"""检测 macOS 系统语言"""
|
|
||||||
try:
|
|
||||||
# 使用 defaults 命令获取系统语言设置
|
|
||||||
import subprocess
|
|
||||||
result = subprocess.run(['defaults', 'read', '-g', 'AppleLanguages'],
|
|
||||||
capture_output=True, text=True)
|
|
||||||
output = result.stdout.strip()
|
|
||||||
|
|
||||||
# 解析输出
|
return language_map.get(layout_id, 'en')
|
||||||
if 'zh-Hans' in output:
|
except:
|
||||||
return 'zh_CN' # 简体中文
|
return self._detect_unix_language()
|
||||||
elif 'zh-Hant' in output or 'zh_TW' in output:
|
|
||||||
return 'zh_TW' # 繁体中文
|
|
||||||
else:
|
|
||||||
return 'en' # 默认英语
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error detecting macOS language: {e}")
|
|
||||||
return 'en'
|
|
||||||
|
|
||||||
def _detect_linux_language(self):
|
def _detect_unix_language(self):
|
||||||
"""检测 Linux 系统语言"""
|
"""Detect language on Unix-like systems (Linux, macOS)"""
|
||||||
try:
|
try:
|
||||||
# 检查环境变量
|
# Get the system locale
|
||||||
lang = os.environ.get('LANG', '').lower()
|
system_locale = locale.getdefaultlocale()[0]
|
||||||
if lang.startswith('zh_cn'):
|
if not system_locale:
|
||||||
return 'zh_CN'
|
|
||||||
elif lang.startswith('zh_tw'):
|
|
||||||
return 'zh_TW'
|
|
||||||
else:
|
|
||||||
return 'en'
|
return 'en'
|
||||||
except Exception as e:
|
|
||||||
print(f"Error detecting Linux language: {e}")
|
system_locale = system_locale.lower()
|
||||||
|
|
||||||
|
# Map locale to our language codes
|
||||||
|
if system_locale.startswith('zh_tw') or system_locale.startswith('zh_hk'):
|
||||||
|
return 'zh_tw'
|
||||||
|
elif system_locale.startswith('zh_cn'):
|
||||||
|
return 'zh_cn'
|
||||||
|
elif system_locale.startswith('en'):
|
||||||
|
return 'en'
|
||||||
|
|
||||||
|
# Try to get language from LANG environment variable as fallback
|
||||||
|
env_lang = os.getenv('LANG', '').lower()
|
||||||
|
if 'tw' in env_lang or 'hk' in env_lang:
|
||||||
|
return 'zh_tw'
|
||||||
|
elif 'cn' in env_lang:
|
||||||
|
return 'zh_cn'
|
||||||
|
|
||||||
|
return 'en'
|
||||||
|
except:
|
||||||
return 'en'
|
return 'en'
|
||||||
|
|
||||||
def load_translations(self):
|
def load_translations(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user