Advertisement
Virajsinh

Using Python Translate English to Gujarati

Oct 29th, 2024
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | Source Code | 0 0
  1. # Required
  2. # python version 3.10.8
  3. # pip install mysql-connector-python
  4. # pip install googletrans==4.0.0-rc1
  5.  
  6. import mysql.connector
  7. from googletrans import Translator
  8.  
  9. # Connect to MySQL
  10. db = mysql.connector.connect(
  11.     host="localhost",
  12.     user="root",
  13.     password="",
  14.     database="practice_db"
  15. )
  16.  
  17. cursor = db.cursor()
  18. translator = Translator()
  19.  
  20. # Fetch English text
  21. cursor.execute("SELECT village_id, village_name FROM tbl_village WHERE village_name_guj IS NULL LIMIT 20")
  22. rows = cursor.fetchall()
  23.  
  24. # Translate and update
  25. for row in rows:
  26.     village_id, english_text = row
  27.     try:
  28.         translated_text = translator.translate(english_text, src='en', dest='gu').text
  29.         cursor.execute("UPDATE tbl_village SET village_name_guj = %s WHERE village_id = %s", (translated_text, village_id))
  30.         db.commit()
  31.     except Exception as e:
  32.         print(f"Error translating '{english_text}': {e}")
  33.  
  34. # Close the cursor and database connection
  35. cursor.close()
  36. db.close()
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement