public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Maximiliano Sandoval <m.sandoval@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH proxmox-i18n v2] js generator: extract comments from source
Date: Thu, 31 Aug 2023 13:16:58 +0200	[thread overview]
Message-ID: <20230831111658.98006-1-m.sandoval@proxmox.com> (raw)

Adds a way to extract comments to the resulting .po files loosely
matching xgettext's behavior. Useful for instances like

```js
   // Translators: This is the verb, not the noun
   gettext("Profile");

   // Translators: This would read 'foo bar'
   Ext.String.format(gettext('foo {0}'), 'bar');
```

where the string is not enough to guarantee is a satisfactory
translation.

Do note that two identical messages with different comments will count
as the same message (same msgid) from the point of view of gettext. To
truly differentiate them one would need to support Context via the
pgettext function, see [1].

Unlike xgettext we require comments to start with either `TRANSLATORS`
or `i18n` (the parsing is case-insensitive) simply to avoid filling po
files with potentially confusing comments unrelated to translations.

Caveats:
  - Cannot extract multi-line comments
  - Does not understand comments in the /* foo */ form

[1] https://www.gnu.org/software/gettext/manual/html_node/Contexts.html

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---
 jsgettext.pl | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/jsgettext.pl b/jsgettext.pl
index 7f758fd..1c6c050 100755
--- a/jsgettext.pl
+++ b/jsgettext.pl
@@ -93,7 +93,7 @@ my $href = {
 };
 
 sub extract_msg {
-    my ($filename, $linenr, $line) = @_;
+    my ($filename, $linenr, $line, $comment) = @_;
 
     my $count = 0;
 
@@ -110,8 +110,13 @@ sub extract_msg {
 
 	if (my $po = $href->{$text}) {
 	    $po->reference($po->reference() . " $ref");
+	    if ($po->automatic && $comment) {
+		$po->automatic($po->automatic . "\n" . $comment);
+	    } elsif ($comment) {
+		$po->automatic($comment);
+	    }
 	} else {
-	    $href->{$text} = Locale::PO->new(-msgid=> $text, -reference=> $ref, -msgstr=> '');
+	    $href->{$text} = Locale::PO->new(-msgid=> $text, -reference=> $ref, -msgstr=> '', -automatic=> $comment);
 	}
     }
     die "can't extract gettext message in '$filename' line $linenr\n" if !$count;
@@ -122,10 +127,13 @@ my $sources = find_js_sources($dirs);
 
 foreach my $s (@$sources) {
     open(my $SRC_FH, '<', $s) || die "unable to open file '$s' - $!\n";
+    my $prev_line;
     while(defined(my $line = <$SRC_FH>)) {
 	if ($line =~ m/gettext\s*\(/ && $line !~ m/^\s*function gettext/) {
-	    extract_msg($s, $., $line);
+	    my ($comment) = $prev_line =~ /\/\/\s+((?:translators|i18n).*)/i;
+	    extract_msg($s, $., $line, $comment);
 	}
+	$prev_line = $line;
     }
     close($SRC_FH);
 }
-- 
2.39.2





                 reply	other threads:[~2023-08-31 11:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230831111658.98006-1-m.sandoval@proxmox.com \
    --to=m.sandoval@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal