Skip to content

Commit

Permalink
fix: fixup previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarIthawi committed Aug 8, 2024
1 parent ccbdaf2 commit c2defac
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions i18n_scripts/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_modules_dir(override: Path = None) -> Path:
return Path(__file__).absolute().parent.parent


def get_translation_file_path(modules_dir, module_name, lang_dir, create_dirs=False):
def get_translation_file_path(modules_dir: Path, module_name, lang_dir, create_dirs=False):
"""
Retrieves the path of the translation file for a specified module and language directory.
Expand All @@ -90,10 +90,17 @@ def get_translation_file_path(modules_dir, module_name, lang_dir, create_dirs=Fa
str: The path to the module's translation file (Localizable.strings).
"""
try:
lang_dir_path = os.path.join(modules_dir, module_name, module_name, lang_dir, 'Localizable.strings')
if module_name == MAIN_MODULE_NAME:
# The main project structure is located into `OpenEdX` rather than `OpenEdX/OpenEdX`
module_path = modules_dir / module_name
else:
# Rest of modules such as Core, Course, Dashboard, etc follow the `Dashboard/Dashboard` structure
module_path = modules_dir / module_name / module_name

lang_dir_path = module_path / lang_dir
if create_dirs:
os.makedirs(os.path.dirname(lang_dir_path), exist_ok=True)
return lang_dir_path
lang_dir_path.mkdir(parents=True, exist_ok=True)
return lang_dir_path / 'Localizable.strings'
except Exception as e:
print(f"Error creating directory path: {e}", file=sys.stderr)
raise
Expand Down Expand Up @@ -232,7 +239,7 @@ def get_translations_from_file(modules_dir, lang_dir):
return translations


def write_translations_to_modules(modules_dir, lang_dir, modules_translations):
def write_translations_to_modules(modules_dir: Path, lang_dir, modules_translations):
"""
Write translations to language files for each module.
Expand All @@ -254,6 +261,12 @@ def write_translations_to_modules(modules_dir, lang_dir, modules_translations):
print(f"Error writing translations to file.\n Module: {module}\n Error: {e}", file=sys.stderr)
raise

# The main project structure is located into `OpenEdX` rather than `OpenEdX/OpenEdX`
# Empty files are added, so iOS knows which languages are supported in this app
main_translation_file_path = get_translation_file_path(modules_dir, MAIN_MODULE_NAME, lang_dir, create_dirs=True)
with open(main_translation_file_path, 'w') as f:
f.write("/* Empty Localizable.strings: Created by i18n_scripts/translation.py */")


def _escape(s):
"""
Expand Down Expand Up @@ -404,6 +417,19 @@ def add_translation_files_to_xcode(modules_dir: Path = None):
)
xcode_project.save()

# This project is used to specify which languages are supported by the app for iOS
print(f'## Entering project: {MAIN_MODULE_NAME}')
main_xcode_project = get_xcode_project(modules_dir, module_name=MAIN_MODULE_NAME)
main_xcode_project_module_path = modules_dir / MAIN_MODULE_NAME
with change_directory(main_xcode_project_module_path):
# The main project structure is located into `OpenEdX` rather than `OpenEdX/OpenEdX`
for localizable_abs_path in list_translation_files(main_xcode_project_module_path):
add_localizable(
xcode_project=main_xcode_project,
localizable_relative_path=localizable_abs_path.relative_to(main_xcode_project_module_path),
)
main_xcode_project.save()

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

0 comments on commit c2defac

Please sign in to comment.