mquinson / po4a

Maintain the translations of your documentation with ease (PO for anything)
http://po4a.org/
GNU General Public License v2.0
124 stars 61 forks source link

Feature request: file globbing #272

Open paususe opened 3 years ago

paususe commented 3 years ago

(Per our discussion on IRC)

Please implement file globbing in po4a configuration files.

Sometimes you have a lot of source files and listing them one by one, and keeping the list updated, is cumbersome and prone to failure.

As an example, in some of the documentation guides in Uyuni we have 170+ source AsciiDoc files. Instead of listing each and every .adoc file under e. g. modules/reference (including subdirectories!):

[type: asciidoc] modules/administration/_attributes.adoc $lang:translations/$lang/modules/administration/_attributes.adoc
[type: asciidoc] modules/administration/pages/live-patching.adoc $lang:translations/$lang/modules/administration/pages/live-patching.adoc
[type: asciidoc] modules/administration/pages/master-fingerprint.adoc $lang:translations/$lang/modules/administration/pages/master-fingerprint.adoc
[type: asciidoc] modules/administration/pages/mirror-sources.adoc $lang:translations/$lang/modules/administration/pages/mirror-sources.adoc
...(imagine 175 lines like that)...

it would be so much easier and convenient to be able to say

[type: asciidoc opt=recurse] modules/administration/*.adoc $lang:translations/$lang/modules/administration/*.adoc

Sample po4a config files here: https://github.com/paususe/uyuni-docs/tree/paususe-po4a-config-file/l10n-weblate

mquinson commented 3 years ago

Hello, thanks for the report.

Indeed, that would be very possible to do, for example using this tutorial. I would probably use this syntax instead (no recurse option which meaning is not clear to me, $master component in the translation filename instead):

[type: asciidoc] modules/administration/*.adoc $lang:translations/$lang/modules/administration/$master

The only question is about when I'll find the time to implement it because things are really crazy IRL for me. Help would be welcome.

I guess that I would search in po4a where the config file is parsed (where the $document hash table is populated), and then, I'd add some code just before the hash initialization, to iterate over all globbed files. I guess it's here: https://github.com/mquinson/po4a/blob/master/po4a#L1197

In that location, I'd detect whether the master file contains a * in its name while the translation file contains a $master in its name (if only one of these conditions is met, make an error message). Something like

  if ($master =~ m/\*/ && $master =~ m/\$master/) # both * and $ need to be protected in the regular expression

If so, populate an array with all the matching file names using the previously cited tutorial.

Then the whole code block that populates the $document hash would need to be enclosed in a loop traversing the previously populated array of all actual master file. Here is an example of such a loop in Perl.

Then, the doc would need to be updated, probably at the end of this paragraph.

And finally, a new test case should be added, for example copying this one into a multidoc directory, renaming all the files from multiple to multidoc, and then adding this new test case in the corresponding test suite description. Or maybe you want to create a new test suite description (simply creating a new similar file which name ends with .t should enroll it in the test suite). Maybe derivating the single test case instead of multiple is better because you don't necessary want to test with multiple languages. I'm not sure. If you want to make the things perfect, you could add several multidoc tests, similarly to the existing multiple or single ones: when nor the PO nor the POT files exist; when only one of them exist; when the PO content needs to be fuzzied according to the master doc; when the translation file does not exist yet; when everything is already uptodate, etc.

petterreinholdtsen commented 2 years ago

For the record, while implementing use of po4a for linuxcnc (https://github.com/LinuxCNC/linuxcnc/pull/1356), I felt the need for such file globbing feature too. :)