#!/usr/bin/env python
# -*- coding: utf-8 -*-

import locale
import os
import ctypes
import platform

MESSAGES = {
    'zh': {
        'start_instruction': '开始处理指令',
        'start_execute': '开始执行代码块',
        'execute_result': '执行结果',
        'mcp_init': '正在初始化MCP服务器，如果是第一次加载，可能会耗时较长，请耐心等待...',
        'call_tool': '开始调用MCP工具',
        'call_tool_result': 'MCP工具调用结果',
        'found_mcp': '从{0}个启用的MCP服务器找到{1}个工具',
        'mcp_info': "MCP服务器：{}，工具：{}",
        'mcp_status': 'MCP服务器状态: {}',
        'Start parsing message': '开始解析消息',
        'Message parse result': '消息解析结果',
        'start_feedback': '开始反馈结果',
        'end_instruction': '结束处理指令',
        'no_context': '未找到上下文信息',
        'sync_to_cloud': '正在上传结果，请稍候...',
        'upload_success': '上传成功，私密保存地址：{}',
        'upload_failed': '上传失败 (状态码: {})',
        'reset_warning': '严重警告：这将重新初始化❗❗❗',
        'reset_confirm': '如果你确定要继续，请输入 y',
        'llm_response': '响应',
        'env_description': '环境变量名称和意义',
        'description': '描述',
        'unknown_format': '不支持的文件格式',
        'ask_for_packages': '申请安装第三方包',
        'agree_packages': '如果同意，请输入',
        'ask_for_env': '申请获取环境变量 {}，用途',
        'env_exist': '环境变量 {} 存在，返回给代码使用',
        'input_env': '未找到环境变量 {}，请输入',
        'call_failed': '调用失败',
        'think': '思考',
        'publish_disabled': "当前环境不支持发布",
        'auto_confirm': '自动确认',
        'packages_exist': '申请的第三方包已经安装',
        'thinking': '正在绞尽脑汁思考中，请稍等6-60秒',
        'no_available_llm': '没有可用的 LLM，请检查配置文件',
        'banner1_python': "请用 ai('任务') 输入需要 AI 处理的任务 (输入 ai.use(llm) 切换下述 LLM：",
        'banner1': "请输入需要 AI 处理的任务 (输入 /use <下述 LLM> 切换，输入 /info 查看系统信息)",
        'default': '默认',
        'enabled': '已启用',
        'disabled': '未启用',
        'ai_mode_enter': '进入 AI 模式，开始处理任务，输入 Ctrl+d 或 /done 结束任务',
        'ai_mode_exit': "[退出 AI 模式]",
        'ai_mode_unknown_command': "[AI 模式] 未知命令",
        'Task Summary': '任务总结',
        'Round': '轮次',
        'Time(s)': '时间(秒)',
        'In Tokens': '输入令牌数',
        'Out Tokens': '输出令牌数',
        'Total Tokens': '总令牌数',
        'sending_task': '正在向 {} 下达任务',
        'error_loading_config': "加载配置时出错: {}",
        'config_file_error': "请检查配置文件路径和格式。",
        'config_not_loaded': "配置尚未加载。",
        'llm_config_not_found': "缺少 'llm' 配置。",
        'not usable': "不可用",
        'permission_denied_error': "无权限创建目录: {}",
        'error_creating_config_dir': "创建配置目录时出错: {}",
        'migrate_config': "成功的将旧版本trustoken配置迁移到 {}",
        'migrate_user_config': "成功的将旧版本用户配置迁移到 {}",
        'binding_request_sent': "绑定请求发送成功。\n请求ID: {}\n\n>>> 请在已认证设备的浏览器中打开以下链接以批准:\n>>> {}\n\n(该链接将在{}秒后过期)",
        'scan_qr_code': "或者扫描下方二维码：",
        'config_help': "推荐您手机扫码绑定AiPy大脑，您也可以配置第三方大模型大脑，详情参考：https://d.aipy.app/d/77",
        'qr_code_display_failed': "(无法显示二维码: {})\n",
        'coordinator_request_error': "连接到协调服务器或请求时出错: {}",
        'unexpected_request_error': "请求过程中发生意外错误: {}",
        'waiting_for_approval': "浏览器已打开Trustoken网站，请注册或登录授权",
        'current_status': "当前状态: {}...",
        'binding_expired': "\n绑定请求已过期。",
        'unknown_status': "\n收到未知状态: {}",
        'coordinator_polling_error': "轮询协调服务器时出错: {}",
        'unexpected_polling_error': "轮询过程中发生意外错误: {}",
        'polling_cancelled': "\n用户取消了轮询。",
        'polling_timeout': "\n轮询超时。",
        'start_binding_process': "当前环境缺少必需的配置文件，开始进行配置初始化流程，与trustoken账号绑定即可自动获取配置...",
        'binding_success': "\n绑定流程已成功完成。",
        'binding_failed': "\n绑定流程失败或未完成。",
        'binding_request_failed': "\n绑定请求发起失败。",
        'cancel': "取消",
        'trust_token_auth': "TrustToken 认证",
        'Error': "错误",
        'Time remaining': "剩余时间：{} 秒",
        'approved': "已批准",
        'expired': "已过期",
        'pending': "待处理",
        'unknown_status': "收到未知状态: {}",
        'attempting_migration': '发现旧的配置文件: {}\n尝试从这些文件迁移配置...\n迁移之后，这些文件会被备份到 {}，请注意查看。',
        'task_saved': '结果文件已保存',
        'env_info': '当前配置文件目录：{} 工作目录: {}',
        'sys_info': '系统信息',
        'Save chat history as HTML': '保存聊天记录为 HTML',
        'Clear chat': '清空聊天',
        'Exit': '退出',
        'Start new task': '开始新任务',
        'Website': '官网',
        'Forum': '论坛',
        'WeChat Group': '微信群',
        'About': '关于',
        'Help': '帮助',
        'File': '文件',
        'Edit': '编辑',
        'Task': '任务',
        'Stop task': '停止任务',
        'Share task': '分享任务',
        'Current task has ended': '当前任务已结束',
        'Operation in progress, please wait...': '操作进行中，请稍候...',
        'Operation completed. If you want to start the next task, please click the "End" button': '操作完成。如果开始下一个任务，请点击"结束"按钮',
        'Press Ctrl+Enter to send message': '按 Ctrl+Enter 发送消息',
        'Save chat history as HTML file': '保存聊天记录为 HTML 文件',
        'Exit program': '退出程序',
        'Clear all messages': '清除所有消息',
        'Start a new task': '开始一个新任务',
        'Official website': '官方网站',
        'Official forum': '官方论坛',
        'Official WeChat group': '官方微信群',
        'About AIPY': '关于爱派',
        'Configuration': '配置',
        'Configure program parameters': '配置程序参数',
        'Work Directory': '工作目录',
        'Browse...': '浏览...',
        'Select work directory': '选择工作目录',
        'Max Tokens': '最大Token数',
        'Timeout (seconds)': '超时时间(秒)',
        'Max Rounds': '最大执行轮数',
        'OK': '确定',
        'Cancel': '取消',
        'AIPY is an intelligent assistant that can help you complete various tasks.': '爱派是一个智能助手，可以帮助您完成各种任务。',
        'Current configuration directory': '当前配置目录',
        'Current working directory': '当前工作目录',
        'Version': '版本',
        'AIPY Team': '爱派团队',
        'You can create a new directory in the file dialog': '您可以在文件对话框中选择或创建新目录',
        'Settings': '设置',
        'Open work directory': '打开工作目录',
        'Open work directory in file manager': '在文件管理器中打开工作目录',
        'requesting_binding': '正在申请绑定',
        'Update available': '发现新版本',
        "tt_base_url": "https://api.trustoken.cn/v1",
        "tt_coordinator_url": "https://www.trustoken.cn/api",
        "tt_share_url": "https://store.aipyaipy.com/api/work",
        'Work directory does not exist': '工作目录不存在',
        'Warning': '警告',
        'LLM {} is not available': 'LLM {} 不可用',
        'End': '结束',
        'Send': '发送',
        'AIPY - Your AI Assistant': '🐙爱派，您的干活牛🐂马🐎，啥都能干！',
        'API Market': 'API 市场',
        'LLM Configuration': 'LLM 配置',
        'Provider Configuration': '提供商配置',
        'Me': '我',
        'Turing': '图灵',
        'AIPy': '爱派',
        'Current task': '当前任务',
        'End processing instruction': '结束处理指令',
        'Run result': '运行结果',
        'Update available': '发现新版本',
        'Failed to save file': '无法保存文件',
        'Operation in progress, please wait...': '操作进行中，请稍候...',
        'Operation completed. If you start a new task, please click the "End" button': '操作完成。如果开始下一个任务，请点击"结束"按钮',
        'Press Ctrl+Enter to send message': '按 Ctrl/Cmd+Enter 发送消息',
        'welcome_message': '哈啰！我是**爱派**，您的智能任务小管家!\n请允许我隆重介绍团队其它成员：\n- 图灵：宇宙最强人工智能，复杂任务分析和规划\n- BB-8: 宇宙最强机器人，负责执行任务\n\n温馨提示：打开菜单栏"**帮助**"链接，可以联系**爱派**官方和加入群聊。',
        'Select LLM Provider': '选择 LLM 提供商',
        'Provider': '提供商',
        'Trustoken': 'Trustoken',
        'Other': '其它',
        'Trustoken is an intelligent API Key management service': 'Trustoken 是一个智能的 API Key 管理服务',
        'Auto get and manage API Key': '自动获取和管理 API Key',
        'Support multiple LLM providers': '支持多个 LLM 提供商',
        'Auto select optimal model': '自动选择最优模型',
        'Recommended for beginners, easy to configure and feature-rich': '建议小白用户选择，配置方便、功能丰富',
        'Select other providers requires manual configuration': '选择其它提供商需要手动配置',
        'Need to apply for API Key yourself': '需要自行申请 API Key',
        'Manually input API Key': '手动输入 API Key',
        'Manually select model': '手动选择模型',
        'Trustoken Configuration': 'Trustoken 配置',
        'API Key': 'API Key',
        'Auto open browser to get API Key': '自动打开浏览器获取 API Key',
        'Current status': '当前状态',
        'Binding expired': '绑定已过期',
        'Unknown status': '未知状态',
        'Waiting timeout': '等待超时',
        'API Key obtained successfully': 'API Key 获取成功！',
        'API Key acquisition failed, please try again': 'API Key 获取失败，请重试',
        'Requesting binding': '正在申请绑定',
        'Waiting for user confirmation': '等待用户确认',
        'Remaining time': '剩余时间',
        'seconds': '秒',
        'You can also click this link to open browser to get API Key': '你也可以点击此链接打开浏览器获取API Key',
        'Please get API Key first': '请先获取API Key',
        'Available Models': '可用模型',
        'Temperature (%)': '温度',
        'Please select provider and input API Key, click Next to verify': '请选择提供商并输入API Key，点击下一步验证',
        'Please select model to use and configure parameters': '请选择要使用的模型并配置参数',
        'Model list acquisition failed': '无法获取模型列表，请检查 API Key 是否正确',
        'Hint': '提示',
        'API Name': 'API名称',
        'Delete': '删除',
        'Confirm Delete': '确认删除',
        'Are you sure to delete API': '确定要删除API',
        'Add API': '新增API',
        'Edit API': '编辑API',
        'API Key Settings': 'API密钥设置',
        'Add Environment Variable': '添加环境变量',
        'API Description': 'API描述',
        'API Description Hint': '提示: 描述支持多行文本，将以"""..."""格式保存',
        'Save': '保存',
        'Variable Name': '变量名',
        'Value': '值',
        'Remove': '移除',
        'Add': '添加',
        'Refresh': '刷新',
        'No API configured': '未配置任何API',
        'API Details': 'API详情',
        'Close': '关闭',
        'API configuration saved': 'API配置已保存',
        'Failed to save API configuration': '保存API配置失败',
        'API configuration loaded': 'API配置已加载',
        'Failed to load API configuration': '加载API配置失败',
        'API deleted': 'API已删除',
        'Failed to delete API': '删除API失败',
        'API added': 'API已添加',
        'Failed to add API': '添加API失败',
        'API updated': 'API已更新',
        'Failed to update API': '更新API失败',
        'Support multiline text': '支持多行文本',
        'You can only add two environment variables at most': '最多只能添加两个环境变量',
        'API Market - Manage Your API Configurations': 'API市场 - 管理您的API配置',
        'Manage your API configurations here, including adding new APIs, viewing and editing existing ones.': '在此处管理您的API配置，包括添加新API、查看和编辑现有API。',
        'Add New API': '添加新API',
        'Refresh List': '刷新列表',
        'Number of Keys': '密钥数量',
        'Description': '描述',
        'Tip: Right-click on API item to view, edit and delete': '提示: 右键点击API项可进行查看、编辑和删除操作',
        'No API configured': '暂无API配置',
        'Click "Add New API" button to add API configuration': '点击"添加新API"按钮添加API配置',
        'View Details': '查看详情',
        'API name cannot be empty': 'API名称不能为空',
        'API name already exists': 'API名称已存在',
        'API configuration saved and applied': 'API配置已保存并应用',
        'Failed to save configuration': '保存配置失败',
        'Error': '错误',
        'Success': '成功',
        'API Details': 'API详情',
        'Environment Variables': '环境变量',
        'Variable Name': '变量名',
        'Value': '值',
        'API Description': 'API描述',
        'Support multiline text': '支持多行文本',
        'API Description Hint': '提示: 描述支持多行文本，将以"""..."""格式保存',
        'API Key Settings': 'API密钥设置',
        'Add Environment Variable': '添加环境变量',
        'Enter your API key': '填写您的API密钥',
        'API key description': 'API密钥描述',
        'You can only add two environment variables at most': '最多只能添加两个环境变量',
        'Hint': '提示',
        'Save and Apply': '保存并应用',
        'Cancel': '取消',
        'Close': '关闭',
        'Edit': '编辑',
        'Delete': '删除',
        'Remove': '移除',
        'Are you sure to delete API': '确定要删除API',
        'Confirm Delete': '确认删除',
        'Select other providers': '请选择其它提供商',
        'Model list acquisition failed': '无法获取模型列表，请检查 API Key 是否正确',
        'Available Models': '请选择模型',
        'Max Tokens': '请输入最大 Token 数（默认：8192）',
        'Temperature (%)': '请输入 Temperature（0-1，默认：0.7）',
        'Click here for more information': '点击这里获取更多信息',
        'LLM Provider Configuration Wizard': 'LLM 提供商配置向导',
        'Select Model': '请选择模型',
        'Trustoken uses automatic model selection': 'Trustoken 使用自动模型选择',
        'Configuration saved': '配置已保存',
        'Failed to save configuration': '保存配置失败',
        'Configuration loaded': '配置已加载',
        'Failed to load configuration': '加载配置失败',
        'Configuration applied': '配置已应用',
        'Failed to apply configuration': '应用配置失败',
        'Starting LLM Provider Configuration Wizard': '未找到 LLM 配置，开始配置 LLM 提供商',
        'User cancelled configuration': '用户取消配置',
        "https://aipy.app": "https://www.aipyaipy.com",
        "https://d.aipy.app": "https://d.aipyaipy.com",
        "https://d.aipy.app/d/13": "https://d.aipyaipy.com/d/13",
        'Share result': '分享结果',
        'Share failed': '分享失败',
        'Share success': '分享成功',
        'Click the link below to view the task record:': '点击下方链接查看任务记录',
        'View task record': '查看任务记录',
    },
    'en': {
        'start_instruction': 'Start processing instruction',
        'start_execute': 'Start executing code block',
        'execute_result': 'Execution result',
        'mcp_init': 'Initializing MCP server, this may take a while if it\'s the first load, please wait patiently...',
        'call_tool': 'Start calling MCP tool',
        'call_tool_result': 'MCP tool call result',
        'found_mcp': 'Found {1} tools from {0} enabled MCP servers',
        'mcp_info': "MCP Server: {}, tools: {}",
        'mcp_status': 'MCP server status: {}',
        'Start parsing message': 'Start parsing message',
        'Message parse result': 'Message parse result',
        'start_feedback': 'Start sending feedback',
        'end_instruction': 'End processing instruction',
        'no_context': 'No context information found',
        'sync_to_cloud': 'Uploading result, please wait...',
        'upload_success': 'Article uploaded successfully, {}',
        'upload_failed': 'Upload failed (status code: {})',
        'reset_warning': 'Severe warning: This will reinitialize❗❗❗',
        'reset_confirm': 'If you are sure to continue, enter y',
        'llm_response': 'Reply',
        'env_description': 'Environment variable name and meaning',
        'description': 'Description',
        'unknown_format': 'Unsupported file format',
        'ask_for_packages': 'Request to install third-party packages',
        'agree_packages': 'If you agree, please enter',
        'ask_for_env': 'Request to obtain environment variable {}, purpose',
        'env_exist': 'Environment variable {} exists, returned for code use',
        'input_env': 'Environment variable {} not found, please enter',
        'call_failed': 'Call failed',
        'think': 'Think',
        'publish_disabled': "Current environment does not support publishing",
        'auto_confirm': 'Auto confirm',
        'packages_exist': 'Third-party packages have been installed',
        'thinking': 'is thinking hard, please wait 6-60 seconds',
        'no_available_llm': 'No available LLM, please check the configuration file',
        'banner1_python': "Please use ai('task') to enter the task to be processed by AI (enter ai.use(llm) to switch to the following LLM:",
        'banner1': "Please enter the task to be processed by AI (enter /use <following LLM> to switch, enter /info to view system information)",
        'default': 'Default',
        'enabled': 'Enabled',
        'disabled': 'Disabled',
        'ai_mode_enter': 'Enter AI mode, start processing tasks, enter Ctrl+d or /done to end the task',
        'ai_mode_exit': "[Exit AI mode]",
        'ai_mode_unknown_command': "[AI mode] Unknown command",
        'Task Summary': 'Task Summary',
        'Round': 'Round',
        'Time(s)': 'Time(s)',
        'In Tokens': 'In Tokens',
        'Out Tokens': 'Out Tokens',
        'Total Tokens': 'Total Tokens',
        'sending_task': 'Sending task to {}',
        'error_loading_config': "Error loading configuration: {}",
        'config_file_error': "Please check the configuration file path and format.",
        'config_not_loaded': "Configuration not loaded.",
        'llm_config_not_found': "Missing 'llm' configuration.",
        'not usable': "Not usable",
        'permission_denied_error': "Permission denied to create directory: {}",
        'error_creating_config_dir': "Error creating configuration directory: {}",
        'migrate_config': "Successfully migrated old version trustoken configuration to {}",
        'migrate_user_config': "Successfully migrated old version user configuration to {}",
        'binding_request_sent': "Binding request sent successfully.\nRequest ID: {}\n\n>>> Please open this URL in your browser on an authenticated device to approve:\n>>> {}\n\n(This link expires in {} seconds)",
        'scan_qr_code': "Or scan the QR code below:",
        'config_help': "We recommend you scan the QR code to bind the AiPy brain, you can also configure a third-party large model brain, details refer to: https://d.aipy.app/d/77",
        'qr_code_display_failed': "(Could not display QR code: {})\n",
        'coordinator_request_error': "Error connecting to coordinator or during request: {}",
        'unexpected_request_error': "An unexpected error occurred during request: {}",
        'waiting_for_approval': "Browser has opened the Trustoken website, please register or login to authorize",
        'current_status': "Current status: {}...",
        'binding_expired': "\nBinding request expired.",
        'unknown_status': "\nUnknown status received: {}",
        'coordinator_polling_error': "Error connecting to coordinator during polling: {}",
        'unexpected_polling_error': "An unexpected error occurred during polling: {}",
        'polling_cancelled': "\nPolling cancelled by user.",
        'polling_timeout': "\nPolling timed out.",
        'start_binding_process': "The current environment lacks the required configuration file. Starting the configuration initialization process to bind with the Trustoken account...",
        'binding_success': "\nBinding process completed successfully.",
        'binding_failed': "\nBinding process failed or was not completed.",
        'binding_request_failed': "\nFailed to initiate binding request.",
        'cancel': "Cancel",
        'trust_token_auth': "TrustToken Authentication",
        'Error': "Error",
        'Time remaining': "Time remaining: {} seconds",
        'approved': "Approved",
        'expired': "Expired",
        'pending': "Pending",
        'unknown_status': "Received unknown status: {}",
        'attempting_migration': 'Found old configuration files: {}\nAttempting to migrate configuration from these files...\nAfter migration, these files will be backed up to {}, please check them.',
        'task_saved': 'Result file saved',
        'env_info': 'Current configuration file directory: {} Working directory: {}',
        'sys_info': 'System information',
        'Save chat history as HTML': 'Save chat history as HTML',
        'Clear chat': 'Clear chat',
        'Exit': 'Exit',
        'Start new task': 'Start new task',
        'Website': 'Website',
        'Forum': 'Forum',
        'WeChat Group': 'WeChat Group',
        'About': 'About',
        'Help': 'Help',
        'File': 'File',
        'Edit': 'Edit',
        'Task': 'Task',
        'Stop task': 'Stop task',
        'Share task': 'Share task',
        'Current task has ended': 'Current task has ended',
        'Operation in progress, please wait...': 'Operation in progress, please wait...',
        'Operation completed. If you want to start the next task, please click the "End" button': 'Operation completed. If you want to start the next task, please click the "End" button',
        'Press Ctrl+Enter to send message': 'Press Ctrl+Enter to send message',
        'Save chat history as HTML file': 'Save chat history as HTML file',
        'Exit program': 'Exit program',
        'Clear all messages': 'Clear all messages',
        'Start a new task': 'Start a new task',
        'Official website': 'Official website',
        'Official forum': 'Official forum',
        'Official WeChat group': 'Official WeChat group',
        'About AIPY': 'About AIPY',
        'Configuration': 'Configuration',
        'Configure program parameters': 'Configure program parameters',
        'Work Directory': 'Work Directory',
        'Browse...': 'Browse...',
        'Select work directory': 'Select work directory',
        'Max Tokens': 'Max Tokens',
        'Timeout (seconds)': 'Timeout (seconds)',
        'Max Rounds': 'Max Rounds',
        'OK': 'OK',
        'Cancel': 'Cancel',
        'AIPY is an intelligent assistant that can help you complete various tasks.': 'AIPY is an intelligent assistant that can help you complete various tasks.',
        'Current configuration directory': 'Current configuration directory',
        'Current working directory': 'Current working directory',
        'Version': 'Version',
        'AIPY Team': 'AIPY Team',
        'You can create a new directory in the file dialog': 'You can create a new directory in the file dialog',
        'Settings': 'Settings',
        "tt_base_url": "https://sapi.trustoken.ai/v1",
        "tt_coordinator_url": "https://www.trustoken.ai/api",
        "tt_share_url": "https://store.aipy.app/api/work",
        'Work directory does not exist': 'Work directory does not exist',
        'Warning': 'Warning',
        'LLM {} is not available': 'LLM {} is not available',
        'End': 'End',
        'Send': 'Send',
        'AIPY - Your AI Assistant': '🐙 AIPY - Your AI Assistant 🐂 🐎',
        'API Market': 'API Market',
        'LLM Configuration': 'LLM Configuration',
        'Provider Configuration': 'Provider Configuration',
        'Me': 'Me',
        'Turing': 'Turing',
        'AIPy': 'AIPy',
        'Current task': 'Current task',
        'End processing instruction': 'End processing instruction',
        'Run result': 'Run result',
        'Update available': 'Update available',
        'Failed to save file': 'Failed to save file',
        'Operation in progress, please wait...': 'Operation in progress, please wait...',
        'Operation completed. If you start a new task, please click the "End" button': 'Operation completed. If you start a new task, please click the "End" button',
        'Press Ctrl+Enter to send message': 'Press Ctrl/Cmd+Enter to send message',
        'welcome_message': 'Hello! I am **AIPy**, your intelligent task assistant!\nPlease allow me to introduce the other members of the team:\n- Turing: The strongest artificial intelligence, complex task analysis and planning\n- BB-8: The strongest robot, responsible for executing tasks\n\nNote: Click the "**Help**" link in the menu bar to contact the **AIPy** official and join the group chat.',
        'Select LLM Provider': 'Select LLM Provider',
        'Provider': 'Provider',
        'Trustoken': 'Trustoken',
        'Other': 'Other',
        'Trustoken is an intelligent API Key management service': 'Trustoken is an intelligent API Key management service',
        'Auto get and manage API Key': 'Auto get and manage API Key',
        'Support multiple LLM providers': 'Support multiple LLM providers',
        'Auto select optimal model': 'Auto select optimal model',
        'Recommended for beginners, easy to configure and feature-rich': 'Recommended for beginners, easy to configure and feature-rich',
        'Select other providers requires manual configuration': 'Select other providers requires manual configuration',
        'Need to apply for API Key yourself': 'Need to apply for API Key yourself',
        'Manually input API Key': 'Manually input API Key',
        'Manually select model': 'Manually select model',
        'Trustoken Configuration': 'Trustoken Configuration',
        'API Key': 'API Key',
        'Auto open browser to get API Key': 'Auto open browser to get API Key',
        'Current status': 'Current status',
        'Binding expired': 'Binding expired',
        'Unknown status': 'Unknown status',
        'Waiting timeout': 'Waiting timeout',
        'API Key obtained successfully': 'API Key obtained successfully!',
        'API Key acquisition failed, please try again': 'API Key acquisition failed, please try again',
        'Requesting binding': 'Requesting binding',
        'Waiting for user confirmation': 'Waiting for user confirmation',
        'Remaining time': 'Remaining time',
        'seconds': 'seconds',
        'You can also click this link to open browser to get API Key': 'You can also click this link to open browser to get API Key',
        'Please get API Key first': 'Please get API Key first',
        'Available Models': 'Available Models',
        'Temperature (%)': 'Temperature (%)',
        'Please select provider and input API Key, click Next to verify': 'Please select provider and input API Key, click Next to verify',
        'Please select model to use and configure parameters': 'Please select model to use and configure parameters',
        'Model list acquisition failed': 'Model list acquisition failed',
        'Hint': 'Hint',
        'API Name': 'API Name',
        'Delete': 'Delete',
        'Confirm Delete': 'Confirm Delete',
        'Are you sure to delete API': 'Are you sure to delete API',
        'Add API': 'Add API',
        'Edit API': 'Edit API',
        'API Key Settings': 'API Key Settings',
        'Add Environment Variable': 'Add Environment Variable',
        'API Description': 'API Description',
        'API Description Hint': 'Hint: Description supports multi-line text, will be saved in """...""" format',
        'Save': 'Save',
        'Variable Name': 'Variable Name',
        'Value': 'Value',
        'Remove': 'Remove',
        'Add': 'Add',
        'Refresh': 'Refresh',
        'No API configured': 'No API configured',
        'API Details': 'API Details',
        'Close': 'Close',
        'API configuration saved': 'API configuration saved',
        'Failed to save API configuration': 'Failed to save API configuration',
        'API configuration loaded': 'API configuration loaded',
        'Failed to load API configuration': 'Failed to load API configuration',
        'API deleted': 'API deleted',
        'Failed to delete API': 'Failed to delete API',
        'API added': 'API added',
        'Failed to add API': 'Failed to add API',
        'API updated': 'API updated',
        'Failed to update API': 'Failed to update API',
        'Support multiline text': 'Support multiline text',
        'You can only add two environment variables at most': 'You can only add two environment variables at most',
        'API Market - Manage Your API Configurations': 'API Market - Manage Your API Configurations',
        'Manage your API configurations here, including adding new APIs, viewing and editing existing ones.': 'Manage your API configurations here, including adding new APIs, viewing and editing existing ones.',
        'Add New API': 'Add New API',
        'Refresh List': 'Refresh List',
        'Number of Keys': 'Number of Keys',
        'Description': 'Description',
        'Tip: Right-click on API item to view, edit and delete': 'Tip: Right-click on API item to view, edit and delete',
        'No API configured': 'No API configured',
        'Click "Add New API" button to add API configuration': 'Click "Add New API" button to add API configuration',
        'View Details': 'View Details',
        'API name cannot be empty': 'API name cannot be empty',
        'API name already exists': 'API name already exists',
        'API configuration saved and applied': 'API configuration saved and applied',
        'Failed to save configuration': 'Failed to save configuration',
        'Error': 'Error',
        'Success': 'Success',
        'API Details': 'API Details',
        'Environment Variables': 'Environment Variables',
        'Variable Name': 'Variable Name',
        'Value': 'Value',
        'API Description': 'API Description',
        'Support multiline text': 'Support multiline text',
        'API Description Hint': 'Hint: Description supports multi-line text, will be saved in """...""" format',
        'API Key Settings': 'API Key Settings',
        'Add Environment Variable': 'Add Environment Variable',
        'Enter your API key': 'Enter your API key',
        'API key description': 'API key description',
        'You can only add two environment variables at most': 'You can only add two environment variables at most',
        'Hint': 'Hint',
        'Save and Apply': 'Save and Apply',
        'Cancel': 'Cancel',
        'Close': 'Close',
        'Edit': 'Edit',
        'Delete': 'Delete',
        'Remove': 'Remove',
        'Are you sure to delete API': 'Are you sure to delete API',
        'Confirm Delete': 'Confirm Delete',
        'Click here for more information': 'Click here for more information',
        'LLM Provider Configuration Wizard': 'LLM Provider Configuration Wizard',
        'Select Model': 'Select Model',
        'Trustoken uses automatic model selection': 'Trustoken uses automatic model selection',
        'Configuration saved': 'Configuration saved',
        'Failed to save configuration': 'Failed to save configuration',
        'Configuration loaded': 'Configuration loaded',
        'Failed to load configuration': 'Failed to load configuration',
        'Configuration applied': 'Configuration applied',
        'Failed to apply configuration': 'Failed to apply configuration',
        'Select other providers': 'Select other providers',
        'Starting LLM Provider Configuration Wizard': 'Starting LLM Provider Configuration Wizard',
        'User cancelled configuration': 'User cancelled configuration',
        "https://aipy.app": "https://aipy.app",
        "https://d.aipy.app": "https://d.aipy.app",
        "https://d.aipy.app/d/13": "https://d.aipy.app/d/13",        
        'Share result': 'Share result',
        'Share failed': 'Share failed',
        'Share success': 'Share success',
        'Click the link below to view the task record:': 'Click the link below to view the task record:',
        'View task record': 'View task record',
    }
}

