public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pve-devel] [PATCH proxmox-i18n] js generator: try to extract comments from source
@ 2023-08-30 12:48 Maximiliano Sandoval
  2023-08-30 13:23 ` Thomas Lamprecht
  0 siblings, 1 reply; 4+ messages in thread
From: Maximiliano Sandoval @ 2023-08-30 12:48 UTC (permalink / raw)
  To: pve-devel

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

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

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

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, see [1].

Caveats:
  - Cannot extract multiline comments
  - Does not understand comments in the /* comment */ form

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

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

diff --git a/jsgettext.pl b/jsgettext.pl
index 7f758fd..d0bf7a9 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;
 
@@ -111,7 +111,7 @@ sub extract_msg {
 	if (my $po = $href->{$text}) {
 	    $po->reference($po->reference() . " $ref");
 	} 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 +122,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+(.*)/;
+	    extract_msg($s, $., $line, $comment);
 	}
+	$prev_line = $line;
     }
     close($SRC_FH);
 }
-- 
2.39.2





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-08-30 15:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-30 12:48 [pve-devel] [PATCH proxmox-i18n] js generator: try to extract comments from source Maximiliano Sandoval
2023-08-30 13:23 ` Thomas Lamprecht
2023-08-30 13:49   ` Maximiliano Sandoval
2023-08-30 15:11   ` Maximiliano Sandoval

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