Vishal Tyagi
← Projects
Automation·shipped

HTML Content Translator

Python utility that walks an HTML DOM with BeautifulSoup, translates visible text nodes (EN→HI), and leaves tags, attributes, and script/style alone.

Date2023-03
Reading TimeN/A
Statusshipped
StackPython, BeautifulSoup

The trap

Treating HTML as a string and running a translator over it breaks tags, attributes, and scripts. You have to parse a DOM and touch text nodes only.

soup = BeautifulSoup(open(input_file, encoding='utf-8'), 'html.parser')

for tag in soup.find_all(string=True):
    if tag.parent.name in ['script', 'style']:
        continue
    english_text = tag.text.strip()
    if english_text:
        tag.replace_with(translate(english_text))

Scope

Small CLI utility: fixed paths, dictionary-style EN→HI conversion, prints pairs as it goes. Not a localization platform — a sharp tool for static HTML batches.

Try this pattern when

You need offline, markup-safe text transforms (translate, censor, rewrite) and don’t want a full i18n framework.