diff --git a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py index 3dc160f..cd736b4 100644 --- a/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py +++ b/Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py @@ -10,35 +10,55 @@ 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") + +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) + + +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") -file = open(path, "r").read() -file = json.loads(file) + +# 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) + +except FileNotFoundError: + print("Error: The base language file 'en-US.json' does not exist.") + exit(1) + data = {"sourceLanguage": "en"} strings = {} -for key in file: - language = {} - for lang in languages: - lang_json = json.loads( - open(os.path.join(os.path.join(directory, "jsons"), f"{lang}.json"), "r").read() - ) +# 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 base_language_data: + language = {} + for lang, lang_json in lang_data.items(): # use already loaded language data + translation = lang_json.get(key, "") if lang == "en-US": lang = "en" - if translation != "": + + if translation: language[lang] = {"stringUnit": {"state": "", "value": translation}} 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." 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")