Skip to content

Python 3: convert translatable strings to use formatted string literals #10384

@josephsl

Description

@josephsl

Hi,

This is something to be researched in 2020:

Background

Over the years, python provided numerous ways to interpolate a string (insert values of variables inside a string literal). The most common ones are percent operator and str.format function, the latter introduced in Python 2.x. In Python 3.6, another string interpolation, called formatted string literals (or "F strings") was introduced that allows directly embedding variable values without resorting to dedicated format function.

Example: object location string:

"Positioned at {xPos}, {yPos}".format(xPos = x, yPos = y)

Python 3.6 and later:

f"Positioned at {x}, {y}"

This change has huge implications for one particular string collection: translatable strings, as we'll see below.

Is your feature request related to a problem? Please describe.

In recent NVDA releases, translatable strings included more understandable variable names as part of the string, or if needed, percent operator is used in strategic places. The most prominent example is object location message, column and row header messages in Microsoft Excel and many others. In order for this to happen, strings must go through a call to format function like so:

_("{appModule} is loaded".format(appModule = object.appName)

Or if a variable is defined, it will need to go through format function like so:

profileName = config.conf.curProfileName
ui.message(_("Currently active profile is {profileName}".format(profileName = profileName)

Note that the above are approximate examples.

Describe the solution you'd like

If possible, convert translatable strings to formatted string literals (F strings) like so:

_(f"{appModuleName} is loaded")

And:

ui.message(_(f"Currently active profile is {profileName}")

Describe alternatives you've considered

Leave them as is.

Additional context

Although it makes code more readable, care must be taken to make sure each string can be converted into formatted string literals. One candidate is an easy to remember variable name that is assigned a meaningful value.

As for implementation strategy, to be discussed in a future comment.

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions