weblate icon indicating copy to clipboard operation
weblate copied to clipboard

Support Weblate Flags in Gettext Files via Extracted Comments

Open anubeon opened this issue 2 years ago • 10 comments

Describe the problem

We use GNU gettext for translating strings for embedded systems, with our programmers providing human-readable cues for things like maximum character length via the extracted comments feature (example below).

#.
#. STARTUP TYPEWRITER TEXT
#. ------------------------------------------------------------------
#. EEPROM BLANK (Maxlen=12)
msgid "BLANK"
msgstr "VIDE"

#. (DO NOT TRANSLATE!)
msgid "EEPROM"
msgstr ""

For our use case, this could potentially be a far more efficient means of setting weblate-flags such as max-length:N than manually setting said flags via Weblate's UI. Presently, however, only certain XML based formats support storage and synchronisation of weblate-flags.

Describe the solution you would like

We suggest supporting storage and synchronisation of weblate-flags within gettext's extracted comments, in a similar vain to the non-standard weblate-flags attribute supported for XLIFF.

As the extracted comments feature of GNU gettext is designed to allow programmers to provide cues to translators, this would seem the ideal means of conveying machine-readable instructions to a translation tool such as Weblate.

Example:

#.
#. STARTUP TYPEWRITER TEXT
#. ------------------------------------------------------------------
#. EEPROM BLANK weblate-flags="max-length:12, priority:50"
msgid "BLANK"
msgstr "VIDE"

#. weblate-flags="read-only"
msgid "EEPROM"
msgstr ""

In the example above, weblate-flags are:

  1. Appended to the end of the extracted comments.
  2. Signified via the keyword/prefix weblate-flags=.
  3. Encapsulated by double quotes.
  4. Comma-delimited.

These would seem like reasonable conventions to follow?

Describe alternatives you have considered

No response

Screenshots

No response

Additional context

No response

anubeon avatar May 16 '24 16:05 anubeon

Weblate does extract flags from Gettext type comments:

#: weblate/addons/forms.py:541
#, python-format
msgid "Could not parse CSS selector: %s"
msgstr "Nepodařilo se zpracovat CSS selektor: %s"

Will get python-format flag. The issue with that is that there is no way to generate custom flags using gettext and I think that gettext tools will complain about unknown flags.

nijel avatar May 17 '24 06:05 nijel

It makes sense to support extracting Weblate flags from the comments, but the syntax has to be carefully designed. For example, the flags itself can contain quotes, so using quotes to delimit them doesn't sound like a good idea.

nijel avatar May 17 '24 06:05 nijel

This issue has been put aside. It is currently unclear if it will ever be implemented as it seems to cover too narrow of a use case or doesn't seem to fit into Weblate.

Please try to clarify the use case or consider proposing something more generic to make it useful to more users.

github-actions[bot] avatar May 17 '24 06:05 github-actions[bot]

It makes sense to support extracting Weblate flags from the comments, but the syntax has to be carefully designed. For example, the flags itself can contain quotes, so using quotes to delimit them doesn't sound like a good idea.

@nijel Of course, I'm not wedded to the use of quotes as the delimiter. Can you foresee any issues with using curly brackets? Again, I'm not wedded to this.

#.
#. STARTUP TYPEWRITER TEXT
#. ------------------------------------------------------------------
#. EEPROM BLANK weblate-flags{ max-length:12, priority:50 }
msgid "BLANK"
msgstr "VIDE"

#. weblate-flags{ read-only }
msgid "EEPROM"
msgstr ""

Please try to clarify the use case or consider proposing something more generic to make it useful to more users.

The use case would be identical to that which (presumably) warranted the implementation of weblate-flags in XML based formats such as XLIFF.

Of course one could argue "Why not just use XLIFF?", but there are presumably plenty of projects out there which rely on gettext, and which aren't in a position to migrate, that might find the ability to communicate weblate-flags such as read-only, and min-length:N useful?

anubeon avatar Jun 10 '24 16:06 anubeon

In https://github.com/WeblateOrg/weblate/issues/14722 (marked as duplicate with this issue here) I explain the situation for Docbook + ITS. Then flags can be added in different levels, and so there is no designated place the comment where the flags can occur. So some of the suggestions in this issue would not work. My proposal was to scan the entire comment for flags, or alternatively prefix them with flag: like flag:priority:90. My understanding of gettext is limited mostly to Docbook + ITS + Itstool, so I might overlook other considerations or conventions.

nicorikken avatar Apr 29 '25 06:04 nicorikken

ITS content ends up in the extracted comment, this is a snipped from a POT file @nicorikken linked in #14722:

#. (itstool) path: info/title
#. (itstool) path: cover/bridgehead
#. Ampersand character & is escaped for XML source format as &
#.
#. ignore-same
#: Ada_Zangemann.dbk:263
#: Ada_Zangemann.dbk:307
#: Ada_Zangemann.dbk:497
msgid "Ada & Zangemann"
msgstr ""

nijel avatar Apr 29 '25 06:04 nijel

What we need, is to be reliably able to distinguish flags from the other content. Flags like forbidden or read-only can appear in regular comment, so there needs to be a way to detect that some part of the comment is actually meant to be processed as flags. The tags can also contain (quoted) whitespace like replacements:%s:"John Doe".

nijel avatar Apr 29 '25 06:04 nijel

GNU gettext is going to support custom sticky flags in PO files (see https://lists.gnu.org/archive/html/bug-gettext/2025-06/msg00018.html) and I believe this is a better approach to this. Let's see how it evolves.

nijel avatar Jul 01 '25 14:07 nijel

Workaround

As long as gettext doesn’t have it yet, I myself implemented source code comments (here in Python) like

error = 'an error'
# Open Document Warning
# weblate-flags: priority:50
_('Failed to open document from path: {error}').format(error=error)

with a custom comman called after the POT and PO files generation

perl -0777 -i -pe 's/#\. weblate-flags:\s*([^\n]+)\n((?:#.*\n)*)/\2#, \1\n/g;s/(#,\s.*)\n#,\s(.*)/\1, \2/g' po/*pot po/*.po

which leads to the following in the POT file:

#. Open Document Warning
#: src/document.py:44
#, python-brace-format, priority:50
msgid "Failed to open document from path: {error}"
msgstr ""

Implementation in Weblate (if not gettext gets it)

In case, gettext doesn’t get it I’d vote for a very simple format

#. weblate-flags: max-length:12, priority:50

to be directly supported by Weblate. If a comment (#. ) starts with #. weblate-flags:, interpret the comma-separated list after that as additional flags.

mahlzahn avatar Jul 31 '26 14:07 mahlzahn

which leads to the following in the POT file:

#. Open Document Warning
#: src/document.py:44
#, python-brace-format, priority:50
msgid "Failed to open document from path: {error}"
msgstr ""

@nijel But unfortunately, it isn't picked up by Weblate, in the single languages: https://translate.codeberg.org/browse/censor/-/ms/?q=priority%3A50.

Edit: The English original (from the POT file) has it: https://translate.codeberg.org/browse/censor/-/en/?q=priority%3A50. I use Weblate's extension to update PO files https://docs.weblate.org/en/weblate-2026.7.1/admin/addons.html#addon-weblate-gettext-msgmerge.

Edit2: And of course, it is just calling msgmerge and this seems to drop custom flags.

Edit3: If you look now again at https://translate.codeberg.org/browse/censor/-/ms/?q=priority%3A50, there are couple of priority:50 strings, but I updated them using bulk editing, searching for {error} OR "Failed to" OR found. Not a very satisfying workaround.

mahlzahn avatar Jul 31 '26 17:07 mahlzahn