__lang__ = 'en'

def get_lang():
    return __lang__

def set_lang(lang=None):
    global __lang__, MESSAGES
    if lang and lang in MESSAGES:
        __lang__ = lang
        return
    
    language, _ = locale.getlocale()
    if language:
        language = language.lower()
        if language.find('china') >=0 or language.find('chinese') >= 0 or language.find('zh_') >= 0:
            __lang__ = 'zh'

def T(key, *args):
    msg = MESSAGES[__lang__][key]
    return msg.format(*args) if args else msg

def get_system_language() -> str:
    """
    获取当前运行环境的语言代码 (例如 'en', 'zh')。
    支持 Windows, macOS, Linux。
    返回小写的语言代码，如果无法确定则返回 'en'。
    """
    language_code = 'en' # 默认英语

    try:
        if platform.system() == "Windows":
            # Windows: 使用 GetUserDefaultUILanguage 或 GetSystemDefaultUILanguage
            # https://learn.microsoft.com/en-us/windows/win32/intl/language-identifiers
            windll = ctypes.windll.kernel32
            # GetUserDefaultUILanguage 返回当前用户的UI语言ID
            lang_id = windll.GetUserDefaultUILanguage()
            # 将语言ID转换为标准语言代码 (例如 1033 -> en, 2052 -> zh)
            # 主要语言ID在低10位
            primary_lang_id = lang_id & 0x3FF
            if primary_lang_id == 0x04: # zh - Chinese
                language_code = 'zh'
            elif primary_lang_id == 0x09: # en - English
                language_code = 'en'
            # 可以根据需要添加更多语言ID映射
            # 参考: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/a9eac961-e77d-41a6-90a5-ce1a8b0cdb9c

        elif platform.system() == "Darwin": # macOS
            # macOS: 优先使用 locale.getlocale()
            language, encoding = locale.getlocale()
            if language:
                language_code = language.split('_')[0].lower()
            else:
                # 备选方案: 读取环境变量
                lang_env = os.environ.get('LANG')
                if lang_env:
                    language_code = lang_env.split('_')[0].lower()

        else: # Linux/Unix
            # Linux/Unix: 优先使用 locale.getlocale()
            language, encoding = locale.getlocale()
            if language:
                language_code = language.split('_')[0].lower()
            else:
                # 备选方案: 读取环境变量 LANG 或 LANGUAGE
                lang_env = os.environ.get('LANG') or os.environ.get('LANGUAGE')
                if lang_env:
                    language_code = lang_env.split('_')[0].lower()

        # 规范化常见的中文代码
        if language_code.startswith('zh'):
            language_code = 'zh'

    except Exception:
        # 如果发生任何错误，回退到默认值 'en'
        pass # 使用默认的 'en'

    return language_code

set_lang()
