Vauxoo / pre-commit-vauxoo

pre-commit-vauxoo python library to add a command to use all the configuration files and environment variables of Vauxoo
Other
2 stars 2 forks source link

Autoformatter to sort translation terms and wrap lines #104

Closed luisg123v closed 1 year ago

luisg123v commented 1 year ago

One of the things that developers and reviewers (at least myself) invest most of the time on is to insert translations in the right place, i.e. in the same order Odoo exports them.

Adding translations in the correct order is important because:

To help with the above, I propose:

This will also cause lines to be wrapped to 80 characters, similar as Odoo does. Actually, Odoo internally also uses pure polib starting from v13 (seethis and this).

A code like the following would do the trick:

import polib

# Load the .po file
filename = "es.po"
po = polib.pofile(filename)

# Sort terms alphabetically
po.sort(key=lambda t: t.msgid)

# Save the result, which will also wrap lines
po.save()

Another alternative without using polib but without wrapping lines, with pure Python would be:

with open("es.po") as po:
    potext = po.read()

entries = potext.split('\n\n')
headers = entries[0]
terms = entries[1:]
sorted_terms = sorted(terms, key=lambda t: t.split('msgid "')[1])
sorted_terms.insert(0, headers)

with open("es.po.out", "w") as out_file:
    out_file.write("\n\n".join(sorted_terms))

What do you think @moylop260 @antonag32?

CC @edy1192 @montesinose @deivislaya

antonag32 commented 1 year ago

I like the idea, it can probably be made into a simple hook and may be generic enough to go into something like pre-commit-hooks.

luisg123v commented 1 year ago

@antonag32 Take into account pre-commit hooks is for the OCA and they don't need this, because they have a bot that updates translation terms automatically from Weblate

antonag32 commented 1 year ago

Oh, I did not know that, thanks for the heads up. I was thinking about the "official" pre-commit hooks: https://github.com/pre-commit/pre-commit-hooks tho

antonag32 commented 1 year ago

@luisg123v I made a POC for the hook. Can you test it and see if you like it? You can run it with pre-commit run --all-files. Here is a sample pre-commit-config.yaml to use it:

repos:
  - repo: https://github.com/antonag32/pre-commit-hooks
    rev: 85da0ad6d4636ff7c5a84c55b29a45dd7b3df7f0  # Use the ref you want to point at
    hooks:
      - id: pretty-format-po
        args: []

"--autofix" can be added to args for autofixing as well.

cc @moylop260

luisg123v commented 1 year ago

Hi @antonag32,

I've tested your hook but, unfortunately, it's giving unexpected results.

It's wrapping lines to a wrong widdth.

y checking your code, it seems the problem is you're forcing a default line length when it's not provided. I think you should not, because Odoo doesn't specify one, which means it fallbacks to the default one, which is (according to the documentation) 78 characters.

You could test using this file which is already formatted correctly, so you shouldn't get a lint error using that one.

Regards,

antonag32 commented 1 year ago

I think this can be closed?

moylop260 commented 1 year ago

Fixed from https://github.com/Vauxoo/pre-commit-vauxoo/pull/105