How to mark some strings as translatable?


Python modules generated by the extension wizard are using the ‘_()’ function for strings to be translated.

The simple use case is obvious: _(“T e x t”).

How do we mark formatted strings with runtime place holders?

f"Diameter ({lengthUnit})"

By the way, what about strings in the widget managed by designer? Are they marked as translatable by default? Does the dev need to perform some special steps?

Thank you.

Hi @chir.set -

This document explains the process for internationalizing an extension:

The .ui files should be fine in general with a few caveats described in the document.

For python, we need to avoid the f string interpolation method for translatable strings because the part in {} is executable python code, and we don’t want people to be able to use that to inject incorrect behaviour via the translation files. Instead we need to use the named placeholder method of formatting.

Thank you for your reply.

So, to continue on the above example, we should use :

lengthUnitValue = <result of some processing>
_("Diameter ({lengthUnit})").format(lengthUnit = lengthUnitValue)

according to the link above.

Is that right?

1 Like

Yes, that looks correct.

Also people providing translations should be aware that these placeholders inside the {} blocks need to be left alone or the code can break. Maybe weblate or something warns about this, I’m not sure.

Small nitpick, python style usually omits the spaces around the = sign so you may get style warnings about it.

1 Like

Oops, funny, or not funny, I changed to ’ = ’ on purpose in many places for readability :slight_smile: :smiley: I’ll deal with that in a future maintenance session. Thanks.

Haha, yes, I do find it funny!

The style guide clearly says “A Foolish Consistency is the Hobgoblin of Little Minds” and yet we kind of insist on it anyway : D

@chir.set if the document did not answer any of your questions or not clear enough then please submit a pull request to add/change the text. If needed then of course you can ask due clarification here.


I’m not sure that we have a similar understanding of the problem; I’ll push a branch in SlicerVMTK with changes I have made regarding translation and we’ll discuss in a PR over there.

Yes, please send a pull request to SlicerVMTK extension. I’ll have a look and merge it and then I will create the translation component on Weblate.

Here it is, please consider that the translation commits are based on #107.

You can now add translations for VMTK extension here:

After translating SlicerVMTK to French, I merged #111 to fix some inconsistencies.

Weblate does not seem to pickup updates from the code base on its own. How do the translatable strings get updated in weblate? Is it automated on Slicer’s side? Is it done manually on a scheduled basis?

Thanks for any info.