Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarIthawi committed Aug 8, 2024
1 parent c2defac commit 669279a
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions i18n_scripts/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

LOCALIZABLE_FILES_TREE = '<group>'
MAIN_MODULE_NAME = 'OpenEdX'
I18N_MODULE_NAME = 'I18N'


def parse_arguments():
Expand Down Expand Up @@ -76,7 +77,7 @@ def get_modules_dir(override: Path = None) -> Path:
return Path(__file__).absolute().parent.parent


def get_translation_file_path(modules_dir: Path, module_name, lang_dir, create_dirs=False):
def get_translation_file_path(modules_dir: str, module_name, lang_dir, create_dirs=False):
"""
Retrieves the path of the translation file for a specified module and language directory.
Expand All @@ -89,6 +90,7 @@ def get_translation_file_path(modules_dir: Path, module_name, lang_dir, create_d
Returns:
str: The path to the module's translation file (Localizable.strings).
"""
modules_dir = Path(modules_dir)
try:
if module_name == MAIN_MODULE_NAME:
# The main project structure is located into `OpenEdX` rather than `OpenEdX/OpenEdX`
Expand Down Expand Up @@ -122,7 +124,8 @@ def get_modules_to_translate(modules_dir):
if (
os.path.isdir(os.path.join(modules_dir, directory))
and os.path.isfile(get_translation_file_path(modules_dir, directory, 'en.lproj'))
and directory != 'I18N'
and directory != I18N_MODULE_NAME
and directory != MAIN_MODULE_NAME
)
]
return modules_list
Expand Down Expand Up @@ -164,7 +167,7 @@ def get_translations(modules_dir):
print(f"Error retrieving translations: {e}", file=sys.stderr)
raise

return {'I18N': translations}
return {I18N_MODULE_NAME: translations}


def combine_translation_files(modules_dir=None):
Expand Down Expand Up @@ -193,7 +196,7 @@ def get_languages_dirs(modules_dir):
a specific language and ends with the '.lproj' extension.
"""
try:
lang_parent_dir = os.path.join(modules_dir, 'I18N', 'I18N')
lang_parent_dir = os.path.join(modules_dir, I18N_MODULE_NAME, I18N_MODULE_NAME)
languages_dirs = [
directory for directory in os.listdir(lang_parent_dir)
if directory.endswith('.lproj') and directory != "en.lproj"
Expand Down Expand Up @@ -223,7 +226,7 @@ def get_translations_from_file(modules_dir, lang_dir):
"""
translations = defaultdict(list)
try:
translations_file_path = get_translation_file_path(modules_dir, 'I18N', lang_dir)
translations_file_path = get_translation_file_path(modules_dir, I18N_MODULE_NAME, lang_dir)
lang_list = localizable.parse_strings(filename=translations_file_path)
for translation_entry in lang_list:
module_name, key_remainder = translation_entry['key'].split('.', maxsplit=1)
Expand Down Expand Up @@ -265,7 +268,7 @@ def write_translations_to_modules(modules_dir: Path, lang_dir, modules_translati
# 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 */")
f.write(f'/* Empty {lang_dir}/Localizable.strings: Created by i18n_scripts/translation.py */')


def _escape(s):
Expand Down Expand Up @@ -504,13 +507,13 @@ def replace_underscores(modules_dir=None):
languages_dirs = get_languages_dirs(modules_dir)

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

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

0 comments on commit 669279a

Please sign in to comment.