Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="node338.html" /> | |
13 | <link rel="prev" href="node336.html" /> | |
14 | <link rel="parent" href="node333.html" /> | |
15 | <link rel="next" href="node338.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.28.3.4 Deferred translations</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="6.28.3.3 Changing languages on" | |
25 | href="node336.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="6.28.3 Internationalizing your programs" | |
28 | href="node333.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="6.28.3.5 gettext() vs. lgettext()" | |
31 | href="node338.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="node336.html">6.28.3.3 Changing languages on</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="node333.html">6.28.3 Internationalizing your programs</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="node338.html">6.28.3.5 gettext() vs. lgettext()</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H3><A NAME="SECTION0082834000000000000000"> | |
56 | 6.28.3.4 Deferred translations</A> | |
57 | </H3> | |
58 | ||
59 | <P> | |
60 | In most coding situations, strings are translated where they are coded. | |
61 | Occasionally however, you need to mark strings for translation, but | |
62 | defer actual translation until later. A classic example is: | |
63 | ||
64 | <P> | |
65 | <div class="verbatim"><pre> | |
66 | animals = ['mollusk', | |
67 | 'albatross', | |
68 | 'rat', | |
69 | 'penguin', | |
70 | 'python', | |
71 | ] | |
72 | # ... | |
73 | for a in animals: | |
74 | print a | |
75 | </pre></div> | |
76 | ||
77 | <P> | |
78 | Here, you want to mark the strings in the <code>animals</code> list as being | |
79 | translatable, but you don't actually want to translate them until they | |
80 | are printed. | |
81 | ||
82 | <P> | |
83 | Here is one way you can handle this situation: | |
84 | ||
85 | <P> | |
86 | <div class="verbatim"><pre> | |
87 | def _(message): return message | |
88 | ||
89 | animals = [_('mollusk'), | |
90 | _('albatross'), | |
91 | _('rat'), | |
92 | _('penguin'), | |
93 | _('python'), | |
94 | ] | |
95 | ||
96 | del _ | |
97 | ||
98 | # ... | |
99 | for a in animals: | |
100 | print _(a) | |
101 | </pre></div> | |
102 | ||
103 | <P> | |
104 | This works because the dummy definition of <tt class="function">_()</tt> simply returns | |
105 | the string unchanged. And this dummy definition will temporarily | |
106 | override any definition of <tt class="function">_()</tt> in the built-in namespace | |
107 | (until the <tt class="keyword">del</tt> command). | |
108 | Take care, though if you have a previous definition of <tt class="function">_</tt> in | |
109 | the local namespace. | |
110 | ||
111 | <P> | |
112 | Note that the second use of <tt class="function">_()</tt> will not identify ``a'' as | |
113 | being translatable to the <b class="program">pygettext</b> program, since it is not | |
114 | a string. | |
115 | ||
116 | <P> | |
117 | Another way to handle this is with the following example: | |
118 | ||
119 | <P> | |
120 | <div class="verbatim"><pre> | |
121 | def N_(message): return message | |
122 | ||
123 | animals = [N_('mollusk'), | |
124 | N_('albatross'), | |
125 | N_('rat'), | |
126 | N_('penguin'), | |
127 | N_('python'), | |
128 | ] | |
129 | ||
130 | # ... | |
131 | for a in animals: | |
132 | print _(a) | |
133 | </pre></div> | |
134 | ||
135 | <P> | |
136 | In this case, you are marking translatable strings with the function | |
137 | <tt class="function">N_()</tt>,<A NAME="tex2html47" | |
138 | HREF="#foot36714"><SUP>6.6</SUP></A> which won't conflict with any definition of | |
139 | <tt class="function">_()</tt>. However, you will need to teach your message extraction | |
140 | program to look for translatable strings marked with <tt class="function">N_()</tt>. | |
141 | <b class="program">pygettext</b> and <b class="program">xpot</b> both support this through the | |
142 | use of command line switches. | |
143 | ||
144 | <P> | |
145 | <BR><HR><H4>Footnotes</H4> | |
146 | <DL> | |
147 | <DT><A NAME="foot36714">...N_(),</A><A | |
148 | HREF="node337.html#tex2html47"><SUP>6.6</SUP></A></DT> | |
149 | <DD>The choice of <tt class="function">N_()</tt> here is totally | |
150 | arbitrary; it could have just as easily been | |
151 | <tt class="function">MarkThisStringForTranslation()</tt>. | |
152 | ||
153 | ||
154 | </DD> | |
155 | </DL> | |
156 | <DIV CLASS="navigation"> | |
157 | <div class='online-navigation'> | |
158 | <p></p><hr /> | |
159 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
160 | <tr> | |
161 | <td class='online-navigation'><a rel="prev" title="6.28.3.3 Changing languages on" | |
162 | href="node336.html"><img src='../icons/previous.png' | |
163 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
164 | <td class='online-navigation'><a rel="parent" title="6.28.3 Internationalizing your programs" | |
165 | href="node333.html"><img src='../icons/up.png' | |
166 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
167 | <td class='online-navigation'><a rel="next" title="6.28.3.5 gettext() vs. lgettext()" | |
168 | href="node338.html"><img src='../icons/next.png' | |
169 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
170 | <td align="center" width="100%">Python Library Reference</td> | |
171 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
172 | href="contents.html"><img src='../icons/contents.png' | |
173 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
174 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
175 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
176 | <td class='online-navigation'><a rel="index" title="Index" | |
177 | href="genindex.html"><img src='../icons/index.png' | |
178 | border='0' height='32' alt='Index' width='32' /></A></td> | |
179 | </tr></table> | |
180 | <div class='online-navigation'> | |
181 | <b class="navlabel">Previous:</b> | |
182 | <a class="sectref" rel="prev" href="node336.html">6.28.3.3 Changing languages on</A> | |
183 | <b class="navlabel">Up:</b> | |
184 | <a class="sectref" rel="parent" href="node333.html">6.28.3 Internationalizing your programs</A> | |
185 | <b class="navlabel">Next:</b> | |
186 | <a class="sectref" rel="next" href="node338.html">6.28.3.5 gettext() vs. lgettext()</A> | |
187 | </div> | |
188 | </div> | |
189 | <hr /> | |
190 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
191 | </DIV> | |
192 | <!--End of Navigation Panel--> | |
193 | <ADDRESS> | |
194 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
195 | </ADDRESS> | |
196 | </BODY> | |
197 | </HTML> |