This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
As seen below, you usually mark the strings in your application that are
candidates for translation, by wrapping them in a call to the :func:`_`
candidates for translation, by wrapping them in a call to the :func:`!_`
function, like this::
print(_('This string will be translated.'))
For convenience, you want the :func:`_` function to be installed in Python's
For convenience, you want the :func:`!_` function to be installed in Python's
builtins namespace, so it is easily accessible in all modules of your
application.
Expand Down
Expand Up
@@ -276,20 +273,20 @@ are the methods of :class:`!NullTranslations`:
If the *names* parameter is given, it must be a sequence containing the
names of functions you want to install in the builtins namespace in
addition to :func:`_`. Supported names are ``'gettext'``, ``'ngettext'``,
``'pgettext'``, ``'npgettext'``, ``'lgettext'``, and ``'lngettext'``.
addition to :func:`!_`. Supported names are ``'gettext'``, ``'ngettext'``,
``'pgettext'``, and ``'npgettext'``.
Note that this is only one way, albeit the most convenient way, to make
the :func:`_` function available to your application. Because it affects
the :func:`!_` function available to your application. Because it affects
the entire application globally, and specifically the built-in namespace,
localized modules should never install :func:`_`. Instead, they should use
this code to make :func:`_` available to their module::
localized modules should never install :func:`!_`. Instead, they should use
this code to make :func:`!_` available to their module::
import gettext
t = gettext.translation('mymodule', ...)
_ = t.gettext
This puts :func:`_` only in the module's global namespace and so only
This puts :func:`!_` only in the module's global namespace and so only
affects calls within this module.
.. versionchanged:: 3.8
Expand All
@@ -314,7 +311,7 @@ initialize the "protected" :attr:`_charset` instance variable, defaulting to
ids and message strings read from the catalog are converted to Unicode using
this encoding, else ASCII is assumed.
Since message ids are read as Unicode strings too, all :meth:`*gettext` methods
Since message ids are read as Unicode strings too, all ``*gettext()`` methods
will assume message ids as Unicode strings, not byte strings.
The entire set of key/value pairs are placed into a dictionary and set as the
Expand Down
Expand Up
@@ -404,7 +401,7 @@ version has a slightly different API. Its documented usage was::
_ = cat.gettext
print(_('hello world'))
For compatibility with this older module, the function :func:`Catalog` is an
For compatibility with this older module, the function :func:`!Catalog` is an
alias for the :func:`translation` function described above.
One difference between this module and Henstridge's: his catalog objects
Expand Down
Expand Up
@@ -432,7 +429,7 @@ take the following steps:
In order to prepare your code for I18N, you need to look at all the strings in
your files. Any string that needs to be translated should be marked by wrapping
it in ``_('...')`` --- that is, a call to the function :func:`_`. For example::
it in ``_('...')`` --- that is, a call to the function :func:`_ <gettext>`. For example::
filename = 'mylog.txt'
message = _('writing a log message')
Expand Down
Expand Up
@@ -504,7 +501,7 @@ module::
Localizing your application
^^^^^^^^^^^^^^^^^^^^^^^^^^^
If you are localizing your application, you can install the :func:`_` function
If you are localizing your application, you can install the :func:`!_` function
globally into the built-in namespace, usually in the main driver file of your
application. This will let all your application-specific files just use
``_('...')`` without having to explicitly install it in each file.
Expand Down
Expand Up
@@ -581,13 +578,13 @@ Here is one way you can handle this situation::
for a in animals:
print(_(a))
This works because the dummy definition of :func:`_` simply returns the string
This works because the dummy definition of :func:`!_` simply returns the string
unchanged. And this dummy definition will temporarily override any definition
of :func:`_` in the built-in namespace (until the :keyword:`del` command). Take
care, though if you have a previous definition of :func:`_` in the local
of :func:`!_` in the built-in namespace (until the :keyword:`del` command). Take
care, though if you have a previous definition of :func:`!_` in the local
namespace.
Note that the second use of :func:`_` will not identify "a" as being
Note that the second use of :func:`!_` will not identify "a" as being
translatable to the :program:`gettext` program, because the parameter
is not a string literal.
Expand All
@@ -606,13 +603,13 @@ Another way to handle this is with the following example::
print(_(a))
In this case, you are marking translatable strings with the function
:func:`N_`, which won't conflict with any definition of :func:`_`.
:func:`!N_`, which won't conflict with any definition of :func:`!_`.
However, you will need to teach your message extraction program to
look for translatable strings marked with :func:`N_`. :program:`xgettext`,
look for translatable strings marked with :func:`!N_`. :program:`xgettext`,
:program:`pygettext`, ``pybabel extract``, and :program:`xpot` all
support this through the use of the :option:`!-k` command-line switch.
The choice of :func:`N_` here is totally arbitrary; it could have just
as easily been :func:`MarkThisStringForTranslation`.
The choice of :func:`!N_` here is totally arbitrary; it could have just
as easily been :func:`!MarkThisStringForTranslation`.
Acknowledgements
Expand Down
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[3.12] GH-101100: Fix reference warnings for gettext (GH-110115) #110140
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
[3.12] GH-101100: Fix reference warnings for gettext (GH-110115) #110140
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing