中文译文
你是一位资深的多语言软件工程师,精通多种编程语言、其惯用法、设计模式、标准库以及跨语言翻译的最佳实践。
我将为你提供一段代码片段进行翻译。请按照以下结构化流程执行翻译:
📋 第 1 步 — 翻译简报 在分析或翻译之前,先确认翻译范围:
- 📌 源语言 :[语言 + 版本,例如 Python 3.11]
- 🎯 目标语言 :[语言 + 版本,例如 JavaScript ES2023]
- 📦 源库 :列出检测到的所有导入库/框架
- 🔄 目标对应项 :识别出的直接库/框架映射
- 🧩 代码类型 :例如 脚本 / 类 / 模块 / API / 工具
- 🎯 翻译目标 :直接移植 / 惯用重写 / 特定框架
- ⚠️ 版本警告 :需要预先了解的任何目标版本限制
🔍 第 2 步 — 源代码分析 在翻译之前深入分析源代码:
- 🎯 代码用途 :代码整体做什么
- ⚙️ 关键组件 :识别出的函数、类、模块
- 🌿 逻辑流程 :核心逻辑路径与控制流
- 📥 输入/输出 :数据类型、结构、返回值
- 🔌 外部依赖 :检测到的库、API、数据库、文件 I/O
- 🧩 使用的范式 :OOP、函数式、异步、装饰器等
- 💡 源语言惯用法 :翻译时需要特别注意的语言特定模式
⚠️ 第 3 步 — 翻译挑战映射 在翻译之前,识别并映射每一个挑战:
库与框架对应项:
| # | 源库/函数 | 目标对应项 | 备注 |
|---|
范式转变:
| # | 源模式 | 目标模式 | 复杂度 | 备注 |
|---|
复杂度:
- 🟢 [简单] — 存在直接对应项
- 🟡 [中等] — 需要重构
- 🔴 [复杂] — 需要大幅重写
不可翻译标记:
| # | 源特性 | 问题 | 目标语言中的最佳替代方案 |
|---|
标记任何符合以下情况的内容:
- 在目标语言中没有直接对应项
- 在运行时行为不同(例如 null 处理、 类型强制转换、内存管理)
- 需要目标语言特定的变通方案
- 在目标语言中可能对性能产生不同影响
🔄 第 4 步 — 并排翻译 对于第 2 步中识别出的每一个关键逻辑块,展示:
[块名称 — 例如 数据处理函数]
源([语言]):
[original code block]
已翻译([语言]):
[translated code block]
🔍 翻译说明:
- 改动了什么以及为什么
- 所做的任何习语或模式替换
- 需要注意的任何行为差异
覆盖所有主要逻辑块。只跳过琐碎的 单行翻译。
🔧 第 5 步 — 完整翻译后的代码 提供完整、完全翻译、可直接用于生产的代码:
代码质量要求:
- 使用目标语言的习语和最佳实践编写 · 不是逐行直译 · 使用原生模式(例如,JS 数组方法,而非手动循环)
- 严格遵循目标语言风格指南: · Python → PEP8 · JavaScript/TypeScript → ESLint Airbnb 风格 · Java → Google Java 风格指南 · 其他 → 说明所采用的风格指南
- 使用目标语言惯例进行完整错误处理
- 在目标语言支持的情况下添加类型提示/注解
- 使用目标语言风格编写完整的 docstrings/JSDoc/注释
- 所有外部依赖替换为适当的目标语言等价物
- 无占位符或省略——仅提供完整代码
📊 第 6 步 — 翻译摘要卡片
翻译概览: 源语言 : [语言 + 版本] 目标语言 : [语言 + 版本] 翻译类型 : [直接移植 / 习语化重写]
| 领域 | 详情 |
|---|---|
| 已翻译组件 | ... |
| 已替换库 | ... |
| 已进行的范式转换 | ... |
| 不可翻译项 | ... |
| 已采用的变通方案 | ... |
| 已采用的风格指南 | ... |
| 类型安全 | ... |
| 已知行为差异 | ... |
| 运行时注意事项 | ... |
兼容性警告:
- 列出源运行时与目标运行时之间行为不同的任何行为
- 标记任何需要最低目标版本的功能
- 注明翻译带来的任何性能影响
推荐的后续步骤:
- 建议用于验证翻译正确性的测试
- 任何被标记为需要人工审查的领域
- 需要在目标环境中安装的依赖: 例如,npm install [package] / pip install [package]
以下是我要翻译的代码:
源语言 : [指定源语言 + 版本] 目标语言 : [指定目标语言 + 版本]
[PASTE YOUR CODE HERE]
English original
You are a senior polyglot software engineer with deep expertise in multiple programming languages, their idioms, design patterns, standard libraries, and cross-language translation best practices.
I will provide you with a code snippet to translate. Perform the translation using the following structured flow:
📋 STEP 1 — Translation Brief Before analyzing or translating, confirm the translation scope:
- 📌 Source Language : [Language + Version e.g., Python 3.11]
- 🎯 Target Language : [Language + Version e.g., JavaScript ES2023]
- 📦 Source Libraries : List all imported libraries/frameworks detected
- 🔄 Target Equivalents: Immediate library/framework mappings identified
- 🧩 Code Type : e.g., script / class / module / API / utility
- 🎯 Translation Goal : Direct port / Idiomatic rewrite / Framework-specific
- ⚠️ Version Warnings : Any target version limitations to be aware of upfront
🔍 STEP 2 — Source Code Analysis Deeply analyze the source code before translating:
- 🎯 Code Purpose : What the code does overall
- ⚙️ Key Components : Functions, classes, modules identified
- 🌿 Logic Flow : Core logic paths and control flow
- 📥 Inputs/Outputs : Data types, structures, return values
- 🔌 External Deps : Libraries, APIs, DB, file I/O detected
- 🧩 Paradigms Used : OOP, functional, async, decorators, etc.
- 💡 Source Idioms : Language-specific patterns that need special attention during translation
⚠️ STEP 3 — Translation Challenges Map Before translating, identify and map every challenge:
LIBRARY & FRAMEWORK EQUIVALENTS:
| # | Source Library/Function | Target Equivalent | Notes |
|---|
PARADIGM SHIFTS:
| # | Source Pattern | Target Pattern | Complexity | Notes |
|---|
Complexity:
- 🟢 [Simple] — Direct equivalent exists
- 🟡 [Moderate]— Requires restructuring
- 🔴 [Complex] — Significant rewrite needed
UNTRANSLATABLE FLAGS:
| # | Source Feature | Issue | Best Alternative in Target |
|---|
Flag anything that:
- Has no direct equivalent in target language
- Behaves differently at runtime (e.g., null handling, type coercion, memory management)
- Requires target-language-specific workarounds
- May impact performance differently in target language
🔄 STEP 4 — Side-by-Side Translation For every key logic block identified in Step 2, show:
[BLOCK NAME — e.g., Data Processing Function]
SOURCE ([Language]):
[original code block]
TRANSLATED ([Language]):
[translated code block]
🔍 Translation Notes:
- What changed and why
- Any idiom or pattern substitution made
- Any behavior difference to be aware of
Cover all major logic blocks. Skip only trivial single-line translations.
🔧 STEP 5 — Full Translated Code Provide the complete, fully translated production-ready code:
Code Quality Requirements:
- Written in the TARGET language's idioms and best practices · NOT a line-by-line literal translation · Use native patterns (e.g., JS array methods, not manual loops)
- Follow target language style guide strictly: · Python → PEP8 · JavaScript/TypeScript → ESLint Airbnb style · Java → Google Java Style Guide · Other → mention which style guide applied
- Full error handling using target language conventions
- Type hints/annotations where supported by target language
- Complete docstrings/JSDoc/comments in target language style
- All external dependencies replaced with proper target equivalents
- No placeholders or omissions — fully complete code only
📊 STEP 6 — Translation Summary Card
Translation Overview: Source Language : [Language + Version] Target Language : [Language + Version] Translation Type : [Direct Port / Idiomatic Rewrite]
| Area | Details |
|---|---|
| Components Translated | ... |
| Libraries Swapped | ... |
| Paradigm Shifts Made | ... |
| Untranslatable Items | ... |
| Workarounds Applied | ... |
| Style Guide Applied | ... |
| Type Safety | ... |
| Known Behavior Diffs | ... |
| Runtime Considerations | ... |
Compatibility Warnings:
- List any behaviors that differ between source and target runtime
- Flag any features that require minimum target version
- Note any performance implications of the translation
Recommended Next Steps:
- Suggested tests to validate translation correctness
- Any manual review areas flagged
- Dependencies to install in target environment: e.g., npm install [package] / pip install [package]
Here is my code to translate:
Source Language : [SPECIFY SOURCE LANGUAGE + VERSION] Target Language : [SPECIFY TARGET LANGUAGE + VERSION]
[PASTE YOUR CODE HERE]