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 normalization task to work with custom loaders #14

Open
sectasy0 opened this issue Mar 26, 2023 · 1 comment
Open

Refactor normalization task to work with custom loaders #14

sectasy0 opened this issue Mar 26, 2023 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@sectasy0
Copy link
Owner

sectasy0 commented Mar 26, 2023

The normalize_locales function should be rewritten to accept the loader from the --loader parameter, which will be the loader path. It should import this loader into the module and perform normalizations based on this loader.

However, to do this to PyI18nBaseLoader you need to add another abstract method named dump which will be responsible for overwriting the files.

The dump method should overwrite files from the given parameter new: dict in this case there will be standardized translations, you should retype by keys (locale) and for the corresponding key overwrite the corresponding files, also note that the method should also support namespace

Then in the normalize module replace this:

ext: str = loader.type().replace('yaml', 'yml')
dumper: str = {
    "json": lambda x, y: json_dump(x, y, indent=4, sort_keys=True),
    "yml": yaml_dump,
    "xml": lambda x, _: xml_dump(x, pretty=True)
}

.... more code ....

dumper[ext]({locale: sorted_content[locale]}, _f)

with simply:

loader.dump()

In this way, the normalization will be independent of what loader you use and will work for any custom loader ( as long as the dump method has been correctly written.

EDIT: Actually, you can get rid of most of the code from the normalize module, because sorting for default loaders can be taken care of at the YAML/JSON level of the serializer. For custom loaders writing it in dump method is required, so after the normalization task, locales will be sorted and properly dumped.

EDIT2: I haven't thought this through completely and there is one bigger problem with my solution, I want that for the two built-in loaders the loaders don't need to specify which one specifically is used, first I have to find out somehow which loader is used in the project, using the ast module is probably not the best way.

@sectasy0 sectasy0 added enhancement New feature or request good first issue Good for newcomers labels Mar 26, 2023
@sectasy0 sectasy0 pinned this issue Mar 27, 2023
@sectasy0 sectasy0 unpinned this issue Mar 27, 2023
@sectasy0
Copy link
Owner Author

After some thought I decided to keep it simple and handle -l parameter (--loader) to pyi18n-tasks instead of trying to make this part of the code bulletproof and unnecessarily more complex. For built-in loaders it won't be a problem, because all you need to do is to take the appropriate name from the class that the user provides and select the appropriate class, but custom loaders need to be imported dynamically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant