- Current implementation achieve around 80% coverage. And keeps original formatting.
- Error rate is around 15%
Recommended to use with version control. Do a git diff after translation.
#!/bin/env python3
import re, os, sys, html
from bs4 import BeautifulSoup
from bs4.element import Comment
try:
target_file = sys.argv[ 1 ]
except IndexError:
print( "Please specify the file" )
sys.exit( 1 )
infinTigger = 10
def html_escape( text ):
return html.escape( text ).replace( "×", "×" )
def auto_trans( file_path ):
otext = None
with open( file_path, 'r' ) as f:
otext = f.read()
loopGuard = None
loopCount = 0
while True:
soup = BeautifulSoup( otext, "html.parser" )
texts = soup.findAll( text = True )
doBreak = True
for element in texts:
stext = element.strip()
stext = html_escape( stext )
if not stext or stext.startswith( "{" ):
continue
if stext.startswith( "&" ) and stext.endswith( ";" ):
continue
eType = type( element )
if eType is Comment:
continue
if "{%" in stext and "%}" in stext:
print( "WARINING: Partial trans[ " + stext + " ]")
continue
text_cont = element.parent
oelement = str( element )
otext = otext.replace( oelement, element.replace( stext, "{% trans '" + stext + "' %}" ) )
if stext == loopGuard:
loopCount = loopCount + 1
else:
loopCount = 0
if infinTigger < loopCount:
print( "Fatal Error, infinite loop occured on: %s -> %s" % ( text_cont, stext ) )
print( "Possibly escaped text" )
print( "Exiting." )
sys.exit( 1 )
loopGuard = stext
doBreak = False
break
if doBreak:
break
with open( file_path, 'w' ) as f:
f.write( otext )
auto_trans( target_file )
<div>
div
</div>
output.replace( /<{%trans '([^']+)' %}/g, "<\1" );