DEV Community

Cover image for # Opened a Repo in Another Language — Here’s What Saved Me
Bhagyesh Patil
Bhagyesh Patil

Posted on

# Opened a Repo in Another Language — Here’s What Saved Me

> Ever opened a GitHub repo and found comments in a language you don't speak? Here's how I solved it in a weekend.


The Problem Nobody Talks About

You find a promising open-source library. The code looks clean. But then you see this:

# 这个函数计算总价格,请勿修改!
def calculate_total(items):
    # 临时修复:不要触碰此逻辑
    total = 0
    for item in items:
        total += item.price
    return total
Enter fullscreen mode Exit fullscreen mode

🤔 "Is this important? Should I touch this? What does that warning say?"

Google Translate gives you: "This function calculates the total price, please do not modify!"

But wait—what about that second comment? The one that says "Temporary fix: Do not touch this logic"?

That's a landmine. And you almost stepped on it.


Why Existing Solutions Suck

Method Problem
Google Translate Loses technical context, breaks formatting
Copy-paste Tedious, comments scattered everywhere
Ask a colleague Not always available, awkward
Just guess 💀 RIP production

I needed something that:

  • ✅ Translates only the comments
  • ✅ Leaves code 100% untouched
  • ✅ Flags dangerous/critical comments
  • ✅ Works with any programming language

So I built it.


Introducing: CodeComments Translator

A tool that does exactly one thing really well: translates code comments while preserving your code structure.

How It Works

  1. Paste your code with comments in any language
  2. Select your target language (English, Spanish, French, Hindi, etc.)
  3. Click Translate
  4. Get your code back with translated comments—code untouched

The Secret Sauce: Risk Detection 🔴

Not all comments are created equal. Some are just explanations. Others are warnings that could save you hours of debugging.

My translator doesn't just translate—it analyzes the comment's intent:

# Before (Chinese)
# 临时修复:密码硬编码,稍后修改

# After (English)  
# 🔴 [CRITICAL] Temporary fix: password hardcoded, modify later
Enter fullscreen mode Exit fullscreen mode

See that red badge? That's your "DON'T TOUCH THIS" warning.

Risk Levels:

  • 🔴 [CRITICAL] — Security issues, hardcoded secrets, "do not modify"
  • ⚠️ [CAUTION] — TODOs, workarounds, deprecated code
  • 🟢 Normal — Regular explanations

The Tech Stack

Here's what's under the hood:

Frontend:     Next.js 14 + React 18 + TypeScript
Styling:      Tailwind CSS + Framer Motion
Translation:  Lingo.dev API (primary) + Gemini (fallback)
Highlighting: Prism.js
Enter fullscreen mode Exit fullscreen mode

Why Lingo.dev?

I tested multiple translation APIs. Here's why Lingo.dev won:

  1. Context-aware — Understands technical terms, doesn't butcher them
  2. SDK is dead simple — Install, configure, translate. Done.
  3. Affordable — 10,000 words/month free tier
import { LingoDotDevEngine } from "lingo.dev/sdk";

const lingoDotDev = new LingoDotDevEngine({
  apiKey: process.env.LINGODOTDEV_API_KEY,
});

const translated = await lingoDotDev.localizeText(commentText, {
  sourceLocale: "zh",
  targetLocale: "en",
});
Enter fullscreen mode Exit fullscreen mode

That's it. Four lines to translate any text.


Building the Comment Detector

The trickiest part? Extracting comments from code without breaking anything.

Different languages have different comment styles:

Language Single-line Multi-line
Python # """
JavaScript // /* */
SQL -- /* */
Ruby # =begin =end

My regex-based detector handles them all:

const COMMENT_PATTERNS = [
  { lang: 'python', pattern: /#(.*)$/gm },
  { lang: 'javascript', pattern: /\/\/(.*)$/gm },
  { lang: 'javascript', pattern: /\/\*([\s\S]*?)\*\//gm },
  { lang: 'sql', pattern: /--(.*)$/gm },
];
Enter fullscreen mode Exit fullscreen mode

The key insight: extract the comment, translate it, put it back in the exact same position.


The Translate Flow

Here's the full architecture:

┌─────────────────────────────────────────────────────────┐
│                     User's Code                         │
│   def foo():                                            │
│       # 这是一个临时函数                                  │
│       pass                                              │
└────────────────────────┬────────────────────────────────┘
                         │
                         ▼
┌─────────────────────────────────────────────────────────┐
│               Comment Detector                          │
│   Extracts: "这是一个临时函数"                            │
│   Position: Line 2, after "# "                          │
└────────────────────────┬────────────────────────────────┘
                         │
                         ▼
┌─────────────────────────────────────────────────────────┐
│                 Lingo.dev API                           │
│   Input:  "这是一个临时函数"                              │
│   Output: "This is a temporary function"                │
└────────────────────────┬────────────────────────────────┘
                         │
                         ▼
┌─────────────────────────────────────────────────────────┐
│               Risk Analyzer                             │
│   Keyword Match: "temporary" → ⚠️ CAUTION               │
└────────────────────────┬────────────────────────────────┘
                         │
                         ▼
┌─────────────────────────────────────────────────────────┐
│                  Final Output                           │
│   def foo():                                            │
│       # ⚠️ [CAUTION] This is a temporary function       │
│       pass                                              │
└─────────────────────────────────────────────────────────┘
Enter fullscreen mode Exit fullscreen mode

Real-World Example

Here's actual code from a Chinese e-commerce repo:

Before:

// 不要删除这段代码!支付系统依赖它
function processPayment(order) {
    // 临时方案:硬编码折扣率
    const discount = 0.15; // TODO: 从数据库读取

    // 警告:此函数不处理退款
    return order.total * (1 - discount);
}
Enter fullscreen mode Exit fullscreen mode

After:

// 🔴 [CRITICAL] Do not delete this code! Payment system depends on it
function processPayment(order) {
    // ⚠️ [CAUTION] Temporary solution: hardcoded discount rate
    const discount = 0.15; // ⚠️ [CAUTION] TODO: Read from database

    // ⚠️ [CAUTION] Warning: This function does not handle refunds
    return order.total * (1 - discount);
}
Enter fullscreen mode Exit fullscreen mode

Now you know what you're dealing with before touching anything.


Try It Yourself

Quick Start

# Clone the repo
git clone https://github.com/BhagyeshPatil2004/CodeComments-Translator.git

# Install dependencies
npm install

# Set up your API key
echo "LINGO_API_KEY=your_key_here" > .env

# Run it
npm run dev
Enter fullscreen mode Exit fullscreen mode

Open http://localhost:3000 and start translating!


📸 Demo


Key Takeaways

  1. Don't guess what foreign comments mean — especially warnings
  2. Lingo.dev makes translation stupid simple — SDK is 4 lines of code
  3. Risk detection saves lives (okay, maybe just saves debugging time)
  4. Preserve code structure — never modify what you don't understand

Links


Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.