Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor convert_jsons_to_xcstrings.py for Improved File Handling and Error Management #62

Merged
48 changes: 34 additions & 14 deletions Scribe-i18n/scripts/iOS/convert_jsons_to_xcstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
2 changes: 1 addition & 1 deletion Scribe-i18n/scripts/iOS/convert_xcstrings_to_jsons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down