Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / lib / node333.html
CommitLineData
86530b38
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">
566.28.3 Internationalizing your programs and modules</A>
57</H2>
58Internationalization (I18N) refers to the operation by which a program
59is made aware of multiple languages. Localization (L10N) refers to
60the adaptation of your program, once internationalized, to the local
61language and cultural habits. In order to provide multilingual
62messages for your Python programs, you need to take the following
63steps:
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>
82In order to prepare your code for I18N, you need to look at all the
83strings in your files. Any string that needs to be translated
84should be marked by wrapping it in <code>_('...')</code> -- that is, a call
85to the function <tt class="function">_()</tt>. For example:
86
87<P>
88<div class="verbatim"><pre>
89filename = 'mylog.txt'
90message = _('writing a log message')
91fp = open(filename, 'w')
92fp.write(message)
93fp.close()
94</pre></div>
95
96<P>
97In this example, the string <code>'writing a log message'</code> is marked as
98a candidate for translation, while the strings <code>'mylog.txt'</code> and
99<code>'w'</code> are not.
100
101<P>
102The Python distribution comes with two tools which help you generate
103the message catalogs once you've prepared your source code. These may
104or may not be available from a binary distribution, but they can be
105found in a source distribution, in the <span class="file">Tools/i18n</span> directory.
106
107<P>
108The <b class="program">pygettext</b><A NAME="tex2html45"
109 HREF="#foot36712"><SUP>6.4</SUP></A> program
110scans all your Python source code looking for the strings you
111previously marked as translatable. It is similar to the GNU
112<b class="program">gettext</b> program except that it understands all the
113intricacies of Python source code, but knows nothing about C or C++
114source code. You don't need GNU <code>gettext</code> unless you're also
115going 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
119message catalog <span class="file">.pot</span> files, essentially structured human
120readable files which contain every marked string in the source code,
121along with a placeholder for the translation strings.
122<b class="program">pygettext</b> is a command line script that supports a similar
123command line interface as <b class="program">xgettext</b>; for details on its use,
124run:
125
126<P>
127<div class="verbatim"><pre>
128pygettext.py --help
129</pre></div>
130
131<P>
132Copies of these <span class="file">.pot</span> files are then handed over to the
133individual human translators who write language-specific versions for
134every supported natural language. They send you back the filled in
135language-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
139machine-readable <span class="file">.mo</span> binary catalog files. The <span class="file">.mo</span>
140files are what the <tt class="module">gettext</tt> module uses for the actual
141translation processing during run-time.
142
143<P>
144How you use the <tt class="module">gettext</tt> module in your code depends on
145whether you are internationalizing your entire application or a single
146module.
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&#231;ois Pinard has
154written a program called
155<b class="program">xpot</b> which does a similar job. It is available as part of
156his <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
163compatible with GNU <b class="program">msgfmt</b> except that it provides a
164simpler, 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
167applications.
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>
224See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
225</ADDRESS>
226</BODY>
227</HTML>