<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"node331.html" />
<link rel=
"prev" href=
"node329.html" />
<link rel=
"parent" href=
"node328.html" />
<link rel=
"next" href=
"node331.html" />
<meta name='aesop' content='information'
/>
<title>6.28.2.2 The GNUTranslations class
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.28.2.1 The NullTranslations class"
href=
"node329.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"6.28.2 Class-based API"
href=
"node328.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"6.28.2.3 Solaris message catalog"
href=
"node331.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node329.html">6.28.2.1 The NullTranslations class
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"node328.html">6.28.2 Class-based API
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node331.html">6.28.2.3 Solaris message catalog
</A>
<!--End of Navigation Panel-->
<H3><A NAME=
"SECTION0082822000000000000000">
6.28.2.2 The
<tt class=
"class">GNUTranslations
</tt> class
</A>
The
<tt class=
"module">gettext
</tt> module provides one additional class derived from
<tt class=
"class">NullTranslations
</tt>:
<tt class=
"class">GNUTranslations
</tt>. This class
overrides
<tt class=
"method">_parse()
</tt> to enable reading GNU
<b class=
"program">gettext
</b>
format
<span class=
"file">.mo
</span> files in both big-endian and little-endian format.
It also coerces both message ids and message strings to Unicode.
<tt class=
"class">GNUTranslations
</tt> parses optional meta-data out of the
translation catalog. It is convention with GNU
<b class=
"program">gettext
</b> to
include meta-data as the translation for the empty string. This
meta-data is in
<a class=
"rfc" id='rfcref-
88910' xml:id='rfcref-
88910'
href=
"http://www.faqs.org/rfcs/rfc822.html">RFC
822</a>-style
<code>key: value
</code> pairs, and should
contain the
<code>Project-Id-Version
</code> key. If the key
<code>Content-Type
</code> is found, then the
<code>charset
</code> property is used
to initialize the ``protected''
<tt class=
"member">_charset
</tt> instance variable,
defaulting to
<code>None
</code> if not found. If the charset encoding is
specified, then all message ids and message strings read from the
catalog are converted to Unicode using this encoding. The
<tt class=
"method">ugettext()
</tt> method always returns a Unicode, while the
<tt class=
"method">gettext()
</tt> returns an encoded
8-bit string. For the message
id arguments of both methods, either Unicode strings or
8-bit strings
containing only US-ASCII characters are acceptable. Note that the
Unicode version of the methods (i.e.
<tt class=
"method">ugettext()
</tt> and
<tt class=
"method">ungettext()
</tt>) are the recommended interface to use for
internationalized Python programs.
The entire set of key/value pairs are placed into a dictionary and set
as the ``protected''
<tt class=
"member">_info
</tt> instance variable.
If the
<span class=
"file">.mo
</span> file's magic number is invalid, or if other problems
occur while reading the file, instantiating a
<tt class=
"class">GNUTranslations
</tt> class
can raise
<tt class=
"exception">IOError
</tt>.
The following methods are overridden from the base class implementation:
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2448' xml:id='l2h-
2448'
class=
"method">gettext
</tt></b>(
</nobr></td>
<td><var>message
</var>)
</td></tr></table></dt>
Look up the
<var>message
</var> id in the catalog and return the
corresponding message string, as an
8-bit string encoded with the
catalog's charset encoding, if known. If there is no entry in the
catalog for the
<var>message
</var> id, and a fallback has been set, the
look up is forwarded to the fallback's
<tt class=
"method">gettext()
</tt> method.
Otherwise, the
<var>message
</var> id is returned.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2449' xml:id='l2h-
2449'
class=
"method">lgettext
</tt></b>(
</nobr></td>
<td><var>message
</var>)
</td></tr></table></dt>
Equivalent to
<tt class=
"method">gettext()
</tt>, but the translation is returned
in the preferred system encoding, if no other encoding was explicitly
set with
<tt class=
"method">set_output_charset()
</tt>.
<span class=
"versionnote">New in version
2.4.
</span>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2450' xml:id='l2h-
2450'
class=
"method">ugettext
</tt></b>(
</nobr></td>
<td><var>message
</var>)
</td></tr></table></dt>
Look up the
<var>message
</var> id in the catalog and return the
corresponding message string, as a Unicode string. If there is no
entry in the catalog for the
<var>message
</var> id, and a fallback has been
set, the look up is forwarded to the fallback's
<tt class=
"method">ugettext()
</tt>
method. Otherwise, the
<var>message
</var> id is returned.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2451' xml:id='l2h-
2451'
class=
"method">ngettext
</tt></b>(
</nobr></td>
<td><var>singular, plural, n
</var>)
</td></tr></table></dt>
Do a plural-forms lookup of a message id.
<var>singular
</var> is used as
the message id for purposes of lookup in the catalog, while
<var>n
</var> is
used to determine which plural form to use. The returned message
string is an
8-bit string encoded with the catalog's charset encoding,
If the message id is not found in the catalog, and a fallback is
specified, the request is forwarded to the fallback's
<tt class=
"method">ngettext()
</tt> method. Otherwise, when
<var>n
</var> is
1 <var>singular
</var> is
returned, and
<var>plural
</var> is returned in all other cases.
<span class=
"versionnote">New in version
2.3.
</span>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2452' xml:id='l2h-
2452'
class=
"method">lngettext
</tt></b>(
</nobr></td>
<td><var>singular, plural, n
</var>)
</td></tr></table></dt>
Equivalent to
<tt class=
"method">gettext()
</tt>, but the translation is returned
in the preferred system encoding, if no other encoding was explicitly
set with
<tt class=
"method">set_output_charset()
</tt>.
<span class=
"versionnote">New in version
2.4.
</span>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2453' xml:id='l2h-
2453'
class=
"method">ungettext
</tt></b>(
</nobr></td>
<td><var>singular, plural, n
</var>)
</td></tr></table></dt>
Do a plural-forms lookup of a message id.
<var>singular
</var> is used as
the message id for purposes of lookup in the catalog, while
<var>n
</var> is
used to determine which plural form to use. The returned message
string is a Unicode string.
If the message id is not found in the catalog, and a fallback is
specified, the request is forwarded to the fallback's
<tt class=
"method">ungettext()
</tt> method. Otherwise, when
<var>n
</var> is
1 <var>singular
</var> is
returned, and
<var>plural
</var> is returned in all other cases.
<div class=
"verbatim"><pre>
cat = GNUTranslations(somefile)
'There is %(num)d file in this directory',
'There are %(num)d files in this directory',
<span class=
"versionnote">New in version
2.3.
</span>
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.28.2.1 The NullTranslations class"
href=
"node329.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"6.28.2 Class-based API"
href=
"node328.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"6.28.2.3 Solaris message catalog"
href=
"node331.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node329.html">6.28.2.1 The NullTranslations class
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"node328.html">6.28.2 Class-based API
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node331.html">6.28.2.3 Solaris message catalog
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.