Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarIthawi committed Aug 8, 2024
1 parent 669279a commit 2b26b07
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions i18n_scripts/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,19 @@ def get_modules_dir(override: Path = None) -> Path:
return Path(__file__).absolute().parent.parent


def get_translation_file_path(modules_dir: str, 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.
Parameters:
modules_dir (str): The path to the base directory containing all the modules.
modules_dir (Path): The path to the base directory containing all the modules.
module_name (str): The name of the module for which the translation path is being retrieved.
lang_dir (str): The name of the language directory within the module's directory.
create_dirs (bool): If True, creates the parent directories if they do not exist. Defaults to False.
Returns:
str: The path to the module's translation file (Localizable.strings).
Path: 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 All @@ -108,24 +107,24 @@ def get_translation_file_path(modules_dir: str, module_name, lang_dir, create_di
raise


def get_modules_to_translate(modules_dir):
def get_modules_to_translate(modules_dir: Path):
"""
Retrieve the names of modules that have translation files for a specified language.
Parameters:
modules_dir (str): The path to the directory containing all the modules.
modules_dir (Path): The path to the directory containing all the modules.
Returns:
list of str: A list of module names that have translation files for the specified language.
"""
try:
modules_list = [
directory for directory in os.listdir(modules_dir)
module_dir for module_dir in os.listdir(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_MODULE_NAME
and directory != MAIN_MODULE_NAME
(modules_dir / module_dir).is_dir()
and os.path.isfile(get_translation_file_path(modules_dir, module_dir, 'en.lproj'))
and module_dir != I18N_MODULE_NAME
and module_dir != MAIN_MODULE_NAME
)
]
return modules_list
Expand All @@ -137,12 +136,12 @@ def get_modules_to_translate(modules_dir):
raise


def get_translations(modules_dir):
def get_translations(modules_dir: Path):
"""
Retrieve the translations from all modules in the modules_dir.
Parameters:
modules_dir (str): The directory containing the modules.
modules_dir (Path): The directory containing the modules.
Returns:
dict: A dict containing a list of dictionaries containing the 'key', 'value', and 'comment' for each
Expand Down Expand Up @@ -175,28 +174,27 @@ def combine_translation_files(modules_dir=None):
Combine translation files from different modules into a single file.
"""
try:
if not modules_dir:
modules_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
modules_dir = get_modules_dir(override=modules_dir)
translation = get_translations(modules_dir)
write_translations_to_modules(modules_dir, 'en.lproj', translation)
except Exception as e:
print(f"Error combining translation files: {e}", file=sys.stderr)
raise


def get_languages_dirs(modules_dir):
def get_languages_dirs(modules_dir: Path):
"""
Retrieve directories containing language files for translation.
Args:
modules_dir (str): The directory containing all the modules.
modules_dir (Path): The directory containing all the modules.
Returns:
list: A list of directories containing language files for translation. Each directory represents
a specific language and ends with the '.lproj' extension.
"""
try:
lang_parent_dir = os.path.join(modules_dir, I18N_MODULE_NAME, I18N_MODULE_NAME)
lang_parent_dir = 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 @@ -308,8 +306,7 @@ def split_translation_files(modules_dir=None):
None
"""
try:
if not modules_dir:
modules_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
modules_dir = get_modules_dir(override=modules_dir)
languages_dirs = get_languages_dirs(modules_dir)
for lang_dir in languages_dirs:
translations = get_translations_from_file(modules_dir, lang_dir)
Expand Down Expand Up @@ -358,7 +355,7 @@ def get_xcode_projects(modules_dir: Path) -> [{Path, XcodeProject}]:
"""
Return a list of module_name, xcode_project pairs.
"""
for module_name in get_modules_to_translate(str(modules_dir)):
for module_name in get_modules_to_translate(modules_dir):
xcode_project = get_xcode_project(modules_dir, module_name)
yield module_name, xcode_project

Expand Down Expand Up @@ -406,7 +403,6 @@ def add_translation_files_to_xcode(modules_dir: Path = None):
"""
try:
modules_dir = get_modules_dir(override=modules_dir)

for module_name, xcode_project in get_xcode_projects(modules_dir):
print(f'## Entering project: {module_name}')
module_path = modules_dir / module_name
Expand Down Expand Up @@ -478,7 +474,6 @@ def clean_translation_files(modules_dir: Path = None):
"""
try:
modules_dir = get_modules_dir(override=modules_dir)

for module_name, xcode_project in get_xcode_projects(modules_dir):
print(f'## Entering project: {module_name}')
module_path = modules_dir / module_name
Expand All @@ -501,9 +496,7 @@ def clean_translation_files(modules_dir: Path = None):

def replace_underscores(modules_dir=None):
try:
if not modules_dir:
modules_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

modules_dir = get_modules_dir(override=modules_dir)
languages_dirs = get_languages_dirs(modules_dir)

for lang_dir in languages_dirs:
Expand Down

0 comments on commit 2b26b07

Please sign in to comment.