You need to enable JavaScript to run this app. 寻求CLI工具实现英文静态着陆页多语言批量翻译生成
优惠活动
大模型
产品
解决方案
定价
更多

寻求CLI工具实现英文静态着陆页多语言批量翻译生成

CLI Tools to Translate Static HTML Landing Pages

Hey, let's tackle your need for a CLI tool to turn your home.en.html into localized versions for different languages—using simple commands like the ones you mentioned. Here are three practical solutions that fit the bill:

1. Quick & Dirty: translate-shell + Shell Script Wrap

translate-shell is a popular CLI translation tool that works with Google/Bing Translate under the hood. It's built for plain text, but we can wrap it in a tiny shell script to safely translate HTML content (keeping tags intact, only translating the actual text).

How to set it up:

  • Install translate-shell:

    • macOS: brew install translate-shell
    • Ubuntu/Debian: sudo apt install translate-shell
    • For other systems, grab the source from GitHub and compile it.
  • Create a shell script (name it translate-html.sh):

    #!/bin/bash
    if [ $# -ne 3 ]; then
        echo "Usage: $0 <source-html> <target-lang-code> <output-html>"
        exit 1
    fi
    # Translate the HTML file, skip ANSI colors, output raw text
    trans -b -no-ansi -t $2 < $1 > $3
    
  • Make it executable:

    chmod +x translate-html.sh
    
  • Use it like this:

    ./translate-html.sh home.en.html de home.de.html
    ./translate-html.sh home.en.html fr home.fr.html
    

    Pro tip: trans usually does a good job of ignoring HTML tags and only translating the text inside them. If you run into edge cases, you can tweak the command with additional flags (check trans --help for details).

2. HTML-Focused: html-translate (Node.js-Based)

If you need more control over what gets translated (like skipping certain elements or attributes), html-translate is a purpose-built CLI tool for HTML localization.

Setup steps:

  • Install Node.js first, then install the tool globally:

    npm install -g html-translate
    
  • Basic usage (exactly matches your desired command flow):

    html-translate --source home.en.html --target de --output home.de.html
    html-translate --source home.en.html --target fr --output home.fr.html
    
  • Advanced control (optional): Create a translate-config.json file to ignore specific elements or attributes:

    {
      "ignoreSelectors": [".brand-logo", "[data-notranslate]"],
      "ignoreAttributes": ["class", "id", "src"]
    }
    

    Then run the command with the config:

    html-translate --source home.en.html --target de --output home.de.html --config translate-config.json
    

3. Fully Customizable: Python Script with BeautifulSoup

If you want full control over the translation process (like using your own translation API key to avoid rate limits), a custom Python script is the way to go. This uses BeautifulSoup to parse HTML and googletrans for translation.

How to build it:

  • Install dependencies:

    pip install beautifulsoup4 googletrans==4.0.0-rc1
    
  • Write the script (name it translate-html.py):

    import sys
    from bs4 import BeautifulSoup
    from googletrans import Translator
    
    def translate_html(source_path, target_lang, output_path):
        translator = Translator()
        # Read and parse the source HTML
        with open(source_path, "r", encoding="utf-8") as f:
            soup = BeautifulSoup(f.read(), "html.parser")
        
        # Translate all text nodes except script/style/meta tags
        for text_node in soup.find_all(text=True):
            parent_tag = text_node.parent.name
            if parent_tag not in ["script", "style", "meta", "link"]:
                if text_node.strip():  # Skip empty text nodes
                    translated_text = translator.translate(text_node.strip(), dest=target_lang).text
                    text_node.replace_with(translated_text)
        
        # Write the translated HTML to output file
        with open(output_path, "w", encoding="utf-8") as f:
            f.write(str(soup))
    
    if __name__ == "__main__":
        if len(sys.argv) != 4:
            print("Usage: python translate-html.py <source-html> <target-lang> <output-html>")
            sys.exit(1)
        translate_html(sys.argv[1], sys.argv[2], sys.argv[3])
    
  • Run it:

    python translate-html.py home.en.html de home.de.html
    python translate-html.py home.en.html fr home.fr.html
    

    Note: If you hit rate limits with the free googletrans API, you can switch to Google Cloud's official Translation API for more reliability (you'll need an API key and the google-cloud-translate package).

Pick the solution that best fits your needs—option 1 is great for quick wins, option 2 for HTML-specific control, and option 3 for full customization.

内容的提问来源于stack exchange,提问作者user2959760

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:04:28