Translations don't happen when set as default argument

We are currently trying to convert our application using wxPython’s support for locale. However, we have a situation where we have text that is provided as a default argument to a function that we want to translate, but the translation doesn’t happen.

For example,

def AddPage(self, title=_("Empty Page")):
    ...    

If we have a translation for “Empty Page”, the program still uses the raw English version. I’m assuming this is simply due to the way that these arguments are determined during runtime.

For a complete working example of the problem, I have a zip file with the code here: https://drive.google.com/file/d/1X3UZ4ThGhmBcU9cFk0letTEkmWLL_Tu5/view?usp=sharing

In that example, we have “Sample Title Text English” translated to German (fake translation lol, its translated to “Sample Title Text German”), and the app defaults to German locale. PrintTranslation() is set up with a default of _("Sample Title Text English") and then should print that text whenever it’s called. However, when that function is called without any parameters, the English version prints out instead of the German version.

The only workaround that I know of would be change the function to something like this:

    def PrintTranslation(self, text=None):
        text = text or _("Sample Title Text English")
        print(text)

This works, but I’m wondering if there’s a better, or more conventional, way of handling this?

I am not 100% positively sure here, but since functions arguments in Python are evaluated at function definition (I.e., when the “def” is encountered) it might be that the evaluation happens before the settings of the locale (or whatever machinery is needed to make wx.GetTranslation work). Did you try with something else than wx.GetTranslation? gettext maybe?

In any case, the concept of having a function call as default argument seems very alien to me, I very much prefer your second approach.

Andrea.

I am 100% sure that you are correct. :smile: