-
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_jsons_to_xcstrings.py for Improved File Handling and Error Management #62
Changes from 6 commits
f354bb4
81da648
d811de7
d41c8eb
aee58c0
8925a4b
9fcc0de
a3e5fbf
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 |
---|---|---|
|
@@ -10,35 +10,53 @@ | |
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): | ||
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: # Iterate over each key in the base language file | ||
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. These commends though don't need punctuation as you have them, but then as they're inline the first letter shouldn't be capitalized. The first word of a comment is only capitalized when it's its own line. 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.
I’ve made the edits to the comments as suggested. Thank you for pointing out the details; I’m learning a lot from your feedback! |
||
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} | ||
|
||
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." | ||
|
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.
Quick thing, @OmarAI2003: For Pythonic comments if it's its own line then there should be punctuation, so this should have a period.