-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEn to bn all page my site
69 lines (57 loc) · 2.74 KB
/
En to bn all page my site
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import pywikibot
from googletrans import Translator # Install googletrans==4.0.0-rc1 for compatibility
import time
# Initialize translator
translator = Translator()
# Configure your wiki
site = pywikibot.Site('bn', 'wikipedia') # Target site for Bangla content
site.login()
def translate_and_save_all_pages(source_lang='en', target_lang='bn'):
"""
Translate and save all pages from English to Bangla, including titles.
Args:
source_lang (str): Source language code (default 'en' for English).
target_lang (str): Target language code (default 'bn' for Bangla).
"""
# Iterate over all pages in the English Wikipedia
for page in pywikibot.Page(site, 'Main_Page').site.allpages():
print(f"Processing page: {page.title()}")
# Translate page title
try:
translated_title = translator.translate(page.title(), src=source_lang, dest=target_lang).text
if not translated_title:
print(f"Translation returned empty title for '{page.title()}', skipping.")
continue
except Exception as e:
print(f"Translation failed for title '{page.title()}': {e}")
continue
# Check if the page has content
if not page.text.strip():
print(f"Page '{page.title()}' is empty, skipping.")
continue
# Translate page content
try:
translated_text = translator.translate(page.text, src=source_lang, dest=target_lang).text
if not translated_text:
print(f"Translation returned empty text for '{page.title()}', skipping.")
continue
except Exception as e:
print(f"Translation failed for '{page.title()}': {e}")
continue
# Create or update a new page for the translated content
translated_page_title = translated_title
translated_page = pywikibot.Page(site, translated_page_title)
try:
if translated_page.exists():
print(f"Page '{translated_page_title}' exists. Updating content.")
else:
print(f"Page '{translated_page_title}' does not exist. Creating new page.")
translated_page.text = translated_text
translated_page.save(summary=f"Translated from English to Bangla (title and content) using Pywikibot and Google Translate")
print(f"Page '{translated_page_title}' updated successfully.")
except Exception as e:
print(f"Error saving page '{translated_page_title}': {e}")
# Add delay to avoid rate limiting
time.sleep(5)
# Example usage: Translate and save all pages from English to Bangla
translate_and_save_all_pages(source_lang='en', target_lang='bn')