We can use the following code snippet to handle the translation process while preserving HTML tags:
```python
import re
from googletrans import Translator
def translate_html_content(html_content, dest_language='no'):
# Split the HTML content into tags and text
pattern = r'(<[^>]*>)|([^<]+)'
translator = Translator()
def translate_text(text):
return translator.translate(text, dest=dest_language).text
parts = re.split(pattern, html_content)
translated_parts = []
for part in parts:
if part:
if part.startswith('<'):
# It's an HTML tag, preserve it as is
translated_parts.append(part)
else:
# It's text content, translate it
translated_text = translate_text(part)
translated_parts.append(translated_text)
return ''.join(translated_parts)
# Example usage
html_content = """
Most LinkedIn post templates offer frameworks, but I still find them limiting.
Continue Reading
The post 50 LinkedIn Post Templates Based on Top Influencers’ Top Performing Posts appeared first on Copyblogger.
"""
translated_html = translate_html_content(html_content, dest_language='no')
print(translated_html)
```
This approach will:
1. Use regex to split the HTML into tags and text content
2. Preserve all HTML tags and attributes
3. Translate only the text content between tags
4. Maintain the original HTML structure
The output will be the same HTML structure but with Norwegian text content.