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_xcstrings_to_jsons.py for Improved File Handling and Error Management #58

Merged
34 changes: 27 additions & 7 deletions Scribe-i18n/scripts/iOS/convert_xcstrings_to_jsons.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,34 @@
import json
import os

# Determine the directory
directory = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
file = open(os.path.join(directory, "Localizable.xcstrings"), "r").read()

# Read the Localizable.xcstrings file
try:
with open(os.path.join(directory, "Localizable.xcstrings"), "r") as f:
file = f.read()
except FileNotFoundError:
print("Error: Localizable.xcstrings file not found.")
exit(1)

dir_list = os.listdir(directory)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I missed the fact we restructured the json files into their own directory, it should be os.path.join(directory, "jsons") with our current structure? @andrewtavis this could also be an issue with all the other scripts.

Copy link
Contributor Author

@OmarAI2003 OmarAI2003 Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I missed the fact we restructured the json files into their own directory, it should be os.path.join(directory, "jsons") with our current structure? @andrewtavis this could also be an issue with all the other scripts.

I think os.path.join(directory, "Localizable.xcstrings" should remain as is or the file Localizable.xcstrings to be moved to the folder jsons as the file Localizable.xcstrings is in the root directory Scribe-i18n

should I move Localizable.xcstrings to the jsons folder since It's a JSON-like format?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No let's keep it where it is, as that would complicate Weblate that's reading in the jsons directory :)

languages = [file.replace(".json", "") for file in dir_list if file.endswith(".json")]

# Ensure the destination directory exists
if not os.path.exists(directory):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should probably go before dir_list is created on line 23, since it already uses the directory. I don't think we need to check directory itself, since that's supposed to be the root Scribe-i18n directory anyway. It could be worth it if there's a variable for the jsons directory and you do the same check on that instead.

os.makedirs(directory)

for lang in languages:
dest = open(f"{directory}/{lang}.json", "w")
if lang == "en-US":
lang = "en"
lang = "en" if lang == "en-US" else lang

# Attempt to load the JSON data
try:
json_file = json.loads(file)
except json.JSONDecodeError:
print("Error: The Localizable.xcstrings file is not valid JSON.")
exit(1)

json_file = json.loads(file)
strings = json_file["strings"]

data = {}
Expand All @@ -38,8 +55,11 @@

data[key] = translation

json.dump(data, dest, indent=2, ensure_ascii=False)
dest.write("\n")
# Write to the destination JSON file using a context manager
with open(f"{directory}/{lang}.json", "w") as dest:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing as above goes here, I think it should be f"{directory}/jsons/{lang}.json".

json.dump(data, dest, indent=2, ensure_ascii=False)
dest.write("\n")


print(
"Scribe-i18n Localizable.xcstrings file successfully converted to the localization JSON files."
Expand Down
Loading