Automated Python Translation

Joshua Otten, Antonios Anastasopoulos, Kevin MoranarXiv 2025 · DOI

Abstract

Programming languages like Python are often described as "executable pseudo-code," but this readability is largely restricted to English speakers. Keywords and standard library functions—the "natural modality" of the language—act as arbitrary strings for non-English speakers, increasing cognitive load and creating a barrier to entry. This paper introduces an automated pipeline designed to translate Python's core syntax into multiple human languages. By combining LLM-based term expansion with neural machine translation, the researchers propose a "Universal Python" framework. This approach significantly lowers the cognitive overhead for non-English speaking developers by localizing the language itself, achieving high accuracy across diverse language families.

Introduction: The Language Barrier in Programming

Python's popularity stems from its intuitive, English-like syntax. However, for a student in Greece or Bangladesh, keywords like yield, except, or elif are not intuitive concepts; they are arbitrary English tokens that must be memorized. While existing tools like CodeInternational focus on localizing programmer-written comments and variable names, the core "natural modality" of the language remains stubbornly English-centric. The authors address this gap by automating the translation of built-in keywords and standard library functions (e.g., abs(), print(), dataframe) into languages including Spanish, Mandarin, Greek, and Arabic.

Methodology: The Three-Stage Pipeline

The researchers developed a robust three-stage pipeline to overcome the limitations of standard translation tools, which often fail on technical abbreviations. For instance, without context, Google Translate has been known to interpret the mathematical function abs as "abdominal muscles."

  1. Python Term Expansion: Cryptic abbreviations (e.g., abs, delattr) are expanded into full English phrases (e.g., "absolute value", "delete attribute") using GPT-4 with few-shot prompting. This "un-coding" step is critical for accurate downstream translation.
  2. Python Term Translation: These expanded phrases are translated into the target language. The study compared GPT-4, Llama2, and Google Translate, finding that while LLMs excel at expansion, traditional translation engines often provide more consistent results for the translation step itself.
  3. Python Term Abbreviation: To maintain code conciseness, a rule-based system optionally abbreviates the translated terms based on syllable structure.
The proposed pipeline for term translation and the resulting Universal Python Framework.

The pipeline feeds into a Universal Python Framework, which utilizes Neural Machine Translation and multilingual compilation to ensure that code written in Mandarin or French produces the same execution output as the original English version.

Experimental Setup and Datasets

The study benchmarked the pipeline using a comprehensive dataset:

  • Standard and Major Libraries: Over 6,300 unique terms extracted from the Python Standard Library and popular packages like pytorch, pandas, tensorflow, and numpy.
  • Multilingual Scope: Eight diverse languages, ranging from high-resource (Spanish, French) to lower-resource (Sorani Kurdish, Bengali, Hindi).
  • Fine-tuning: A Llama2-7b model was fine-tuned on over 32,000 code examples from the codeparrot/github-code dataset to attempt direct block-level translation.

Key Results and Performance

The most significant finding was the necessity of the expansion phase. GPT-4 proved highly effective at this task, reaching a peak accuracy of 93.2% when provided with five examples.

Expansion accuracy of Python terms using different prompting strategies with GPT-4.

In downstream translation tasks, the pipeline showed impressive results across diverse scripts. For example, Bengali translations achieved a raw accuracy of 82.1% and a character-level F-score (chrF) of 90.8%.

Interestingly, the researchers discovered a "Context Paradox": providing translation engines with extra context (like definitions or example sentences) often caused them to perform worse. The models would frequently identify the Python-specific context and decide to leave the term in English, assuming it was a technical requirement. Consequently, simple translation of expanded terms proved most effective.

Discussion and Future Directions

While the automated pipeline represents a major step toward a "Universal Python," several challenges remain. The current syllable-based abbreviation system is optimized for alphabetic scripts and requires adaptation for logographic systems like Mandarin. Furthermore, while the pipeline generates high-quality translations, full interpreter support is required to execute this localized code natively.

The authors emphasize that while automation significantly reduces the manual effort required to localize a programming language, native speakers remain essential for final validation and community adoption. Future work will focus on resolving grammatical ambiguities—such as distinguishing "print" as a noun versus a verb—and expanding support for more low-resource languages to truly democratize the art of programming.

Summary updated

BibTeX
@misc{otten2025automated,
  author = {Joshua Otten and Antonios Anastasopoulos and Kevin Moran},
  title = {{Automated Python Translation}},
  year = {2025},
  eprint = {2504.11290},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2504.11290},
}
Figure