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

Conversation

OmarAI2003
Copy link
Contributor

@OmarAI2003 OmarAI2003 commented Oct 3, 2024

This PR introduces several improvements discussed here to the convert_xcstrings_to_jsons.py script used for converting Scribe-i18n's Localizable.xcstrings file to localization JSON files.

Changes Made:

  1. File Handling Improvements:

    • Implemented context managers for reading the Localizable.xcstrings file and writing the output JSON files. This ensures that files are properly closed after use.
  2. Error Handling:

    • Added checks for FileNotFoundError to handle cases where the Localizable.xcstrings file is missing.
    • Wrapped the JSON loading process in a try-except block to handle potential JSON decoding errors gracefully, providing informative error messages.
  3. Directory Existence Check:

    • Ensured the destination directory exists before attempting to write JSON files. This avoids potential errors when trying to write to a non-existent directory.

These improvements improve the file's reliability and maintainability, facilitating smoother execution and easier debugging. I welcome any feedback for further improvements.

Related issue:
#57

- Updated file reading to use a context manager for better resource management.
- Added error handling for FileNotFoundError when accessing Localizable.xcstrings.
- Implement try-except block for JSONDecodeError to handle invalid JSON format.
- Refined language handling.
- Added directory existence check and creation to prevent errors.
Copy link

github-actions bot commented Oct 3, 2024

Thank you for the pull request!

The Scribe team will do our best to address your contribution as soon as we can. The following is a checklist for maintainers to make sure this process goes as well as possible. Feel free to address the points below yourself in further commits if you realize that actions are needed :)

If you're not already a member of our public Matrix community, please consider joining! We'd suggest using Element as your Matrix client, and definitely join the General and i18n rooms once you're in. Also consider joining our bi-weekly Saturday dev syncs. It'd be great to have you!

Maintainer checklist

  • The commit messages for the remote branch should be checked to make sure the contributor's email is set up correctly so that they receive credit for their contribution
    • The contributor's name and icon in remote commits should be the same as what appears in the PR
    • If there's a mismatch, the contributor needs to make sure that the email they use for GitHub matches what they have for git config user.email in their local activist repo

@andrewtavis andrewtavis requested a review from Jag-Marcel October 4, 2024 08:29
@andrewtavis
Copy link
Member

@Jag-Marcel, could I ask that you take a first look here? Thanks so much for the PR, @OmarAI2003!

Copy link
Member

@Jag-Marcel Jag-Marcel left a comment

Choose a reason for hiding this comment

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

Almost everything here looks really good to me! As for the issues with the file paths, @OmarAI2003 do you want to change them in your own PRs for #57 too?

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 :)

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".

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):
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.

OmarAI2003 and others added 4 commits October 4, 2024 16:15
This commit restores the Localizable.xcstrings file to its previous directory as requested. Keeping the file in its original location simplifies integration with Weblate, which reads from the jsons directory.
Copy link
Member

@andrewtavis andrewtavis left a comment

Choose a reason for hiding this comment

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

All's really great here, @OmarAI2003! I realized when I tried to run it that we were writing an en.json file, so I just switched en back to en-US for now. We can see how localization progresses in the future for us maybe having different dialects 😊

Would be great if you sent along similar changes to the other scripts! Very very welcome on our end :)

@andrewtavis andrewtavis merged commit 13d52f2 into scribe-org:main Oct 6, 2024
@andrewtavis andrewtavis added the hacktoberfest-accepted Accepted as a part of Hacktoberfest label Oct 6, 2024
@OmarAI2003
Copy link
Contributor Author

All's really great here, @OmarAI2003! I realized when I tried to run it that we were writing an en.json file, so I just switched en back to en-US for now. We can see how localization progresses in the future for us maybe having different dialects 😊

Would be great if you sent along similar changes to the other scripts! Very very welcome on our end :)

Thank you so much for your work and for adding the Hacktoberfest labels! I really appreciate it. 😊 I'll be happy to make similar changes to the other scripts as well. Is there any particular script you'd like me to start with?

@andrewtavis
Copy link
Member

Let's finish the iOS ones and then move on to the Android ones, @OmarAI2003? :)

@OmarAI2003 OmarAI2003 deleted the improve-convert-xcstrings-to-jsons branch October 6, 2024 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted Accepted as a part of Hacktoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants