| 1 |
.. _usage: |
| 2 |
|
| 3 |
===================================================== |
| 4 |
How to Use oslo.i18n in Your Application or Library |
| 5 |
===================================================== |
| 6 |
|
| 7 |
Installing |
| 8 |
========== |
| 9 |
|
| 10 |
At the command line:: |
| 11 |
|
| 12 |
$ pip install oslo.i18n |
| 13 |
|
| 14 |
.. _integration-module: |
| 15 |
|
| 16 |
Creating an Integration Module |
| 17 |
============================== |
| 18 |
|
| 19 |
To use oslo.i18n in a project (e.g. myapp), you will need to create a |
| 20 |
small integration module to hold an instance of |
| 21 |
:class:`~oslo_i18n.TranslatorFactory` and references to |
| 22 |
the marker functions the factory creates. |
| 23 |
|
| 24 |
.. note:: |
| 25 |
|
| 26 |
Libraries probably do not want to expose the new integration module |
| 27 |
as part of their public API, so rather than naming it |
| 28 |
``myapp.i18n`` it should be called ``myapp._i18n`` to indicate that |
| 29 |
it is a private implementation detail, and not meant to be used |
| 30 |
outside of the library's own code. |
| 31 |
|
| 32 |
.. note:: |
| 33 |
|
| 34 |
Starting with the Pike series, OpenStack no longer supports log |
| 35 |
translation. It is not necessary to add translation instructions to |
| 36 |
new code, and the instructions can be removed from old code. Refer |
| 37 |
to the email thread `understanding log domain change |
| 38 |
<http://lists.openstack.org/pipermail/openstack-dev/2017-March/thread.html#113365>`_ |
| 39 |
on the openstack-dev mailing list for more details. |
| 40 |
|
| 41 |
.. code-block:: python |
| 42 |
|
| 43 |
# myapp/_i18n.py |
| 44 |
|
| 45 |
import oslo_i18n |
| 46 |
|
| 47 |
DOMAIN = "myapp" |
| 48 |
|
| 49 |
_translators = oslo_i18n.TranslatorFactory(domain=DOMAIN) |
| 50 |
|
| 51 |
# The primary translation function using the well-known name "_" |
| 52 |
_ = _translators.primary |
| 53 |
|
| 54 |
# The contextual translation function using the name "_C" |
| 55 |
# requires oslo.i18n >=2.1.0 |
| 56 |
_C = _translators.contextual_form |
| 57 |
|
| 58 |
# The plural translation function using the name "_P" |
| 59 |
# requires oslo.i18n >=2.1.0 |
| 60 |
_P = _translators.plural_form |
| 61 |
|
| 62 |
def get_available_languages(): |
| 63 |
return oslo_i18n.get_available_languages(DOMAIN) |
| 64 |
|
| 65 |
.. TODO: Provide examples for _C and _P |
| 66 |
|
| 67 |
Then, in the rest of your code, use the appropriate marker function |
| 68 |
for each message: |
| 69 |
|
| 70 |
.. code-block:: python |
| 71 |
|
| 72 |
from myapp._i18n import _ |
| 73 |
|
| 74 |
# ... |
| 75 |
|
| 76 |
variable = "openstack" |
| 77 |
some_object.name_msg = _('my name is: %s') % variable |
| 78 |
|
| 79 |
# ... |
| 80 |
|
| 81 |
try: |
| 82 |
|
| 83 |
# ... |
| 84 |
|
| 85 |
except AnException1: |
| 86 |
|
| 87 |
# Log only, log messages are no longer translated |
| 88 |
LOG.exception('exception message') |
| 89 |
|
| 90 |
except AnException2: |
| 91 |
|
| 92 |
# Raise only |
| 93 |
raise RuntimeError(_('exception message')) |
| 94 |
|
| 95 |
else: |
| 96 |
|
| 97 |
# Log and Raise |
| 98 |
msg = _('Unexpected error message') |
| 99 |
LOG.exception(msg) |
| 100 |
raise RuntimeError(msg) |
| 101 |
|
| 102 |
.. note:: |
| 103 |
|
| 104 |
The import of multiple modules from _i18n on a single line is |
| 105 |
a valid exception to |
| 106 |
`OpenStack Style Guidelines <https://docs.openstack.org/hacking/latest/#imports>`_ |
| 107 |
for import statements. |
| 108 |
|
| 109 |
|
| 110 |
It is important to use the marker functions (e.g. _), rather than |
| 111 |
the longer form of the name, because the tool that scans the source |
| 112 |
code for translatable strings looks for the marker function names. |
| 113 |
|
| 114 |
.. warning:: |
| 115 |
|
| 116 |
The old method of installing a version of ``_()`` in the builtins |
| 117 |
namespace is deprecated. Modifying the global namespace affects |
| 118 |
libraries as well as the application, so it may interfere with |
| 119 |
proper message catalog lookups. Calls to |
| 120 |
:func:`gettextutils.install` should be replaced with the |
| 121 |
application or library integration module described here. |
| 122 |
|
| 123 |
|
| 124 |
Handling hacking Objections to Imports |
| 125 |
====================================== |
| 126 |
|
| 127 |
The `OpenStack Style Guidelines <https://docs.openstack.org/hacking/latest/#imports>`_ |
| 128 |
prefer importing modules and accessing names from those modules after |
| 129 |
import, rather than importing the names directly. For example: |
| 130 |
|
| 131 |
:: |
| 132 |
|
| 133 |
# WRONG |
| 134 |
from foo import bar |
| 135 |
|
| 136 |
bar() |
| 137 |
|
| 138 |
# RIGHT |
| 139 |
|
| 140 |
import foo |
| 141 |
|
| 142 |
foo.bar() |
| 143 |
|
| 144 |
The linting tool hacking_ will typically complain about importing |
| 145 |
names from within modules. It is acceptable to bypass this for the |
| 146 |
translation marker functions, because they must have specific names |
| 147 |
and their use pattern is dictated by the message catalog extraction |
| 148 |
tools rather than our style guidelines. To bypass the hacking check |
| 149 |
for imports from this integration module, add an import exception to |
| 150 |
``tox.ini``. |
| 151 |
|
| 152 |
For example:: |
| 153 |
|
| 154 |
# tox.ini |
| 155 |
[hacking] |
| 156 |
import_exceptions = myapp._i18n |
| 157 |
|
| 158 |
.. _hacking: https://pypi.org/project/hacking |
| 159 |
|
| 160 |
.. _lazy-translation: |
| 161 |
|
| 162 |
Lazy Translation |
| 163 |
================ |
| 164 |
|
| 165 |
Lazy translation delays converting a message string to the translated |
| 166 |
form as long as possible, including possibly never if the message is |
| 167 |
not logged or delivered to the user in some other way. It also |
| 168 |
supports logging translated messages in multiple languages, by |
| 169 |
configuring separate log handlers. |
| 170 |
|
| 171 |
Lazy translation is implemented by returning a special object from the |
| 172 |
translation function, instead of a unicode string. That special |
| 173 |
message object supports some, but not all, string manipulation |
| 174 |
APIs. For example, concatenation with addition is not supported, but |
| 175 |
interpolation of variables is supported. Depending on how translated |
| 176 |
strings are used in an application, these restrictions may mean that |
| 177 |
lazy translation cannot be used, and so it is not enabled by default. |
| 178 |
|
| 179 |
To enable lazy translation, call :func:`enable_lazy`. |
| 180 |
|
| 181 |
:: |
| 182 |
|
| 183 |
import oslo_i18n |
| 184 |
|
| 185 |
oslo_i18n.enable_lazy() |
| 186 |
|
| 187 |
Translating Messages |
| 188 |
==================== |
| 189 |
|
| 190 |
Use :func:`~oslo_i18n.translate` to translate strings to |
| 191 |
a specific locale. :func:`translate` handles delayed translation and |
| 192 |
strings that have already been translated immediately. It should be |
| 193 |
used at the point where the locale to be used is known, which is often |
| 194 |
just prior to the message being returned or a log message being |
| 195 |
emitted. |
| 196 |
|
| 197 |
:: |
| 198 |
|
| 199 |
import oslo_i18n |
| 200 |
|
| 201 |
trans_msg = oslo_i18n.translate(msg, my_locale) |
| 202 |
|
| 203 |
If a locale is not specified the default locale is used. |
| 204 |
|
| 205 |
Available Languages |
| 206 |
=================== |
| 207 |
|
| 208 |
Only the languages that have translations provided are available for |
| 209 |
translation. To determine which languages are available the |
| 210 |
:func:`~oslo_i18n.get_available_languages` is provided. The integration |
| 211 |
module provides a domain defined specific function. |
| 212 |
|
| 213 |
.. code-block:: python |
| 214 |
|
| 215 |
import myapp._i18n |
| 216 |
|
| 217 |
languages = myapp._i18n.get_available_languages() |
| 218 |
|
| 219 |
.. seealso:: |
| 220 |
|
| 221 |
* :doc:`guidelines` |
| 222 |
|
| 223 |
Displaying translated messages |
| 224 |
============================== |
| 225 |
|
| 226 |
Several preparations are required to display translated messages in your |
| 227 |
running application. |
| 228 |
|
| 229 |
Preferred language |
| 230 |
You need to specify your preferred language through an environment variable. |
| 231 |
The preferred language can be specified by ``LANGUAGE``, ``LC_ALL``, |
| 232 |
``LC_MESSAGES``, or ``LANGUAGE`` (A former one has a priority). |
| 233 |
|
| 234 |
``oslo_i18n.translate()`` can be used to translate a string to override the |
| 235 |
preferred language. |
| 236 |
|
| 237 |
.. note:: |
| 238 |
|
| 239 |
You need to use ``enable_lazy()`` to override the preferred language |
| 240 |
by using ``oslo_i18n.translate()``. |
| 241 |
|
| 242 |
Locale directory |
| 243 |
Python ``gettext`` looks for binary ``mo`` files for the given domain |
| 244 |
using the path ``<localedir>/<language>/LC_MESSAGES/<domain>.mo``. |
| 245 |
The default locale directory varies on distributions, |
| 246 |
and it is ``/usr/share/locale`` in most cases. |
| 247 |
|
| 248 |
If you store message catalogs in a different location, |
| 249 |
you need to specify the location via an environment variable |
| 250 |
named ``<DOMAIN>_LOCALEDIR`` where ``<DOMAIN>`` is an upper-case |
| 251 |
domain name with replacing ``_`` and ``.`` with ``-``. |
| 252 |
For example, ``NEUTRON_LOCALEDIR`` for a domain ``neutron`` and |
| 253 |
``OSLO_I18N_LOCALEDIR`` for a domain ``oslo_i18n``. |
| 254 |
|
| 255 |
.. note:: |
| 256 |
|
| 257 |
When you specify locale directories via ``<DOMAIN>_LOCALEDIR`` |
| 258 |
environment variables, you need to specify an environment variable per |
| 259 |
domain. More concretely, if your application using a domain ``myapp` |
| 260 |
uses oslo.policy, you need to specify both ``MYAPP_LOCALEDIR`` and |
| 261 |
``OSLO_POLICY_LOCALEDIR`` to ensure that translation messages from |
| 262 |
both your application and oslo.policy are displayed. |