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 i18-checks to use YAML, modularize utils, and fix errors #7

Merged
merged 6 commits into from
Feb 8, 2025

Conversation

OmarAI2003
Copy link
Contributor

Contributor checklist


Description

This PR refactors the i18n check scripts by improving modularity, introducing YAML-based configuration, and ensuring full project functionality.

  • Refactored Checks: Extracted reusable functions from various check scripts into [utils.py](http://utils.py/) to improve maintainability.
  • YAML-Based Configuration: Replaced direct directory reads with paths defined in .i18n-check.yaml, making the scripts more flexible.
  • Bug Fixes: Addressed various issues in check scripts to ensure they function correctly.
  • Utility Functions: Added helper functions for i18n key validation and YAML configuration loading.
  • i18n Key Mapping: Implemented gen_map_obj.py to generate an i18n-map.ts file with structured key mappings.
  • Configuration Update: Added types_dir path in .i18n-check.yaml to ensure scripts locate the correct directories.

Related issue

Copy link

github-actions bot commented Feb 7, 2025

Thank you for the pull request! ❤️

The i18n-check team will do our best to address your contribution as soon as we can. 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 Development rooms once you're in. Also consider attending our bi-weekly Saturday developer syncs! It'd be great to meet you 😊

Copy link

github-actions bot commented Feb 7, 2025

Maintainer Checklist

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

  • The changelog has been updated with a description of the changes for the upcoming release and the corresponding issue (if necessary)

@andrewtavis andrewtavis self-requested a review February 7, 2025 09:59
@andrewtavis
Copy link
Member

Nice, @OmarAI2003! First glance, this is looking really great 😊 I'll try to bring this in before the sync tomorrow and then we can discuss next steps!

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.

Writing out some notes as I go through the changes, @OmarAI2003:

  • I'd suggest getting your IDE set up with the ruff (edit: ruff, not black) code formatter and also to install the extension (if you already have this done, then check your settings)
    • Imports weren't sorted and there were some formatting changes that can happen automatically on save
    • Set formatting to run on save, if you don't have that, and also on paste so that code you get from elsewhere is automatically formatted

@andrewtavis
Copy link
Member

Installing ruff will also lint the code, which will tell you when you have unused imports and other things like that :)

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.

This is looking great so far, @OmarAI2003 :) Can I ask you to look through the comments I made and the changes I sent along in my commit so you can familiarize yourself with the changes? 😊 I'll make a suggestion on which issue to work on next in the issues 🚀

)
en_us_json_dict = read_json_file(en_us_json_file)

files_to_check =collect_files(frontend_directory, file_types_to_check, directories_to_skip, files_to_skip)
Copy link
Member

Choose a reason for hiding this comment

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

Let's always do explicit function arguments as it makes it easier for others who don't know what the function does to see what the purpose is. Even if it's just repeating the argument as we use the same name, it still makes it more clear what the argument is being used for :)

.i18n-check.yaml Outdated
@@ -5,6 +5,7 @@ src-dir: tests
i18n-dir: tests/test_frontend/test_i18n
i18n-src: tests/test_frontend/test_i18n/test-i18n-src.json
i18n-map: tests/test_frontend/test-i18n-map.ts
types_dir: src\i18n_check\types
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we'll need this, as the i18n-map object location should come directly from the YAML i18n-map value :)

Copy link
Member

Choose a reason for hiding this comment

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

I'll remove this and the file.

Copy link
Member

Choose a reason for hiding this comment

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

The usages of this can all be replaced with i18n_map_file from utils :)

"""
Reads a JSON file and returns its content.
Parameters:
Copy link
Member

Choose a reason for hiding this comment

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

Let's be sure to follow the formatting for the function docstrings :)

Parameters
----------
p : list str
Copy link
Member

Choose a reason for hiding this comment

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

list[str]

i18n_check_dir = str(Path(config["i18n-dir"]).resolve())
json_file_directory = Path(config["i18n-dir"]).resolve()
frontend_directory = Path(config["src-dir"]).resolve()
en_us_json_file = Path(config["i18n-src"]).resolve()
Copy link
Member

Choose a reason for hiding this comment

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

Let's remember that en-US might not be the src language for other projects, and also we might expand this out to work for other file types eventually :) Will rename this accordingly.


# MARK: collecting File

def collect_files(directory, file_types, directories_to_skip, files_to_skip):
Copy link
Member

Choose a reason for hiding this comment

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

Let's also remember to type the arguments for functions :)

# MARK: YAML Reading

# Define the path to the YAML configuration file
config_path = Path(__file__).parent.parent.parent / ".i18n-check.yaml"
Copy link
Member

Choose a reason for hiding this comment

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

There's a chance that this is going to be more robust. Specifically we need to figure out how other CLIs find their specific YAML files. The CLI should search the directory where the code is being called, odds are, but we can do this later :)

@andrewtavis andrewtavis merged commit 5578742 into activist-org:main Feb 8, 2025
Parameters
----------
p : list str
p : list[str]
The string of the directory path of a file that has a key.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this part should be changed to

p : str  
    The directory path of a file that contains a key.

Since p is a plain string, not a list. You can see this from the loop variable c in for i, c in enumerate(p):, where c represents individual characters in the inputted p parameter

Copy link
Member

Choose a reason for hiding this comment

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

Ah ok, nice :) Feel free to revert this in the next PR!

@OmarAI2003
Copy link
Contributor Author

Installing ruff will also lint the code, which will tell you when you have unused imports and other things like that :)

I had Ruff installed as well as its extension, but maybe I wasn't warned by Ruff when I did the commit because I didn't run pre-commit install in the terminal🤔🤷

@andrewtavis
Copy link
Member

Could be the case, @OmarAI2003 :) Maybe also set up format on save? It'll get figured out eventually :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants