From f354bb4198efcddc451d6acad5e420148e175775 Mon Sep 17 00:00:00 2001 From: Omar Agiez Date: Mon, 7 Oct 2024 01:23:40 +0300 Subject: [PATCH 1/8] Fix file handling: Use context managers to ensure safe reading and writing of files. --- Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py index 3dc160f..62e7dc6 100644 --- a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py +++ b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py @@ -15,8 +15,9 @@ [file.replace(".json", "") for file in json_dir_list if file.endswith(".json")] ) path = os.path.join(os.path.join(directory, "jsons"), "en-US.json") -file = open(path, "r").read() -file = json.loads(file) + +with open(path, "r") as json_file: + file = json.load(json_file) data = {"sourceLanguage": "en"} strings = {} @@ -37,8 +38,9 @@ strings[key] = {"comment": "", "localizations": language} data |= {"strings": strings, "version": "1.0"} -file = open(os.path.join(directory, "Localizable.xcstrings"), "w") -json.dump(data, file, indent=2, ensure_ascii=False, separators=(",", " : ")) + +with open(os.path.join(directory, "Localizable.xcstrings"), "w") as outfile: + json.dump(data, outfile, indent=2, ensure_ascii=False, separators=(",", " : ")) print( "Scribe-i18n localization JSON files successfully converted to the Localizable.xcstrings file." From 81da648774fce2efd2451301e61012eb31dc2b93 Mon Sep 17 00:00:00 2001 From: Omar Agiez Date: Mon, 7 Oct 2024 01:48:02 +0300 Subject: [PATCH 2/8] Refactor: Use a single variable for the 'jsons' folder path and ensure it exists. --- .../scripts/iOS/convert_jsons_to_xcstrings.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py index 62e7dc6..82dc08e 100644 --- a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py +++ b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py @@ -10,13 +10,20 @@ import os directory = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -json_dir_list = os.listdir(os.path.join(directory, "jsons")) +# Define the path to the "jsons" folder +jsons_folder = os.path.join(directory, "jsons") + +# Ensure the "jsons" folder exists inside the directory +if not os.path.exists(jsons_folder): + os.makedirs(jsons_folder) + +json_dir_list = os.listdir(jsons_folder) languages = sorted( [file.replace(".json", "") for file in json_dir_list if file.endswith(".json")] ) -path = os.path.join(os.path.join(directory, "jsons"), "en-US.json") -with open(path, "r") as json_file: +# Load the base language file safely +with open(os.path.join(jsons_folder, "en-US.json"), "r") as json_file: file = json.load(json_file) data = {"sourceLanguage": "en"} @@ -25,9 +32,9 @@ language = {} for lang in languages: - lang_json = json.loads( - open(os.path.join(os.path.join(directory, "jsons"), f"{lang}.json"), "r").read() - ) + with open(os.path.join(jsons_folder, f"{lang}.json"), "r") as lang_file: + lang_json = json.load(lang_file) + translation = lang_json[key] if key in lang_json else "" if lang == "en-US": From d811de7ac9f58033b2f2d5335b8b0bd62d1b914b Mon Sep 17 00:00:00 2001 From: Omar Agiez Date: Mon, 7 Oct 2024 02:31:10 +0300 Subject: [PATCH 3/8] Optimize performance by pre-loading language JSON files - Refactored the code to load all language JSON files into memory once, reducing redundant file access. - Improved translation lookup efficiency by using a dictionary for language data, resulting in faster processing of keys. - Enhanced overall script performance, especially beneficial for larger datasets --- .../scripts/iOS/convert_jsons_to_xcstrings.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py index 82dc08e..13ae2a0 100644 --- a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py +++ b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py @@ -28,18 +28,20 @@ data = {"sourceLanguage": "en"} strings = {} -for key in file: - language = {} - - for lang in languages: - with open(os.path.join(jsons_folder, f"{lang}.json"), "r") as lang_file: - lang_json = json.load(lang_file) +# Pre-load all JSON files into a dictionary +lang_data = {} +for lang in languages: + with open(os.path.join(jsons_folder, f"{lang}.json"), "r") as lang_file: + lang_data[lang] = json.load(lang_file) - translation = lang_json[key] if key in lang_json else "" +for key in file: # Iterate over each key in the base language file + language = {} + for lang, lang_json in lang_data.items(): # Use already loaded language data + translation = lang_json.get(key, "") # Access the translation if lang == "en-US": lang = "en" - if translation != "": + if translation: language[lang] = {"stringUnit": {"state": "", "value": translation}} strings[key] = {"comment": "", "localizations": language} From d41c8eb6457cf287fa4acdec9715d5ed1ca979db Mon Sep 17 00:00:00 2001 From: Omar Agiez Date: Mon, 7 Oct 2024 02:45:47 +0300 Subject: [PATCH 4/8] Remove automatic creation of the 'jsons' folder - Added a check to ensure the 'jsons' folder exists, exiting with an error message if it is missing. --- Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py index 13ae2a0..b2680e6 100644 --- a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py +++ b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py @@ -15,7 +15,9 @@ # Ensure the "jsons" folder exists inside the directory if not os.path.exists(jsons_folder): - os.makedirs(jsons_folder) + print(f"Error: The folder '{jsons_folder}' does not exist. Please ensure the path is correct.") + exit(1) + json_dir_list = os.listdir(jsons_folder) languages = sorted( From aee58c08826755063252d6d01bf0ded1be6f890a Mon Sep 17 00:00:00 2001 From: Omar Agiez Date: Mon, 7 Oct 2024 02:48:29 +0300 Subject: [PATCH 5/8] Add error handling for missing base language file - Implemented a try-except block for loading 'en-US.json' to catch FileNotFoundError. - Added a user-friendly error message to indicate if the base language file is missing. --- Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py index b2680e6..5377f30 100644 --- a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py +++ b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py @@ -25,8 +25,13 @@ ) # Load the base language file safely -with open(os.path.join(jsons_folder, "en-US.json"), "r") as json_file: - file = json.load(json_file) +try: + with open(os.path.join(jsons_folder, "en-US.json"), "r") as json_file: + file = json.load(json_file) +except FileNotFoundError: + print("Error: The base language file 'en-US.json' does not exist.") + exit(1) + data = {"sourceLanguage": "en"} strings = {} From 8925a4b9481d7284cd4e57cc70ba5a47fe34180e Mon Sep 17 00:00:00 2001 From: Omar Agiez Date: Mon, 7 Oct 2024 02:51:44 +0300 Subject: [PATCH 6/8] Rename variable for improved clarity - Changed variable name from 'file' to 'base_language_data' for better readability and to clearly indicate its purpose. --- Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py index 5377f30..5e70a9a 100644 --- a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py +++ b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py @@ -27,7 +27,7 @@ # Load the base language file safely try: with open(os.path.join(jsons_folder, "en-US.json"), "r") as json_file: - file = json.load(json_file) + base_language_data = json.load(json_file) except FileNotFoundError: print("Error: The base language file 'en-US.json' does not exist.") exit(1) @@ -42,7 +42,7 @@ with open(os.path.join(jsons_folder, f"{lang}.json"), "r") as lang_file: lang_data[lang] = json.load(lang_file) -for key in file: # Iterate over each key in the base language file +for key in base_language_data: # Iterate over each key in the base language file language = {} for lang, lang_json in lang_data.items(): # Use already loaded language data translation = lang_json.get(key, "") # Access the translation From 9fcc0dee26b0701f463298fc92030d5719d86360 Mon Sep 17 00:00:00 2001 From: Omar Agiez Date: Mon, 7 Oct 2024 04:29:30 +0300 Subject: [PATCH 7/8] fix: update inline comments to start with lowercase as per style guide. --- .../scripts/iOS/convert_jsons_to_xcstrings.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py index 5e70a9a..5f71bb4 100644 --- a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py +++ b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py @@ -10,10 +10,10 @@ import os directory = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -# Define the path to the "jsons" folder +# Define the path to the "jsons" folder. jsons_folder = os.path.join(directory, "jsons") -# Ensure the "jsons" folder exists inside the directory +# Ensure the "jsons" folder exists inside the directory. if not os.path.exists(jsons_folder): print(f"Error: The folder '{jsons_folder}' does not exist. Please ensure the path is correct.") exit(1) @@ -24,7 +24,7 @@ [file.replace(".json", "") for file in json_dir_list if file.endswith(".json")] ) -# Load the base language file safely +# Load the base language file safely. try: with open(os.path.join(jsons_folder, "en-US.json"), "r") as json_file: base_language_data = json.load(json_file) @@ -36,16 +36,16 @@ data = {"sourceLanguage": "en"} strings = {} -# Pre-load all JSON files into a dictionary +# Pre-load all JSON files into a dictionary. lang_data = {} for lang in languages: with open(os.path.join(jsons_folder, f"{lang}.json"), "r") as lang_file: lang_data[lang] = json.load(lang_file) -for key in base_language_data: # Iterate over each key in the base language file +for key in base_language_data: # iterate over each key in the base language file language = {} - for lang, lang_json in lang_data.items(): # Use already loaded language data - translation = lang_json.get(key, "") # Access the translation + for lang, lang_json in lang_data.items(): # use already loaded language data + translation = lang_json.get(key, "") # access the translation if lang == "en-US": lang = "en" if translation: From a3e5fbf18fc8477feb7e7a63e8b03270920ead4f Mon Sep 17 00:00:00 2001 From: Andrew Tavis McAllister Date: Mon, 7 Oct 2024 23:41:57 +0200 Subject: [PATCH 8/8] Minor edits to script spacing and comments --- Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py | 8 +++++--- Scribe-i18n/scripts/iOS/convert_xcstrings_to_jsons.py | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py index 5f71bb4..cd736b4 100644 --- a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py +++ b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py @@ -10,10 +10,10 @@ import os directory = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + # Define the path to the "jsons" folder. jsons_folder = os.path.join(directory, "jsons") -# Ensure the "jsons" folder exists inside the directory. if not os.path.exists(jsons_folder): print(f"Error: The folder '{jsons_folder}' does not exist. Please ensure the path is correct.") exit(1) @@ -28,6 +28,7 @@ try: with open(os.path.join(jsons_folder, "en-US.json"), "r") as json_file: base_language_data = json.load(json_file) + except FileNotFoundError: print("Error: The base language file 'en-US.json' does not exist.") exit(1) @@ -42,12 +43,13 @@ with open(os.path.join(jsons_folder, f"{lang}.json"), "r") as lang_file: lang_data[lang] = json.load(lang_file) -for key in base_language_data: # iterate over each key in the base language file +for key in base_language_data: language = {} for lang, lang_json in lang_data.items(): # use already loaded language data - translation = lang_json.get(key, "") # access the translation + translation = lang_json.get(key, "") if lang == "en-US": lang = "en" + if translation: language[lang] = {"stringUnit": {"state": "", "value": translation}} diff --git a/Scribe-i18n/scripts/iOS/convert_xcstrings_to_jsons.py b/Scribe-i18n/scripts/iOS/convert_xcstrings_to_jsons.py index 83f8863..74c9656 100644 --- a/Scribe-i18n/scripts/iOS/convert_xcstrings_to_jsons.py +++ b/Scribe-i18n/scripts/iOS/convert_xcstrings_to_jsons.py @@ -58,7 +58,7 @@ data[key] = translation lang = "en-US" if lang == "en" else lang - # Write to the destination JSON file using a context manager. + with open(f"{jsons_folder}/{lang}.json", "w") as dest: json.dump(data, dest, indent=2, ensure_ascii=False) dest.write("\n")