Medium Translator
As Medium reader and contributor in some cases, I have to confess that for some of complex but interesting articles I like or need :-) to see French translation. After time spent to copy/paste paragraphs from Medium to Google Translator and decided finally to improve this and to make it smoother…
A First version: A first version, below, using advanced libraries newspaper, deep_translator and langdetect.
# pip install newspaper3k googletrans==4.0.0-rc1
from newspaper import Article
from deep_translator import GoogleTranslator
from langdetect import detect
def download_medium_article(url):
article = Article(url)
article.download()
article.parse()
return article.text
def translate_to_french(text):
lang = detect(text)
print(f"Detected language: [{lang}]")
translated_text = GoogleTranslator(source='auto', target='fr').translate(text)
return translated_text
if __name__ == "__main__":
# Replace this with the Medium article you want to translate
medium_url = "https://wlockett.medium.com/the-musk-effect-d264bdd17d93"
try:
english_text = download_medium_article(medium_url)
print("Article downloaded. Translating...")
french_text = translate_to_french(english_text)
print("\n--- Article en Français ---\n")
print(french_text)
except Exception as e:
print(f"An error occurred: {e}")This works fine on my PC but I use to read the articles on iPhone and therefore I need a solution available on Mobile.
Get Stéphane Chalon’s stories in your inbox
Join Medium for free to get updates from this writer.
A second version: The good news is that on Mobile, there are multiple Apps that allow to execute Python code. Many of them need weekly or monhtly subscription for execution. One is not free but can be purchased for reasonable price, Pythonista is one of them. It has many functionalities and number of classical libraries but… not the 3 ones used above in example.
Here, Chat GPT, DeepSeek, Le Chat could help me to port it to a solution that does not need libraries not available in Pythonista. This is solution below.
import requests
from bs4 import BeautifulSoupdownload_medium_article()
def download_medium_article(url):
"""
Fetches and extracts text from a Medium article
"""
headers = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X)'
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
for element in soup(['script', 'style', 'nav', 'footer', 'iframe', 'img']):
element.decompose()
paragraphs = soup.find_all('p')
if not paragraphs:
raise Exception("No content found in article.")
return '\n\n'.join(p.get_text(strip=True) for p in paragraphs)
except Exception as e:
raise Exception(f"Failed to download article: {str(e)}")detect_language_google():
def detect_language_google(text):
"""
Uses Google Translate to detect language.
"""
try:
url = "https://translate.googleapis.com/translate_a/single"
params = {
"client": "gtx",
"sl": "auto",
"tl": "en",
"dt": "t",
"q": text
}
response = requests.get(url, params=params)
result = response.json()
return result[2] # Detected language code
except Exception as e:
print(f"Language detection failed: {e}")
return "unknown"translate_to_french():
def translate_to_french(text):
"""
Translates text to French using Google's unofficial API (no key required).
"""
try:
lang = detect_language_google(text[:300])
print(f"Detected language: [{lang}]")
if lang == 'fr':
return text
url = "https://translate.googleapis.com/translate_a/single"
params = {
"client": "gtx",
"sl": "auto",
"tl": "fr",
"dt": "t",
"q": text
}
response = requests.get(url, params=params)
translated = ''.join(part[0] for part in response.json()[0])
return translated
except Exception as e:
print(f"Translation failed: {e}")
return textmain:
The usage, script is asking for a Medium url, if you copy link of an an article you are reading (my previous article for instance ;-) “https://medium.com/@stchalon/blueskys-butterfly-asking-wall-street-about-his-sentiments-e79affe9b276” and you enter it in the query you get article translated into French that you can have beside you to improve your reading experience as would say a communication person :-)
if __name__ == "__main__":
try:
medium_url = input("Enter Medium article URL: ").strip()
if not medium_url.startswith("http"):
raise ValueError("Invalid URL. Must start with http or https.")
print(f"\nDownloading: {medium_url}")
article_text = download_medium_article(medium_url)
print("\nTranslating to French...")
french_text = translate_to_french(article_text)
print("\n--- Translated Text ---\n")
print(french_text[:2000] + "...") # Preview
save = input("\nSave to file? (y/n): ").lower()
if save == 'y':
filename = "translated_article.txt"
with open(filename, 'w', encoding='utf-8') as f:
f.write(french_text)
print(f"Saved to {filename}")
except Exception as e:
print(f"Error: {str(e)}")







