How to properly add context to a translatable string in GNOME Shell?

Hi all.

There is a string in the quick settings menu, “Power Mode”, that appears twice (here, for example), and I would like to translate it in different ways depending on its position.

I know that this means adding a “context” to the translatable string in the source code so that gettext can differentiate the two instances. Now, I neved coded in JavaScript—so all this is probably due to my ignorance—but I figured it was an easy enough change, so I cloned the GNOME Shell repo and tried to do it on my machine (hoping to post a merge request later).

I’m using the toolbox workflow outlined here on the GitLab repo to build and run the modified version of the shell locally, on Fedora 42 (with the latest updates).

The string appears as _('Power Mode’), and I tried two different things to change it.

  1. If I understood the syntax right (see e.g. this line), I should modify it to C_('context1', 'Power Mode’) and C_('context2', 'Power Mode’) in the two different cases (with more meaningful context indicators, of course). When I call xgettext -f po/POTFILES.in to generate the PO template, however, the “Power mode” string disappears from the translatable ones, and ends up in the “#~ strings” at the end of the PO file. To be fair, every other string marked for translation with the C_(#1, #2) function for adding context has the same fate.
  2. From the gettext manual, I read that the pgettext function can be used to add a context to a translatable string. If I use this instead of C_, then the “Power Mode” string correctly appears, with two different contexts, in the PO file. However, the shell doesn’t run correctly anymore because it does not recognize the pgettext function:
GNOME Shell-CRITICAL **: 11:34:36.074: Failed to setup quick settings: ReferenceError: pgettext is not defined

What am I missing here to make it work?

That is correct.

Yes, C_ is not recognized as a default keyword by xgettext. You have to specify it explicitly on the command line, or (easier) use ninja -C path/to/builddir gnome-shell-pot to generate the pot file.

1 Like

Thanks, that did it!