up translated pages
This commit is contained in:
parent
48b8683028
commit
8008e0291e
4 changed files with 85 additions and 12 deletions
File diff suppressed because one or more lines are too long
Binary file not shown.
|
@ -162,8 +162,23 @@ def save_to_json(data, filename):
|
||||||
filename (str): Name of the file
|
filename (str): Name of the file
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
# Convert data to JSON string
|
||||||
|
json_str = json.dumps(data, indent=2, ensure_ascii=False)
|
||||||
|
|
||||||
|
# Print the JSON string for debugging
|
||||||
|
logger.info(f"JSON string to be written to {filename}:")
|
||||||
|
logger.info(f"JSON keys at top level: {list(data.keys())}")
|
||||||
|
if 'translations' in data:
|
||||||
|
logger.info(f"JSON keys in translations: {list(data['translations'].keys())}")
|
||||||
|
if 'type' in data['translations']:
|
||||||
|
logger.info(f"'type' key exists in translations")
|
||||||
|
if 'type_key' in data['translations']:
|
||||||
|
logger.info(f"'type_key' key exists in translations")
|
||||||
|
|
||||||
|
# Write the JSON string to the file
|
||||||
with open(filename, 'w', encoding='utf-8') as f:
|
with open(filename, 'w', encoding='utf-8') as f:
|
||||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
f.write(json_str)
|
||||||
|
|
||||||
logger.info(f"Data saved to {filename}")
|
logger.info(f"Data saved to {filename}")
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
logger.error(f"Error saving data to {filename}: {e}")
|
logger.error(f"Error saving data to {filename}: {e}")
|
||||||
|
@ -458,9 +473,9 @@ def fetch_wiki_page(key, language='en', is_specific_page=False):
|
||||||
|
|
||||||
# Check grammar for French pages
|
# Check grammar for French pages
|
||||||
grammar_suggestions = []
|
grammar_suggestions = []
|
||||||
if language == 'fr':
|
# if language == 'fr':
|
||||||
logger.info(f"Checking grammar for French page: {key}")
|
# logger.info(f"Checking grammar for French page: {key}")
|
||||||
grammar_suggestions = check_grammar_with_grammalecte(clean_text)
|
# grammar_suggestions = check_grammar_with_grammalecte(clean_text)
|
||||||
|
|
||||||
# Extract links
|
# Extract links
|
||||||
links = content.select('a')
|
links = content.select('a')
|
||||||
|
|
|
@ -148,17 +148,18 @@ French translation:"""
|
||||||
logger.error(f"Error translating text: {e}")
|
logger.error(f"Error translating text: {e}")
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def translate_wiki_page(key):
|
def translate_wiki_page(key, force=False):
|
||||||
"""
|
"""
|
||||||
Translate a wiki page
|
Translate a wiki page
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
key (str): Key or page title
|
key (str): Key or page title
|
||||||
|
force (bool): Force translation even if French page exists
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: Translation information
|
dict: Translation information
|
||||||
"""
|
"""
|
||||||
logger.info(f"Translating wiki page for key: {key}")
|
logger.info(f"Translating wiki page for key: {key!r} (type: {type(key)})")
|
||||||
|
|
||||||
# Check if the key is a specific page
|
# Check if the key is a specific page
|
||||||
is_specific_page = key in SPECIFIC_PAGES or key.startswith('http') or key.startswith('FR:')
|
is_specific_page = key in SPECIFIC_PAGES or key.startswith('http') or key.startswith('FR:')
|
||||||
|
@ -171,8 +172,8 @@ def translate_wiki_page(key):
|
||||||
|
|
||||||
# Check if French page already exists
|
# Check if French page already exists
|
||||||
fr_page = fetch_wiki_page(key, 'fr', is_specific_page=is_specific_page)
|
fr_page = fetch_wiki_page(key, 'fr', is_specific_page=is_specific_page)
|
||||||
if fr_page:
|
if fr_page and not force:
|
||||||
logger.info(f"French page for key '{key}' already exists")
|
logger.info(f"French page for key '{key}' already exists (use force=True to translate anyway)")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Extract the main content from the English page
|
# Extract the main content from the English page
|
||||||
|
@ -220,13 +221,16 @@ def save_translation(translation_info):
|
||||||
bool: True if successful, False otherwise
|
bool: True if successful, False otherwise
|
||||||
"""
|
"""
|
||||||
if not translation_info:
|
if not translation_info:
|
||||||
|
logger.warning("No translation info provided, cannot save")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Load existing translations
|
# Load existing translations
|
||||||
translations = load_json_data(TRANSLATIONS_FILE)
|
translations = load_json_data(TRANSLATIONS_FILE)
|
||||||
|
logger.info(f"Loaded existing translations: {list(translations.get('translations', {}).keys())}")
|
||||||
|
|
||||||
# Initialize if empty
|
# Initialize if empty
|
||||||
if not translations:
|
if not translations:
|
||||||
|
logger.info("No existing translations found, initializing new translations object")
|
||||||
translations = {
|
translations = {
|
||||||
'translations': {},
|
'translations': {},
|
||||||
'last_updated': datetime.now().isoformat()
|
'last_updated': datetime.now().isoformat()
|
||||||
|
@ -234,21 +238,51 @@ def save_translation(translation_info):
|
||||||
|
|
||||||
# Add or update translation
|
# Add or update translation
|
||||||
key = translation_info['key']
|
key = translation_info['key']
|
||||||
|
logger.info(f"Adding/updating translation for key '{key!r}' (type: {type(key)})")
|
||||||
|
|
||||||
|
# Debug: print the translations dictionary structure before adding the new translation
|
||||||
|
logger.info(f"Translations structure before adding: {type(translations)}, keys: {list(translations.keys())}")
|
||||||
|
logger.info(f"Translations['translations'] type: {type(translations.get('translations', {}))}")
|
||||||
|
|
||||||
|
# Add the translation
|
||||||
translations['translations'][key] = translation_info
|
translations['translations'][key] = translation_info
|
||||||
translations['last_updated'] = datetime.now().isoformat()
|
translations['last_updated'] = datetime.now().isoformat()
|
||||||
|
|
||||||
|
# Debug: print the translations dictionary structure after adding the new translation
|
||||||
|
logger.info(f"Translations structure after adding: keys: {list(translations.keys())}")
|
||||||
|
logger.info(f"Translations['translations'] keys after adding: {list(translations.get('translations', {}).keys())}")
|
||||||
|
|
||||||
# Save translations
|
# Save translations
|
||||||
save_to_json(translations, TRANSLATIONS_FILE)
|
logger.info(f"Saving translations to {TRANSLATIONS_FILE}")
|
||||||
|
try:
|
||||||
|
save_to_json(translations, TRANSLATIONS_FILE)
|
||||||
|
logger.info("save_to_json completed successfully")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error saving translations: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Verify the translation was saved
|
||||||
|
try:
|
||||||
|
verify_translations = load_json_data(TRANSLATIONS_FILE)
|
||||||
|
logger.info(f"Verify translations loaded, keys: {list(verify_translations.get('translations', {}).keys())}")
|
||||||
|
if key in verify_translations.get('translations', {}):
|
||||||
|
logger.info(f"Verified translation for key '{key!r}' was saved")
|
||||||
|
else:
|
||||||
|
logger.warning(f"Failed to verify translation for key '{key!r}' was saved")
|
||||||
|
logger.warning(f"Keys in verify_translations: {list(verify_translations.get('translations', {}).keys())}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error verifying translation: {e}")
|
||||||
|
|
||||||
logger.info(f"Translation saved for key '{key}'")
|
logger.info(f"Translation saved for key '{key}'")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def update_translation(key):
|
def update_translation(key, force=True):
|
||||||
"""
|
"""
|
||||||
Update a translation for a specific key
|
Update a translation for a specific key
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
key (str): Key or page title
|
key (str): Key or page title
|
||||||
|
force (bool): Force translation even if French page exists
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
bool: True if successful, False otherwise
|
bool: True if successful, False otherwise
|
||||||
|
@ -256,7 +290,7 @@ def update_translation(key):
|
||||||
logger.info(f"Updating translation for key: {key}")
|
logger.info(f"Updating translation for key: {key}")
|
||||||
|
|
||||||
# Translate the page
|
# Translate the page
|
||||||
translation_info = translate_wiki_page(key)
|
translation_info = translate_wiki_page(key, force=force)
|
||||||
|
|
||||||
# Save the translation
|
# Save the translation
|
||||||
if translation_info:
|
if translation_info:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue