Re: Translatable Strings

Julien Cochuyt

Dear Noelia,

You're raising here a good point.
The u prefix is indeed useless here, even on Python 2.
The reason for this is that gettext always returns a Unicode string, thus even if the text argument contained non-ASCII characters, the string would be formatted just fine.
I couldn't tell if gettext always behaved that way, and maybe the prefix was needed at the time this line was written.
I do not think that there is any urge in modifying such constructs in the NVDA codebase as it is not technically harmful in any way.

Please however note that if the same formatting operation was to occur without gettext being involved, the u prefix would be required on Python 2.
Eg.: log.debug(u"insertion: {text}".format(text=text))
This is because the text argument might contain non-ANSI characters.
Python 3 version (for newer code): log.debug(f"insertion: {text}")

Best regards,

Julien Cochuyt

Le dim. 3 nov. 2019 à 18:42, Noelia Ruiz <nrm1977@...> a écrit :
Dear Julien, I thought that NVDA contains strings using u" prefix. I
have searched them with git grep -n format( and at least I see this:
> source/NVDAObjects/UIA/                    return _(u"insertion: {text}").format(text=text)

Should this be modified to avoid confusion? This is on master branch.
Cheers and thanks for your precise answer, which is very useful.

El 03/11/2019 a las 17:41, Julien Cochuyt escribió:
> Dear Nathaniel,
> Indeed, for translatable strings, you still have to use the _("text")
> construct.
> The underscore is a global alias to the gettext function which is
> responsible for substituting the appropriate translation.
> On the other hand, the u prefix in u"text" denotes a Unicode string.
> It must be used in Python 2 as soon as your string literal contains non
> ANSI characters.
> In Python 3, all strings are Unicode. The u prefix is still supported
> for backward compatibility, though.
> Note: If the English version of the string literal contains only ANSI
> characters but a given translation requires Unicode, you still can
> safely use _("text") without the u prefix even on Python 2, as gettext
> will return the translated Unicode string just fine.
> Best regards,
> Julien Cochuyt
> Accessolutions
> Le dim. 3 nov. 2019 à 13:07, Nathaniel Schmidt <schmidty2244@...
> <mailto:schmidty2244@...>> a écrit :
>     Hi all,____
>     __ __
>     I am currently writing an app module and have only just started
>     re-consulting the NVDA source code for relevant information I need.
>     I say "re-consulting" because the last time I did this was somewhere
>     between 2012-2014, so much of my previous knowledge has simply not
>     been retained in my memory, or if any of it has been retained, it
>     will be based on old, potentially unusable info.  Forgive my
>     ignorance but I am just looking for some clarification regarding the
>     currently-used convention for using translatable strings, as I
>     understand that it may have substantially changed.  Can I still use
>     the previously implemented _("text"), or can text now simply be used
>     as Python2-3-compatible Unicode strings i.e. u"text"?
>     __ __
>     Thanks in advance and kind regards,
>     __ __
>     Nathaniel Schmidt

Join to automatically receive all group messages.