Skip to content

Commit

Permalink
Fix encoding issue
Browse files Browse the repository at this point in the history
The reading was not in utf-8.
Also try/catch decode error.
  • Loading branch information
NicolasGrosjean committed May 8, 2021
1 parent 030258f commit e035694
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/copy_on_other_languages.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import shutil
import sys
Expand All @@ -15,15 +16,20 @@ def copy_on_other_languages(localisation_dir, source_lang, dest_langs):
# Delete destination directories if they exist
shutil.rmtree(os.path.join(localisation_dir, dest_lang), ignore_errors=True)

# Copy English directory
# Copy source directory
shutil.copytree(os.path.join(localisation_dir, source_lang), os.path.join(localisation_dir, dest_lang))


# Edit copied files
for root, _, files in os.walk(os.path.join(localisation_dir, dest_lang)):
for file in files:
with open(os.path.join(root, file), 'r') as f:
lines = f.readlines()
with open(os.path.join(root, file), 'r', encoding='utf-8') as f:
try:
lines = f.readlines()
except UnicodeDecodeError as e:
logging.error(f'Error when parsing {file}')
logging.exception(e)
continue
with open(os.path.join(root, file.replace(f'{source_lang}.yml', f'{dest_lang}.yml')), 'w',
encoding='utf-8') as f:
f.write(f'\ufeffl_{dest_lang}:\n')
Expand All @@ -33,4 +39,5 @@ def copy_on_other_languages(localisation_dir, source_lang, dest_langs):


if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
copy_on_other_languages(sys.argv[1], sys.argv[2], sys.argv[3:])

0 comments on commit e035694

Please sign in to comment.