sectasy0/pyi18n

Refactor normalization task to work with custom loaders

Opened this issue · 1 comments

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.

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.