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="node339.html" /> | |
13 | <link rel="prev" href="node328.html" /> | |
14 | <link rel="parent" href="module-gettext.html" /> | |
15 | <link rel="next" href="node334.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.28.3 Internationalizing your programs and modules</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.2.4 The Catalog constructor" | |
25 | href="node332.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 gettext " | |
28 | href="module-gettext.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.1 Localizing your module" | |
31 | href="node334.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="node332.html">6.28.2.4 The Catalog constructor</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="module-gettext.html">6.28 gettext </A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="node334.html">6.28.3.1 Localizing your module</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION0082830000000000000000"> | |
56 | 6.28.3 Internationalizing your programs and modules</A> | |
57 | </H2> | |
58 | Internationalization (I18N) refers to the operation by which a program | |
59 | is made aware of multiple languages. Localization (L10N) refers to | |
60 | the adaptation of your program, once internationalized, to the local | |
61 | language and cultural habits. In order to provide multilingual | |
62 | messages for your Python programs, you need to take the following | |
63 | steps: | |
64 | ||
65 | <P> | |
66 | ||
67 | <OL> | |
68 | <LI>prepare your program or module by specially marking | |
69 | translatable strings | |
70 | </LI> | |
71 | <LI>run a suite of tools over your marked files to generate raw | |
72 | messages catalogs | |
73 | </LI> | |
74 | <LI>create language specific translations of the message catalogs | |
75 | </LI> | |
76 | <LI>use the <tt class="module">gettext</tt> module so that message strings are | |
77 | properly translated | |
78 | </LI> | |
79 | </OL> | |
80 | ||
81 | <P> | |
82 | In order to prepare your code for I18N, you need to look at all the | |
83 | strings in your files. Any string that needs to be translated | |
84 | should be marked by wrapping it in <code>_('...')</code> -- that is, a call | |
85 | to the function <tt class="function">_()</tt>. For example: | |
86 | ||
87 | <P> | |
88 | <div class="verbatim"><pre> | |
89 | filename = 'mylog.txt' | |
90 | message = _('writing a log message') | |
91 | fp = open(filename, 'w') | |
92 | fp.write(message) | |
93 | fp.close() | |
94 | </pre></div> | |
95 | ||
96 | <P> | |
97 | In this example, the string <code>'writing a log message'</code> is marked as | |
98 | a candidate for translation, while the strings <code>'mylog.txt'</code> and | |
99 | <code>'w'</code> are not. | |
100 | ||
101 | <P> | |
102 | The Python distribution comes with two tools which help you generate | |
103 | the message catalogs once you've prepared your source code. These may | |
104 | or may not be available from a binary distribution, but they can be | |
105 | found in a source distribution, in the <span class="file">Tools/i18n</span> directory. | |
106 | ||
107 | <P> | |
108 | The <b class="program">pygettext</b><A NAME="tex2html45" | |
109 | HREF="#foot36712"><SUP>6.4</SUP></A> program | |
110 | scans all your Python source code looking for the strings you | |
111 | previously marked as translatable. It is similar to the GNU | |
112 | <b class="program">gettext</b> program except that it understands all the | |
113 | intricacies of Python source code, but knows nothing about C or C++ | |
114 | source code. You don't need GNU <code>gettext</code> unless you're also | |
115 | going to be translating C code (such as C extension modules). | |
116 | ||
117 | <P> | |
118 | <b class="program">pygettext</b> generates textual Uniforum-style human readable | |
119 | message catalog <span class="file">.pot</span> files, essentially structured human | |
120 | readable files which contain every marked string in the source code, | |
121 | along with a placeholder for the translation strings. | |
122 | <b class="program">pygettext</b> is a command line script that supports a similar | |
123 | command line interface as <b class="program">xgettext</b>; for details on its use, | |
124 | run: | |
125 | ||
126 | <P> | |
127 | <div class="verbatim"><pre> | |
128 | pygettext.py --help | |
129 | </pre></div> | |
130 | ||
131 | <P> | |
132 | Copies of these <span class="file">.pot</span> files are then handed over to the | |
133 | individual human translators who write language-specific versions for | |
134 | every supported natural language. They send you back the filled in | |
135 | language-specific versions as a <span class="file">.po</span> file. Using the | |
136 | <b class="program">msgfmt.py</b><A NAME="tex2html46" | |
137 | HREF="#foot36713"><SUP>6.5</SUP></A> program (in the <span class="file">Tools/i18n</span> directory), you take the | |
138 | <span class="file">.po</span> files from your translators and generate the | |
139 | machine-readable <span class="file">.mo</span> binary catalog files. The <span class="file">.mo</span> | |
140 | files are what the <tt class="module">gettext</tt> module uses for the actual | |
141 | translation processing during run-time. | |
142 | ||
143 | <P> | |
144 | How you use the <tt class="module">gettext</tt> module in your code depends on | |
145 | whether you are internationalizing your entire application or a single | |
146 | module. | |
147 | ||
148 | <P> | |
149 | <BR><HR><H4>Footnotes</H4> | |
150 | <DL> | |
151 | <DT><A NAME="foot36712">...pygettext</A><A | |
152 | HREF="node333.html#tex2html45"><SUP>6.4</SUP></A></DT> | |
153 | <DD>François Pinard has | |
154 | written a program called | |
155 | <b class="program">xpot</b> which does a similar job. It is available as part of | |
156 | his <b class="program">po-utils</b> package at | |
157 | <a class="url" href="http://www.iro.umontreal.ca/contrib/po-utils/HTML/">http://www.iro.umontreal.ca/contrib/po-utils/HTML/</a>. | |
158 | ||
159 | </DD> | |
160 | <DT><A NAME="foot36713">...msgfmt.py</A><A | |
161 | HREF="node333.html#tex2html46"><SUP>6.5</SUP></A></DT> | |
162 | <DD><b class="program">msgfmt.py</b> is binary | |
163 | compatible with GNU <b class="program">msgfmt</b> except that it provides a | |
164 | simpler, all-Python implementation. With this and | |
165 | <b class="program">pygettext.py</b>, you generally won't need to install the GNU | |
166 | <b class="program">gettext</b> package to internationalize your Python | |
167 | applications. | |
168 | ||
169 | </DD> | |
170 | </DL> | |
171 | <p><br /></p><hr class='online-navigation' /> | |
172 | <div class='online-navigation'> | |
173 | <!--Table of Child-Links--> | |
174 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
175 | ||
176 | <UL CLASS="ChildLinks"> | |
177 | <LI><A href="node334.html">6.28.3.1 Localizing your module</a> | |
178 | <LI><A href="node335.html">6.28.3.2 Localizing your application</a> | |
179 | <LI><A href="node336.html">6.28.3.3 Changing languages on the fly</a> | |
180 | <LI><A href="node337.html">6.28.3.4 Deferred translations</a> | |
181 | <LI><A href="node338.html">6.28.3.5 <tt class="function">gettext()</tt> vs. <tt class="function">lgettext()</tt></a> | |
182 | </ul> | |
183 | <!--End of Table of Child-Links--> | |
184 | </div> | |
185 | ||
186 | <DIV CLASS="navigation"> | |
187 | <div class='online-navigation'> | |
188 | <p></p><hr /> | |
189 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
190 | <tr> | |
191 | <td class='online-navigation'><a rel="prev" title="6.28.2.4 The Catalog constructor" | |
192 | href="node332.html"><img src='../icons/previous.png' | |
193 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
194 | <td class='online-navigation'><a rel="parent" title="6.28 gettext " | |
195 | href="module-gettext.html"><img src='../icons/up.png' | |
196 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
197 | <td class='online-navigation'><a rel="next" title="6.28.3.1 Localizing your module" | |
198 | href="node334.html"><img src='../icons/next.png' | |
199 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
200 | <td align="center" width="100%">Python Library Reference</td> | |
201 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
202 | href="contents.html"><img src='../icons/contents.png' | |
203 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
204 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
205 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
206 | <td class='online-navigation'><a rel="index" title="Index" | |
207 | href="genindex.html"><img src='../icons/index.png' | |
208 | border='0' height='32' alt='Index' width='32' /></A></td> | |
209 | </tr></table> | |
210 | <div class='online-navigation'> | |
211 | <b class="navlabel">Previous:</b> | |
212 | <a class="sectref" rel="prev" href="node332.html">6.28.2.4 The Catalog constructor</A> | |
213 | <b class="navlabel">Up:</b> | |
214 | <a class="sectref" rel="parent" href="module-gettext.html">6.28 gettext </A> | |
215 | <b class="navlabel">Next:</b> | |
216 | <a class="sectref" rel="next" href="node334.html">6.28.3.1 Localizing your module</A> | |
217 | </div> | |
218 | </div> | |
219 | <hr /> | |
220 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
221 | </DIV> | |
222 | <!--End of Navigation Panel--> | |
223 | <ADDRESS> | |
224 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
225 | </ADDRESS> | |
226 | </BODY> | |
227 | </HTML> |