5e7ef210c0
Part of fixing bug #995287 Syncs these two commits from oslo-incubator: Support overriding oslo localedir too Add a gettextutils.install() helper function to get a new gettextutils.install() function which allows the default localedir to be overwritten via an environment variable. Note that gettextutils.install() must be called before any other nova modules are imported since some modules attempt to translate strings at import time (e.g. the 'message' attributes on classes in nova.exception). This is broken and inefficient, but fixing it involves adding something like spinx's l_() function and would be very invaisve. Also, note that calling gettextutils.install() in nova.cmd.__init__ means that no program which uses a different translation domain should ever import any of the modules under nova.cmd. Change-Id: I86562b3a65d371673bb21f7179eecc7602bc0775
38 lines
1.4 KiB
ReStructuredText
38 lines
1.4 KiB
ReStructuredText
Internationalization
|
|
====================
|
|
nova uses `gettext <http://docs.python.org/library/gettext.html>`_ so that
|
|
user-facing strings such as log messages appear in the appropriate
|
|
language in different locales.
|
|
|
|
To use gettext, make sure that the strings passed to the logger are wrapped
|
|
in a ``_()`` function call. For example::
|
|
|
|
LOG.debug(_("block_device_mapping %s"), block_device_mapping)
|
|
|
|
If you have multiple arguments, the convention is to use named parameters.
|
|
It's common to use the ``locals()`` dict (which contains the names and values
|
|
of the local variables in the current scope) to do the string interpolation.
|
|
For example::
|
|
|
|
label = ...
|
|
sr_ref = ...
|
|
LOG.debug(_('Introduced %(label)s as %(sr_ref)s.') % locals())
|
|
|
|
If you do not follow the project conventions, your code may cause the
|
|
LocalizationTestCase.test_multiple_positional_format_placeholders test to fail
|
|
in nova/tests/test_localization.py.
|
|
|
|
The ``_()`` function is brought into the global scope by doing::
|
|
|
|
from nova.openstack.common import gettextutils
|
|
gettextutils.install('nova')
|
|
|
|
These lines are needed in any toplevel script before any nova modules are
|
|
imported. If this code is missing, it may result in an error that looks like::
|
|
|
|
NameError: name '_' is not defined
|
|
|
|
The gettextutils.install() function also queries the NOVA_LOCALEDIR environment
|
|
variable to allow overriding the default localedir with a specific custom
|
|
location for Nova's message catalog.
|