Debian Codegraph @

doc/source/user/guidelines.rst

raw
1 =================================
2 Guidelines for Use in OpenStack
3 =================================
4
5 The OpenStack I18N team has a limited capacity to translate messages,
6 so we want to make their work as effective as possible by identifying
7 the most useful text for them to translate. All text messages *the
8 user sees* via exceptions or API calls should be marked for
9 translation. However, some exceptions are used internally to signal
10 error conditions between modules and are not intended to be presented
11 to the user. Those do not need to be translated. Neither do log
12 messages, as explained below.
13
14 .. seealso::
15
16 * :ref:`usage`
17 * :ref:`api`
18
19 Gettext Contextual Form and Plural Form
20 =======================================
21
22 Sometimes under different contexts, the same word should be
23 translated into different phrases using
24 :py:attr:`TranslatorFactory.contextual_form <oslo_i18n.TranslatorFactory.contextual_form>`.
25
26 And recommend the following code to use contextual form::
27
28 # The contextual translation function using the name "_C"
29 _C = _translators.contextual_form
30
31 ...
32 msg = _C('context', 'string')
33
34 In some languages, sometimes the translated strings are different
35 with different item counts using
36 :py:attr:`TranslatorFactory.plural_form <oslo_i18n.TranslatorFactory.plural_form>`
37
38 And recommend the following code to use plural form::
39
40 # The plural translation function using the name "_P"
41 _P = _translators.plural_form
42
43 ...
44 msg = _P('single', 'plural', count)
45
46 The contextual form and plural form are used only when needed.
47 By default, the translation should use the ``_()``.
48
49 .. note::
50 These two functions were only available in oslo.i18n >= 2.1.0.
51
52 Log Translation
53 ===============
54
55 .. note::
56
57 Starting with the Pike series, OpenStack no longer supports log
58 translation. It is not necessary to add translation instructions to
59 new code, and the instructions can be removed from old code. The
60 following documentation is retained to help developers understand
61 existing usage and how to remove it.
62
63 Support was `dropped
64 <http://lists.openstack.org/pipermail/openstack-dev/2017-March/114191.html>`_
65 primarily based on `feedback from operators
66 <http://lists.openstack.org/pipermail/openstack-operators/2017-March/012953.html>`_
67 that they were not only not needed but also undesirable, because they
68 fragmented the set of web pages providing helpful information about
69 any particular log message, thereby reducing the chances of finding
70 those web pages by doing a web search for the message. Refer to
71 the email thread `understanding log domain change
72 <http://lists.openstack.org/pipermail/openstack-dev/2017-March/thread.html#113365>`_
73 on the openstack-dev mailing list for more details.
74
75 OpenStack previously supported translating some log levels using
76 separate message catalogs, and so has separate marker functions. These
77 well-known names were used by the build system jobs that extracted the
78 messages from the source code and passed them to the translation tool.
79
80 ========== ==========
81 Level Function
82 ========== ==========
83 INFO ``_LI()``
84 WARNING ``_LW()``
85 ERROR ``_LE()``
86 CRITICAL ``_LC()``
87 ========== ==========
88
89 .. note::
90 Debug level log messages were never translated.
91
92
93 Using a Marker Function
94 =======================
95
96 The marker functions are used to mark the translatable strings in the
97 code. The strings are extracted into catalogs using a tool that
98 performs source inspection to look for these specific markers, so the
99 function argument must just be a string.
100
101 For example: **do not do this**::
102
103 # WRONG
104 msg = _(variable_containing_msg)
105
106 Instead, use this style::
107
108 # RIGHT
109 msg = _('My message.')
110
111
112 Choosing a Marker Function
113 ==========================
114
115 The purpose of the different marker functions is to separate the
116 translatable messages into different catalogs, which the translation
117 teams can prioritize translating. It is important to choose the right
118 marker function, to ensure that strings the user sees will be
119 translated and to help the translation team manage their work load.
120
121 Everything marked with ``_()`` will be translated. Prioritizing the
122 catalogs created from strings marked with the log marker functions is
123 up to the individual translation teams and their users, but it is
124 expected that they will work on critical and error messages before
125 warning or info.
126
127 ``_()`` is preferred for any user facing message, even if it is also
128 going to a log file. This ensures that the translated version of the
129 message will be available to the user.
130
131 The log marker functions (``_LI()``, ``_LW()``, ``_LE()``, and
132 ``_LC()``) should no longer be used, and existing usages should be
133 removed. Anytime that the message is passed outside of the current
134 context (for example as part of an exception) the ``_()`` marker
135 function must be used instead.
136
137 A common pattern used to be to define a single message object and use
138 it more than once, for the log call and the exception. In that case,
139 ``_()`` had to be used because the message was going to appear in an
140 exception that may be presented to the user.
141
142 However, now that log messages are no longer translated, it is
143 unfortunately necessary to use two separate strings: a plain one for
144 the log message, and a translatable one for the exception.
145
146 For example, **do not do this**::
147
148 # WRONG
149 msg = _('There was an error.')
150 LOG.error(msg)
151 raise LocalExceptionClass(msg)
152
153 or this::
154
155 # EVEN MORE WRONG
156 msg = _LE('There was an error.')
157 LOG.error(msg)
158 raise LocalExceptionClass(msg)
159
160 Instead, use this style::
161
162 # RIGHT
163 LOG.error('There was an error.')
164 raise LocalExceptionClass(_('An error occurred.'))
165
166
167 Adding Variables to Translated Messages
168 =======================================
169
170 Translated messages should not be combined with other literal strings
171 to create partially translated messages. For example, **do not do
172 this**::
173
174 # WRONG
175 raise ValueError(_('some message') + ': variable=%s' % variable)
176
177 Instead, use this style::
178
179 # RIGHT
180 raise ValueError(_('some message: variable=%s') % variable)
181
182 Including the variable reference inside the translated message allows
183 the translator to take into account grammar rules, differences in
184 left-right vs. right-left rendering, and other factors to make the
185 translated message more useful to the end user.
186
187 Any message with more than one variable should use named interpolation
188 instead of positional, to allow translators to move the variables
189 around in the string to account for differences in grammar and writing
190 direction.
191
192 For example, **do not do this**::
193
194 # WRONG
195 raise ValueError(_('some message: v1=%s v2=%s') % (v1, v2))
196
197 Instead, use this style::
198
199 # RIGHT
200 raise ValueError(_('some message: v1=%(v1)s v2=%(v2)s') % {'v1': v1, 'v2': v2})