From: Kefu Chai <k.chai@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH v3 i18n 1/1] add pgettext() and npgettext() support for context-aware translations
Date: Fri, 8 May 2026 08:36:05 +0800 [thread overview]
Message-ID: <20260508003605.2724653-2-k.chai@proxmox.com> (raw)
In-Reply-To: <20260508003605.2724653-1-k.chai@proxmox.com>
Context-aware translations (msgctxt) allow different translations for
the same English word depending on usage context, e.g. "Monitor" for
Ceph versus QEMU monitors.
Changes to po2js.pl:
- Add fnv31a_ctxt() to hash context+msgid combinations using the
standard gettext EOT separator ("\x04") between context and msgid.
Messages without context continue to use hash(msgid) unchanged.
- Switch from load_file_ashash to load_file_asarray to handle multiple
msgid entries that share the same string but carry different msgctxt.
- Add pgettext(context, msgid) and npgettext(context, singular, plural,
n) JavaScript functions to the generated catalog.
The Makefile gains --keyword=npgettext:1c,2,3 for xgettext. pgettext is
already a default keyword for JavaScript in xgettext, but npgettext is
not. The GNU gettext manual [1] lists default keywords per language:
For C/C++: ..., pgettext:1c,2, npgettext:1c,2,3, ...
For JavaScript/TypeScript: ..., pgettext:1c,2
(npgettext is absent)
This was a deliberate decision in gettext 0.18.3 (2013), which added
initial JavaScript support but only included functions common in Gjs and
popular JS libraries at the time. Without the explicit keyword flag,
npgettext() calls are silently ignored during 'make update_pot'.
The companion patches adding default pgettext/npgettext stubs to the UI
templates (pve-manager, proxmox-backup, pmg-gui) have now landed, so
this revision bumps the relevant submodule pointers to pick them up.
References:
[1] https://www.gnu.org/software/gettext/manual/html_node/xgettext-Invocation.html
Signed-off-by: Kefu Chai <k.chai@proxmox.com>
---
Makefile | 1 +
pmg-gui | 2 +-
po2js.pl | 69 +++++++++++++++++++++++++++++++++++++++++++-------
proxmox-backup | 2 +-
pve-manager | 2 +-
5 files changed, 64 insertions(+), 12 deletions(-)
diff --git a/Makefile b/Makefile
index 16aab32..e1d9633 100644
--- a/Makefile
+++ b/Makefile
@@ -155,6 +155,7 @@ define potupdate
--package-version="$(shell cd $(2);git rev-parse HEAD)" \
--msgid-bugs-address="<support@proxmox.com>" \
--copyright-holder="Copyright (C) Proxmox Server Solutions GmbH <support@proxmox.com> & the translation contributors." \
+ --keyword=npgettext:1c,2,3 \
--output="$(1)".pot
endef
diff --git a/pmg-gui b/pmg-gui
index 3d74a47..3467392 160000
--- a/pmg-gui
+++ b/pmg-gui
@@ -1 +1 @@
-Subproject commit 3d74a47a4c7255c986486fcd4134c70668057de4
+Subproject commit 346739228ca40ca313a26cba686f8f757e349d7a
diff --git a/po2js.pl b/po2js.pl
index 316c0bd..4b7b044 100755
--- a/po2js.pl
+++ b/po2js.pl
@@ -8,10 +8,6 @@ use Getopt::Long;
use JSON;
use Locale::PO;
-# current limits:
-# - we do not support plural. forms
-# - no message content support
-
my $options = {};
GetOptions($options, 't=s', 'o=s', 'v=s') or die "unable to parse options\n";
@@ -34,6 +30,15 @@ sub fnv31a {
return $hval & 0x7fffffff;
}
+# Hash function for messages with context
+sub fnv31a_ctxt {
+ my ($msgctxt, $msgid) = @_;
+ return fnv31a($msgid) if !length($msgctxt); # Empty context = no context
+ # Use EOT character (0x04) as separator, standard in gettext
+ my $combined = $msgctxt . "\x04" . $msgid;
+ return fnv31a($combined);
+}
+
my $catalog = {};
my $plurals_catalog = {};
@@ -41,11 +46,20 @@ my $nplurals = 2;
my $plural_forms = "n!=1";
foreach my $filename (@ARGV) {
- my $href = Locale::PO->load_file_ashash($filename)
+ my $aref = Locale::PO->load_file_asarray($filename)
|| die "unable to load '$filename'\n";
my $charset;
- my $hpo = $href->{'""'} || die "no header";
+ # Find header entry (msgid "")
+ my $hpo;
+ foreach my $po (@$aref) {
+ if ($po->msgid eq '""') {
+ $hpo = $po;
+ last;
+ }
+ }
+ die "no header" if !$hpo;
+
my $header = $hpo->dequote($hpo->msgstr);
if ($header =~ m|^Content-Type:\s+text/plain;\s+charset=(\S+)$|im) {
$charset = $1;
@@ -58,8 +72,7 @@ foreach my $filename (@ARGV) {
$plural_forms = $2;
}
- foreach my $k (keys %$href) {
- my $po = $href->{$k};
+ foreach my $po (@$aref) {
next if $po->fuzzy(); # skip fuzzy entries
my $ref = $po->reference();
@@ -76,9 +89,18 @@ foreach my $filename (@ARGV) {
my $qmsgid_plural = decode($charset, $po->msgid_plural);
my $msgid_plural = $po->dequote($qmsgid_plural);
+ # Extract message context if present
+ my $msgctxt = '';
+ if (defined($po->msgctxt)) {
+ my $qmsgctxt = decode($charset, $po->msgctxt);
+ $msgctxt = $po->dequote($qmsgctxt);
+ }
+
next if !length($msgid) && !length($msgid_plural); # skip header
- my $digest = fnv31a($msgid);
+ my $digest = length($msgctxt) > 0
+ ? fnv31a_ctxt($msgctxt, $msgid)
+ : fnv31a($msgid);
die "duplicate digest" if $catalog->{$digest};
@@ -150,6 +172,35 @@ function ngettext(singular, plural, n) {
}
return translation[msg_idx];
}
+
+function fnv31a_ctxt(context, text) {
+ // Use EOT character (0x04) as separator
+ var combined = context + "\\x04" + text;
+ return fnv31a(combined);
+}
+
+function pgettext(context, msgid) {
+ var digest = fnv31a_ctxt(context, msgid);
+ var data = __proxmox_i18n_msgcat__[digest];
+ if (!data) {
+ return msgid; // Return msgid (not context) as fallback
+ }
+ return data[0] || msgid;
+}
+
+function npgettext(context, singular, plural, n) {
+ const msg_idx = Number($plural_forms);
+ const digest = fnv31a_ctxt(context, singular);
+ const translation = __proxmox_i18n_plurals_msgcat__[digest];
+ if (!translation || msg_idx >= translation.length) {
+ if (n === 1) {
+ return singular;
+ } else {
+ return plural;
+ }
+ }
+ return translation[msg_idx];
+}
__EOD
if ($outfile) {
diff --git a/proxmox-backup b/proxmox-backup
index 753a0e1..eadde76 160000
--- a/proxmox-backup
+++ b/proxmox-backup
@@ -1 +1 @@
-Subproject commit 753a0e189016957896a2f51d7909d35bb686dad9
+Subproject commit eadde76c7cc4b58722306224a80ad309e0f892ee
diff --git a/pve-manager b/pve-manager
index 3e0b640..9aa9263 160000
--- a/pve-manager
+++ b/pve-manager
@@ -1 +1 @@
-Subproject commit 3e0b6409de49d3e1e06d5ac5c9017d4e7407370c
+Subproject commit 9aa9263fd66c970cfe7ee0a0c6961118a40d39a0
--
2.47.3
next prev parent reply other threads:[~2026-05-08 0:36 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-05 10:16 [PATCH proxmox-i18n v1] add pgettext() and npgettext() support for context-aware translations Kefu Chai
2026-02-05 13:15 ` Maximiliano Sandoval
2026-02-06 5:44 ` Kefu Chai
2026-02-06 5:47 ` [PATCH proxmox-i18n v2] " Kefu Chai
2026-02-17 15:45 ` Maximiliano Sandoval
2026-02-28 4:00 ` Kefu Chai
2026-03-02 8:15 ` Maximiliano Sandoval
2026-03-12 5:32 ` Kefu Chai
2026-05-08 0:36 ` [PATCH v3 i18n 0/1] " Kefu Chai
2026-05-08 0:36 ` Kefu Chai [this message]
2026-05-08 0:36 ` superseded: [PATCH v2 i18n 1/1] " Kefu Chai
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=20260508003605.2724653-2-k.chai@proxmox.com \
--to=k.chai@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.