@@ -42,10 +42,10 @@ def __init__(self, config: ConfigParser):
4242
4343 self .google_translator = Translator ()
4444
45- def fetch_and_translate (self , page_name , language_code , force = False ):
45+ def fetch_and_translate (self , page_name , language_code , force = False , simulate = False ):
4646 translated_page = self .fortraininglib .get_translation_units (page_name , "en" )
4747
48- if not force and self .fortraininglib .get_translated_title (page_name , language_code ) is not None :
48+ if not simulate and not force and self .fortraininglib .get_translated_title (page_name , language_code ) is not None :
4949 self .logger .warning ("Translation already exists. If you want to force overwrite, use the -f flag." )
5050 return
5151
@@ -57,21 +57,30 @@ def fetch_and_translate(self, page_name, language_code, force=False):
5757 for orig_snippet , trans_snippet in translation_unit :
5858 trans_snippet .content = self .translate_with_deepl_or_google (orig_snippet .content , language_code )
5959 translation_unit .sync_from_snippets ()
60- self .upload_translation (f"{ translation_unit .identifier } /{ language_code } " ,
61- translation_unit .get_translation ())
60+ identifier = f"{ translation_unit .identifier } /{ language_code } "
61+ translated_text = translation_unit .get_translation ()
62+ if simulate :
63+ print (f"--- Translations:{ identifier } ---" )
64+ print (translated_text )
65+ print ()
66+ continue
67+ self .upload_translation (identifier , translated_text )
6268
6369 def translate_with_deepl_or_google (self , text , language_code ) -> str :
6470 """Do the translation: First try DeepL, if that doesn't work (DeepL supports less languages), use Google"""
6571 if self .language_supported_by_deepl :
6672 data = {
67- "auth_key" : self .deepl_api_key ,
6873 "text" : text ,
6974 "target_lang" : language_code
7075 }
71- response = requests .post (self .deepl_endpoint , data = data , timeout = TIMEOUT )
76+ headers = {
77+ "Authorization" : f"DeepL-Auth-Key { self .deepl_api_key } "
78+ }
79+ response = requests .post (self .deepl_endpoint , data = data , headers = headers , timeout = TIMEOUT )
7280 if response .status_code == 200 :
7381 return response .json ()['translations' ][0 ]['text' ]
7482 else :
83+ self .logger .warning (f"DeepL error { response .status_code } : { response .text } " )
7584 self .logger .warning (f"DeepL cannot translate to { language_code } . Using Google Translate instead." )
7685 self .language_supported_by_deepl = False
7786
@@ -93,10 +102,15 @@ def upload_translation(self, identifier: str, translated_text: str):
93102 parser .add_argument ("worksheet_name" , help = "Name of the worksheet to translate." )
94103 parser .add_argument ("language_code" , help = "Target language code for translation." )
95104 parser .add_argument ("-f" , "--force" , action = "store_true" , help = "Force overwrite if translation exists." )
105+ parser .add_argument ("--simulate" , action = "store_true" ,
106+ help = "Print translated units without uploading to MediaWiki." )
96107 args = parser .parse_args ()
97108
98109 config = ConfigParser ()
99110 config .read (join (dirname (abspath (__file__ )), "config.ini" ))
100111
101112 translator_tool = TranslationTool (config )
102- translator_tool .fetch_and_translate (args .worksheet_name , args .language_code , args .force )
113+ translator_tool .fetch_and_translate (args .worksheet_name ,
114+ args .language_code ,
115+ args .force ,
116+ args .simulate )
0 commit comments