Code Translator (Lang → Lang): Preserve Behavior, Adopt Idioms
A line-by-line translation is the worst of both worlds: it doesn't read like the source language anymore and it doesn't read like the target either. This AI tool translates idiomatically — semantics preserved, conventions adopted, and the places they actually diverge clearly called out.
Translate code from one language to another while preserving behavior, adopting idioms of the target language, and flagging any place the two languages don't map cleanly. TRANSLATION METHODOLOGY (follow in order): 1. Understand the Source Goal: Make sure the translation preserves semantics, not just syntax. - Identify the source language and any library or framework features used. - Note any language-specific behaviors that matter (async model, error handling, immutability, generics). 2. Translate to Idiomatic Target - Use the target language's natural idioms — not a line-by-line transcription. - Adopt the target language's conventions for naming, error handling, async, and modularity. - Replace source-language stdlib calls with the target's equivalents, or note when no equivalent exists. 3. Map the Type System - Translate types accurately — including null/undefined semantics, generics, and collection types. - Flag any type construct that can't be expressed cleanly in the target. 4. Preserve Side Effects - Maintain the same side effects (I/O, mutations, network calls) — don't silently reorder or merge them. - If a side-effect pattern is unidiomatic in the target, note it before changing. 5. Test Parity - For non-trivial functions, list 2-3 example inputs and the expected output. - Confirm the translated code returns the same result. 6. Note the Gotchas - Anything the translation cannot preserve perfectly: integer overflow, floating-point handling, threading model, regex flavors. - Suggest a workaround or accepted trade-off for each. OUTPUT CONSTRAINTS: - Return the translated code in a single code block. - Use the target language's standard formatting (rustfmt, prettier, gofmt, etc.). - Don't paste the original code back. - Always include the gotchas section, even if empty. --- MY INFO: Source Language (required): Target Language (required): Code to Translate (required): Target Runtime / Version (optional): [e.g. Node 22, Python 3.12, Go 1.22] Behavior to Preserve Exactly (optional): [edge cases, perf characteristics]
What You Get
- Idiomatic target code — not a literal transcription
- Preserved semantics with side effects intact
- A mapped type system with gaps flagged where types don't translate
- A gotchas section for what the translation can't preserve perfectly
Why It Works
The prompt distinguishes syntactic translation from semantic translation. Async models, null/undefined semantics, integer overflow, regex flavors, threading — these are where naive translations introduce bugs. The output adopts the target language's idioms (naming, error handling, modularity) so the code looks like it belongs.
Best Practices
- Name the runtime: Node 22, Python 3.12, Go 1.22 — versions affect translation choices.
- Provide tests if you have them: They prove parity rather than promise it.
- State what must be preserved: Edge cases, perf characteristics, side effects.
- Read the gotchas: They're usually small but always important.
Port the logic. Adopt the idioms. Know what didn't carry over.