Skip to content

Commit

Permalink
add the replace_underscor option
Browse files Browse the repository at this point in the history
  • Loading branch information
Amr-Nash committed Jun 9, 2024
1 parent 7cc3db7 commit 4a4ecc7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ translation_requirements:

pull_translations: clean_translations_temp_directory
atlas pull $(ATLAS_OPTIONS) translations/openedx-app-ios/I18N:I18N
python3 i18n_scripts/translation.py --split
python3 i18n_scripts/translation.py --split --replace-underscore

extract_translations: clean_translations_temp_directory
python3 i18n_scripts/translation.py --combine
56 changes: 46 additions & 10 deletions i18n_scripts/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@

import argparse
import os
import re
import sys
from collections import defaultdict
import localizable


def parse_arguments():
"""
Parse command line arguments.
This function is the argument parser for this script.
The script takes only one of the two arguments --split or --combine.
Additionally, the --replace-underscore argument can only be used with --split.
"""
parser = argparse.ArgumentParser(description='Split or combine translations.')
parser = argparse.ArgumentParser(description='Split or Combine translations.')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--split', action='store_true',
help='Split translations into separate files for each module and language.')
group.add_argument('--combine', action='store_true',
help='Combine the English translations from all modules into a single file.')
parser.add_argument('--replace-underscore', action='store_true',
help='Replace underscores with "-r" in language directories (only with --split).')
return parser.parse_args()


Expand Down Expand Up @@ -257,17 +260,50 @@ def split_translation_files(modules_dir=None):
raise


def main():
def replace_underscores(modules_dir=None):
try:
args = parse_arguments()
if args.split:
split_translation_files()
elif args.combine:
combine_translation_files()
if not modules_dir:
modules_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

languages_dirs = get_languages_dirs(modules_dir)

for lang_dir in languages_dirs:
try:
pattern = r'_(\w\w.lproj$)'
if re.search(pattern, lang_dir):
replacement = r'-\1'
new_name = re.sub(pattern, replacement, lang_dir)
lang_old_path = os.path.dirname(get_translation_file_path(modules_dir, 'I18N', lang_dir))
lang_new_path = os.path.dirname(get_translation_file_path(modules_dir, 'I18N', new_name))

os.rename(lang_old_path, lang_new_path)
print(f"Renamed {lang_old_path} to {lang_new_path}")

except FileNotFoundError as e:
print(f"Error: The file or directory {lang_old_path} does not exist: {e}", file=sys.stderr)
raise
except PermissionError as e:
print(f"Error: Permission denied while renaming {lang_old_path}: {e}", file=sys.stderr)
raise
except Exception as e:
print(f"Error: An unexpected error occurred while renaming {lang_old_path} to {lang_new_path}: {e}",
file=sys.stderr)
raise

except Exception as e:
print(f"An unexpected error occurred: {e}", file=sys.stderr)
print(f"Error: An unexpected error occurred in rename_translations_files: {e}", file=sys.stderr)
raise


def main():
args = parse_arguments()
if args.split:
if args.replace_underscore:
replace_underscores()
split_translation_files()
elif args.combine:
combine_translation_files()


if __name__ == "__main__":
main()

0 comments on commit 4a4ecc7

Please sign in to comment.