WeblateOrg / WeblateOrg/weblate

Add option to sort PO files by msgid

Open
#11,113 4 comments 0 reactions 0 assignees View on GitHub
Area: File format support enhancement Waiting for: Demand
Dominant language
Python
Stars
6.1k
Forks
1.4k
Avg merge
9h 53m
Merged PRs (30d)
395

Description

### Describe the problem

In our project we require that PO files are sorted by msgid. When weblate adds new strings to the PO file, it will add them at the end.

### Describe the solution you would like

We would like to use the GettextCustomizeAddon to sort the PO files by msgid using msgmerge.

I'm not a weblate developer, but I looked in the code and there is a chance that such functionality could be relatively easily added to the addon as follows (note, since I don't have a developer install of weblate this was never run nor tested):

```
diff --git a/docs/admin/addons.rst b/docs/admin/addons.rst
index 87ef5707b5..6a13b9f993 100644
--- a/docs/admin/addons.rst
+++ b/docs/admin/addons.rst
@@ -627,26 +627,32 @@ Customize gettext output
------------------------

:Add-on ID: ``weblate.gettext.customize``
-:Configuration: +-----------+---------------------+-----------------------------------------------------------------------------------------------------------------------------------+
- | ``width`` | Long lines wrapping | By default gettext wraps lines at 77 characters and at newlines. With the --no-wrap parameter, wrapping is only done at newlines. |
- | | | |
- | | | Available choices: |
- | | | |
- | | | ``77`` -- Wrap lines at 77 characters and at newlines (xgettext default) |
- | | | |
- | | | ``65535`` -- Only wrap lines at newlines (like 'xgettext --no-wrap') |
- | | | |
- | | | ``-1`` -- No line wrapping |
- +-----------+---------------------+-----------------------------------------------------------------------------------------------------------------------------------+
+:Configuration: +-----------------+---------------------+-----------------------------------------------------------------------------------------------------------------------------------+
+ | ``width`` | Long lines wrapping | By default gettext wraps lines at 77 characters and at newlines. With the --no-wrap parameter, wrapping is only done at newlines. |
+ | | | |
+ | | | Available choices: |
+ | | | |
+ | | | ``77`` -- Wrap lines at 77 characters and at newlines (xgettext default) |
+ | | | |
+ | | | ``65535`` -- Only wrap lines at newlines (like 'xgettext --no-wrap') |
+ | | | |
+ | | | ``-1`` -- No line wrapping |
+ +-----------------+---------------------+-----------------------------------------------------------------------------------------------------------------------------------+
+ | ``sort_output`` | Sorting of output | This option controls whether or not PO files are sorted by msgid. |
+ +-----------------+---------------------+-----------------------------------------------------------------------------------------------------------------------------------+
:Triggers: storage post-load

Allows customization of gettext output behavior, for example line wrapping.

It offers the following options:

-* Wrap lines at 77 characters and at newlines
-* Only wrap lines at newlines
-* No line wrapping
+* Long lines wrapping
+ * Wrap lines at 77 characters and at newlines
+ * Only wrap lines at newlines
+ * No line wrapping
+
+* Sorting of output
+ * The option controls whether the ``--sort-output`` flag is used to sort PO files by msgid.

.. note::

diff --git a/weblate/addons/forms.py b/weblate/addons/forms.py
index 469621ef95..9ad9a11460 100644
--- a/weblate/addons/forms.py
+++ b/weblate/addons/forms.py
@@ -129,6 +129,14 @@ class GettextCustomizeForm(BaseAddonForm):
"With the --no-wrap parameter, wrapping is only done at newlines."
),
)
+ sort_output = forms.BooleanField(
+ label=gettext_lazy("Sort PO files by msgid"),
+ required=False,
+ initial=False,
+ help_text=gettext_lazy(
+ "When activated PO files are sorted by msgid automatically."
+ ),
+ )


class MsgmergeForm(BaseAddonForm):
diff --git a/weblate/addons/gettext.py b/weblate/addons/gettext.py
index e171574048..1bd8ac8d1a 100644
--- a/weblate/addons/gettext.py
+++ b/weblate/addons/gettext.py
@@ -320,11 +320,17 @@ class GettextCustomizeAddon(GettextBaseAddon, StoreBaseAddon):

def store_post_load(self, translation, store):
store.store.wrapper.width = int(self.instance.configuration.get("width", 77))
+ store.store.wrapper.sort_output = bool(
+ self.instance.configuration.get("sort_output", False)
+ )

def get_msgmerge_args(self, component):
+ args = []
if int(self.instance.configuration.get("width", 77)) != 77:
- return ["--no-wrap"]
- return []
+ args.append("--no-wrap")
+ if bool(self.instance.configuration.get("sort_output", False)):
+ args.append("--sort-output")
+ return args


```

### Describe alternatives you have considered

_No response_

### Screenshots

_No response_

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.