-
Notifications
You must be signed in to change notification settings - Fork 10
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
Changes from 4 commits
32b8f70
5b97d83
b1ccff7
42e76dd
0d48009
46a68da
d30380a
8b13ee6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
languages = [file.replace(".json", "") for file in dir_list if file.endswith(".json")] | ||
|
||
# Ensure the destination directory exists | ||
if not os.path.exists(directory): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check should probably go before |
||
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 = {} | ||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing as above goes here, I think it should be |
||
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." | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
os.path.join(directory, "Localizable.xcstrings"
should remain as is or the fileLocalizable.xcstrings
to be moved to the folderjsons
as the fileLocalizable.xcstrings
is in the root directoryScribe-i18n
should I move
Localizable.xcstrings
to thejsons
folder since It's a JSON-like format?There was a problem hiding this comment.
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 :)