0
0
mirror of https://github.com/yeongpin/cursor-free-vip.git synced 2025-04-24 08:25:23 +00:00

Merge pull request #751 from TranMC/main

Language configuration saved setting
This commit is contained in:
Pin Studios 2025-04-24 10:39:06 +08:00 committed by GitHub
commit 5fac4d6e45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 127 additions and 34 deletions

View File

@ -105,6 +105,12 @@ def setup_config(translator=None):
'Token': {
'refresh_server': 'https://token.cursorpro.com.cn',
'enable_refresh': True
},
'Language': {
'current_language': '', # Set by local system detection if empty
'fallback_language': 'en',
'auto_update_languages': 'True',
'language_cache_dir': os.path.join(config_dir, "language_cache")
}
}
@ -364,4 +370,4 @@ def get_config(translator=None):
global _config_cache
if _config_cache is None:
_config_cache = setup_config(translator)
return _config_cache
return _config_cache

View File

@ -32,7 +32,9 @@
"exiting": "Exiting ……",
"bypass_version_check": "Bypass Cursor Version Check",
"check_user_authorized": "Check User Authorized",
"bypass_token_limit": "Bypass Token Limit"
"bypass_token_limit": "Bypass Token Limit",
"language_config_saved": "Language configuration saved successfully",
"lang_invalid_choice": "Invalid choice. Please enter one of the following options: ({lang_choices})"
},
"languages": {
"ar": "Arabic",

View File

@ -31,7 +31,9 @@
"operation_cancelled_by_user": "Thao tác đã bị người dùng hủy",
"exiting": "Đang thoát ……",
"bypass_version_check": "Bỏ qua Kiểm tra Phiên bản Cursor",
"check_user_authorized": "Kiểm tra Quyền Người dùng"
"check_user_authorized": "Kiểm tra Quyền Người dùng",
"language_config_saved": "Đổi ngôn ngữ thành công",
"lang_invalid_choice": "Lựa chọn không hợp lệ. Vui lòng nhập một số từ ({lang_choices})"
},
"languages": {
"ar": "Tiếng Ả Rập",

View File

@ -32,7 +32,9 @@
"exiting": "退出中 ……",
"bypass_version_check": "绕过 Cursor 版本检查",
"check_user_authorized": "检查用户授权",
"bypass_token_limit": "绕过 Token 限制"
"bypass_token_limit": "绕过 Token 限制",
"language_config_saved": "语言配置保存成功",
"lang_invalid_choice": "选择无效。请输入以下选项之一:({lang_choices})"
},
"languages": {
"ar": "阿拉伯语",

View File

@ -32,8 +32,10 @@
"exiting": "退出中 ……",
"bypass_version_check": "繞過 Cursor 版本檢查",
"check_user_authorized": "檢查用戶授權",
"bypass_token_limit": "繞過 Token 限制"
},
"bypass_token_limit": "繞過 Token 限制",
"language_config_saved": "語言設定已成功儲存",
"lang_invalid_choice": "選擇無效。請輸入以下選項之一:({lang_choices})"
},
"languages": {
"ar": "阿拉伯語",
"en": "英文",

135
main.py
View File

@ -12,6 +12,8 @@ import subprocess
from config import get_config, force_update_config
import shutil
import re
from utils import get_user_documents_path
# Add these imports for Arabic support
try:
import arabic_reshaper
@ -86,8 +88,37 @@ def run_as_admin():
class Translator:
def __init__(self):
self.translations = {}
self.current_language = self.detect_system_language() # Use correct method name
self.fallback_language = 'en' # Fallback language if translation is missing
self.config = get_config()
# Create language cache directory if it doesn't exist
if self.config and self.config.has_section('Language'):
self.language_cache_dir = self.config.get('Language', 'language_cache_dir')
os.makedirs(self.language_cache_dir, exist_ok=True)
else:
self.language_cache_dir = None
# Set fallback language from config if available
self.fallback_language = 'en'
if self.config and self.config.has_section('Language') and self.config.has_option('Language', 'fallback_language'):
self.fallback_language = self.config.get('Language', 'fallback_language')
# Load saved language from config if available, otherwise detect system language
if self.config and self.config.has_section('Language') and self.config.has_option('Language', 'current_language'):
saved_language = self.config.get('Language', 'current_language')
if saved_language and saved_language.strip():
self.current_language = saved_language
else:
self.current_language = self.detect_system_language()
# Save detected language to config
if self.config.has_section('Language'):
self.config.set('Language', 'current_language', self.current_language)
config_dir = os.path.join(get_user_documents_path(), ".cursor-free-vip")
config_file = os.path.join(config_dir, "config.ini")
with open(config_file, 'w', encoding='utf-8') as f:
self.config.write(f)
else:
self.current_language = self.detect_system_language()
self.load_translations()
def detect_system_language(self):
@ -208,28 +239,48 @@ class Translator:
except:
return 'en'
def load_translations(self):
"""Load all available translations"""
try:
locales_dir = os.path.join(os.path.dirname(__file__), 'locales')
if hasattr(sys, '_MEIPASS'):
locales_dir = os.path.join(sys._MEIPASS, 'locales')
def download_language_file(self, lang_code):
"""Method kept for compatibility but now returns False as language files are integrated"""
print(f"{Fore.YELLOW}{EMOJI['INFO']} Languages are now integrated into the package, no need to download.{Style.RESET_ALL}")
return False
if not os.path.exists(locales_dir):
print(f"{Fore.RED}{EMOJI['ERROR']} Locales directory not found{Style.RESET_ALL}")
return
def load_translations(self):
"""Load all available translations from the integrated package"""
try:
# Collection of languages we've successfully loaded
loaded_languages = set()
locales_paths = []
# Check for PyInstaller bundle first
if hasattr(sys, '_MEIPASS'):
locales_paths.append(os.path.join(sys._MEIPASS, 'locales'))
# Check script directory next
script_dir = os.path.dirname(os.path.abspath(__file__))
locales_paths.append(os.path.join(script_dir, 'locales'))
# Also check current working directory
locales_paths.append(os.path.join(os.getcwd(), 'locales'))
for locales_dir in locales_paths:
if os.path.exists(locales_dir) and os.path.isdir(locales_dir):
for file in os.listdir(locales_dir):
if file.endswith('.json'):
lang_code = file[:-5] # Remove .json
try:
with open(os.path.join(locales_dir, file), 'r', encoding='utf-8') as f:
self.translations[lang_code] = json.load(f)
loaded_languages.add(lang_code)
loaded_any = True
except (json.JSONDecodeError, UnicodeDecodeError) as e:
print(f"{Fore.RED}{EMOJI['ERROR']} Error loading {file}: {e}{Style.RESET_ALL}")
continue
for file in os.listdir(locales_dir):
if file.endswith('.json'):
lang_code = file[:-5] # Remove .json
try:
with open(os.path.join(locales_dir, file), 'r', encoding='utf-8') as f:
self.translations[lang_code] = json.load(f)
except (json.JSONDecodeError, UnicodeDecodeError) as e:
print(f"{Fore.RED}{EMOJI['ERROR']} Error loading {file}: {e}{Style.RESET_ALL}")
continue
except Exception as e:
print(f"{Fore.RED}{EMOJI['ERROR']} Failed to load translations: {e}{Style.RESET_ALL}")
# Create at least minimal English translations for basic functionality
self.translations['en'] = {"menu": {"title": "Menu", "exit": "Exit", "invalid_choice": "Invalid choice"}}
def fix_arabic(self, text):
if self.current_language == 'ar' and arabic_reshaper and get_display:
@ -277,7 +328,11 @@ class Translator:
def get_available_languages(self):
"""Get list of available languages"""
return list(self.translations.keys())
# Get currently loaded languages
available_languages = list(self.translations.keys())
# Sort languages alphabetically for better display
return sorted(available_languages)
# Create translator instance
translator = Translator()
@ -394,21 +449,45 @@ def select_language():
print(f"\n{Fore.CYAN}{EMOJI['LANG']} {translator.get('menu.select_language')}:{Style.RESET_ALL}")
print(f"{Fore.YELLOW}{'' * 40}{Style.RESET_ALL}")
# Get available languages either from local directory or GitHub
languages = translator.get_available_languages()
languages_count = len(languages)
# Display all available languages with proper indices
for i, lang in enumerate(languages):
lang_name = translator.get(f"languages.{lang}")
lang_name = translator.get(f"languages.{lang}", fallback=lang)
print(f"{Fore.GREEN}{i}{Style.RESET_ALL}. {lang_name}")
try:
choice = input(f"\n{EMOJI['ARROW']} {Fore.CYAN}{translator.get('menu.input_choice', choices=f'0-{len(languages)-1}')}: {Style.RESET_ALL}")
if choice.isdigit() and 0 <= int(choice) < len(languages):
translator.set_language(languages[int(choice)])
# Use the actual number of languages in the prompt
choice = input(f"\n{EMOJI['ARROW']} {Fore.CYAN}{translator.get('menu.input_choice', choices=f'0-{languages_count-1}')}: {Style.RESET_ALL}")
if choice.isdigit() and 0 <= int(choice) < languages_count:
selected_language = languages[int(choice)]
translator.set_language(selected_language)
# Save selected language to config
config = get_config()
if config and config.has_section('Language'):
config.set('Language', 'current_language', selected_language)
# Get config path from user documents
config_dir = os.path.join(get_user_documents_path(), ".cursor-free-vip")
config_file = os.path.join(config_dir, "config.ini")
# Write updated config
with open(config_file, 'w', encoding='utf-8') as f:
config.write(f)
print(f"{Fore.GREEN}{EMOJI['SUCCESS']} {translator.get('menu.language_config_saved', language=translator.get(f'languages.{selected_language}', fallback=selected_language))}{Style.RESET_ALL}")
return True
else:
print(f"{Fore.RED}{EMOJI['ERROR']} {translator.get('menu.invalid_choice')}{Style.RESET_ALL}")
# Show invalid choice message with the correct range
print(f"{Fore.RED}{EMOJI['ERROR']} {translator.get('menu.lang_invalid_choice', lang_choices=f'0-{languages_count-1}')}{Style.RESET_ALL}")
return False
except (ValueError, IndexError):
print(f"{Fore.RED}{EMOJI['ERROR']} {translator.get('menu.invalid_choice')}{Style.RESET_ALL}")
except (ValueError, IndexError) as e:
print(f"{Fore.RED}{EMOJI['ERROR']} {translator.get('menu.lang_invalid_choice', lang_choices=f'0-{languages_count-1}')}{Style.RESET_ALL}")
return False
def check_latest_version():