Can you call the gettext functions in file scope in gnome 45?

In my gnome-shell extension I have a ton of code that invokes the gettext function in file scope like this:

const baz = {
    a: _('foo'),
    b: _('bar'),
}

This appears to be broken in gnome 45. My guess is that the _ function is not initted until after
the enable function runs.

This same issues appears to happen for the Extension.lookupByUUID which also seems to return null if it’s invoked at file scope. What’s going on here?

Here is an example in the extension of where I’m invoking the gettext function:

What can be done about this?

That’s correct, the translations are initialized along with the extension.

The easiest way is just to ensure those calls happen at “run-time”. Everything is run-time in JavaScript, but we can contrast this with initialization-time for extensions.

Assuming you want to keep using those classes the same way, you could define getters in a way you find convenient:

const Phase = {
    get pomodoro() { return _('Pomodoro'); }
    get long_break() { return _('Long Break'); }
    get short_break() { return _('Short Break'); }
} as const;

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.