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="node331.html" /> | |
13 | <link rel="prev" href="node329.html" /> | |
14 | <link rel="parent" href="node328.html" /> | |
15 | <link rel="next" href="node331.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.28.2.2 The GNUTranslations class</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.1 The NullTranslations class" | |
25 | href="node329.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.2 Class-based API" | |
28 | href="node328.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.2.3 Solaris message catalog" | |
31 | href="node331.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="node329.html">6.28.2.1 The NullTranslations class</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="node328.html">6.28.2 Class-based API</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="node331.html">6.28.2.3 Solaris message catalog</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H3><A NAME="SECTION0082822000000000000000"> | |
56 | 6.28.2.2 The <tt class="class">GNUTranslations</tt> class</A> | |
57 | </H3> | |
58 | ||
59 | <P> | |
60 | The <tt class="module">gettext</tt> module provides one additional class derived from | |
61 | <tt class="class">NullTranslations</tt>: <tt class="class">GNUTranslations</tt>. This class | |
62 | overrides <tt class="method">_parse()</tt> to enable reading GNU <b class="program">gettext</b> | |
63 | format <span class="file">.mo</span> files in both big-endian and little-endian format. | |
64 | It also coerces both message ids and message strings to Unicode. | |
65 | ||
66 | <P> | |
67 | <tt class="class">GNUTranslations</tt> parses optional meta-data out of the | |
68 | translation catalog. It is convention with GNU <b class="program">gettext</b> to | |
69 | include meta-data as the translation for the empty string. This | |
70 | meta-data is in <a class="rfc" id='rfcref-88910' xml:id='rfcref-88910' | |
71 | href="http://www.faqs.org/rfcs/rfc822.html">RFC 822</a>-style <code>key: value</code> pairs, and should | |
72 | contain the <code>Project-Id-Version</code> key. If the key | |
73 | <code>Content-Type</code> is found, then the <code>charset</code> property is used | |
74 | to initialize the ``protected'' <tt class="member">_charset</tt> instance variable, | |
75 | defaulting to <code>None</code> if not found. If the charset encoding is | |
76 | specified, then all message ids and message strings read from the | |
77 | catalog are converted to Unicode using this encoding. The | |
78 | <tt class="method">ugettext()</tt> method always returns a Unicode, while the | |
79 | <tt class="method">gettext()</tt> returns an encoded 8-bit string. For the message | |
80 | id arguments of both methods, either Unicode strings or 8-bit strings | |
81 | containing only US-ASCII characters are acceptable. Note that the | |
82 | Unicode version of the methods (i.e. <tt class="method">ugettext()</tt> and | |
83 | <tt class="method">ungettext()</tt>) are the recommended interface to use for | |
84 | internationalized Python programs. | |
85 | ||
86 | <P> | |
87 | The entire set of key/value pairs are placed into a dictionary and set | |
88 | as the ``protected'' <tt class="member">_info</tt> instance variable. | |
89 | ||
90 | <P> | |
91 | If the <span class="file">.mo</span> file's magic number is invalid, or if other problems | |
92 | occur while reading the file, instantiating a <tt class="class">GNUTranslations</tt> class | |
93 | can raise <tt class="exception">IOError</tt>. | |
94 | ||
95 | <P> | |
96 | The following methods are overridden from the base class implementation: | |
97 | ||
98 | <P> | |
99 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
100 | <td><nobr><b><tt id='l2h-2448' xml:id='l2h-2448' class="method">gettext</tt></b>(</nobr></td> | |
101 | <td><var>message</var>)</td></tr></table></dt> | |
102 | <dd> | |
103 | Look up the <var>message</var> id in the catalog and return the | |
104 | corresponding message string, as an 8-bit string encoded with the | |
105 | catalog's charset encoding, if known. If there is no entry in the | |
106 | catalog for the <var>message</var> id, and a fallback has been set, the | |
107 | look up is forwarded to the fallback's <tt class="method">gettext()</tt> method. | |
108 | Otherwise, the <var>message</var> id is returned. | |
109 | </dl> | |
110 | ||
111 | <P> | |
112 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
113 | <td><nobr><b><tt id='l2h-2449' xml:id='l2h-2449' class="method">lgettext</tt></b>(</nobr></td> | |
114 | <td><var>message</var>)</td></tr></table></dt> | |
115 | <dd> | |
116 | Equivalent to <tt class="method">gettext()</tt>, but the translation is returned | |
117 | in the preferred system encoding, if no other encoding was explicitly | |
118 | set with <tt class="method">set_output_charset()</tt>. | |
119 | ||
120 | <P> | |
121 | ||
122 | <span class="versionnote">New in version 2.4.</span> | |
123 | ||
124 | </dl> | |
125 | ||
126 | <P> | |
127 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
128 | <td><nobr><b><tt id='l2h-2450' xml:id='l2h-2450' class="method">ugettext</tt></b>(</nobr></td> | |
129 | <td><var>message</var>)</td></tr></table></dt> | |
130 | <dd> | |
131 | Look up the <var>message</var> id in the catalog and return the | |
132 | corresponding message string, as a Unicode string. If there is no | |
133 | entry in the catalog for the <var>message</var> id, and a fallback has been | |
134 | set, the look up is forwarded to the fallback's <tt class="method">ugettext()</tt> | |
135 | method. Otherwise, the <var>message</var> id is returned. | |
136 | </dl> | |
137 | ||
138 | <P> | |
139 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
140 | <td><nobr><b><tt id='l2h-2451' xml:id='l2h-2451' class="method">ngettext</tt></b>(</nobr></td> | |
141 | <td><var>singular, plural, n</var>)</td></tr></table></dt> | |
142 | <dd> | |
143 | Do a plural-forms lookup of a message id. <var>singular</var> is used as | |
144 | the message id for purposes of lookup in the catalog, while <var>n</var> is | |
145 | used to determine which plural form to use. The returned message | |
146 | string is an 8-bit string encoded with the catalog's charset encoding, | |
147 | if known. | |
148 | ||
149 | <P> | |
150 | If the message id is not found in the catalog, and a fallback is | |
151 | specified, the request is forwarded to the fallback's | |
152 | <tt class="method">ngettext()</tt> method. Otherwise, when <var>n</var> is 1 <var>singular</var> is | |
153 | returned, and <var>plural</var> is returned in all other cases. | |
154 | ||
155 | <P> | |
156 | ||
157 | <span class="versionnote">New in version 2.3.</span> | |
158 | ||
159 | </dl> | |
160 | ||
161 | <P> | |
162 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
163 | <td><nobr><b><tt id='l2h-2452' xml:id='l2h-2452' class="method">lngettext</tt></b>(</nobr></td> | |
164 | <td><var>singular, plural, n</var>)</td></tr></table></dt> | |
165 | <dd> | |
166 | Equivalent to <tt class="method">gettext()</tt>, but the translation is returned | |
167 | in the preferred system encoding, if no other encoding was explicitly | |
168 | set with <tt class="method">set_output_charset()</tt>. | |
169 | ||
170 | <P> | |
171 | ||
172 | <span class="versionnote">New in version 2.4.</span> | |
173 | ||
174 | </dl> | |
175 | ||
176 | <P> | |
177 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
178 | <td><nobr><b><tt id='l2h-2453' xml:id='l2h-2453' class="method">ungettext</tt></b>(</nobr></td> | |
179 | <td><var>singular, plural, n</var>)</td></tr></table></dt> | |
180 | <dd> | |
181 | Do a plural-forms lookup of a message id. <var>singular</var> is used as | |
182 | the message id for purposes of lookup in the catalog, while <var>n</var> is | |
183 | used to determine which plural form to use. The returned message | |
184 | string is a Unicode string. | |
185 | ||
186 | <P> | |
187 | If the message id is not found in the catalog, and a fallback is | |
188 | specified, the request is forwarded to the fallback's | |
189 | <tt class="method">ungettext()</tt> method. Otherwise, when <var>n</var> is 1 <var>singular</var> is | |
190 | returned, and <var>plural</var> is returned in all other cases. | |
191 | ||
192 | <P> | |
193 | Here is an example: | |
194 | ||
195 | <P> | |
196 | <div class="verbatim"><pre> | |
197 | n = len(os.listdir('.')) | |
198 | cat = GNUTranslations(somefile) | |
199 | message = cat.ungettext( | |
200 | 'There is %(num)d file in this directory', | |
201 | 'There are %(num)d files in this directory', | |
202 | n) % {'num': n} | |
203 | </pre></div> | |
204 | ||
205 | <P> | |
206 | ||
207 | <span class="versionnote">New in version 2.3.</span> | |
208 | ||
209 | </dl> | |
210 | ||
211 | <P> | |
212 | ||
213 | <DIV CLASS="navigation"> | |
214 | <div class='online-navigation'> | |
215 | <p></p><hr /> | |
216 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
217 | <tr> | |
218 | <td class='online-navigation'><a rel="prev" title="6.28.2.1 The NullTranslations class" | |
219 | href="node329.html"><img src='../icons/previous.png' | |
220 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
221 | <td class='online-navigation'><a rel="parent" title="6.28.2 Class-based API" | |
222 | href="node328.html"><img src='../icons/up.png' | |
223 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
224 | <td class='online-navigation'><a rel="next" title="6.28.2.3 Solaris message catalog" | |
225 | href="node331.html"><img src='../icons/next.png' | |
226 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
227 | <td align="center" width="100%">Python Library Reference</td> | |
228 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
229 | href="contents.html"><img src='../icons/contents.png' | |
230 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
231 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
232 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
233 | <td class='online-navigation'><a rel="index" title="Index" | |
234 | href="genindex.html"><img src='../icons/index.png' | |
235 | border='0' height='32' alt='Index' width='32' /></A></td> | |
236 | </tr></table> | |
237 | <div class='online-navigation'> | |
238 | <b class="navlabel">Previous:</b> | |
239 | <a class="sectref" rel="prev" href="node329.html">6.28.2.1 The NullTranslations class</A> | |
240 | <b class="navlabel">Up:</b> | |
241 | <a class="sectref" rel="parent" href="node328.html">6.28.2 Class-based API</A> | |
242 | <b class="navlabel">Next:</b> | |
243 | <a class="sectref" rel="next" href="node331.html">6.28.2.3 Solaris message catalog</A> | |
244 | </div> | |
245 | </div> | |
246 | <hr /> | |
247 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
248 | </DIV> | |
249 | <!--End of Navigation Panel--> | |
250 | <ADDRESS> | |
251 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
252 | </ADDRESS> | |
253 | </BODY> | |
254 | </HTML> |