File: i18n.html

package info (click to toggle)
python-qt4 4.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 40,300 kB
  • ctags: 6,185
  • sloc: python: 125,988; cpp: 13,291; xml: 292; makefile: 246; php: 27; sh: 2
file content (203 lines) | stat: -rw-r--r-- 13,196 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Internationalisation of PyQt4 Applications &#8212; PyQt 4.12.1 Reference Guide</title>
    
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '4.12.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="shortcut icon" href="_static/logo_tn.ico"/>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="DBus Support" href="dbus.html" />
    <link rel="prev" title="Using PyQt4 from the Python Shell" href="python_shell.html" /> 
  </head>
  <body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="dbus.html" title="DBus Support"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="python_shell.html" title="Using PyQt4 from the Python Shell"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">PyQt 4.12.1 Reference Guide</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="internationalisation-of-pyqt4-applications">
<h1>Internationalisation of PyQt4 Applications<a class="headerlink" href="#internationalisation-of-pyqt4-applications" title="Permalink to this headline">¶</a></h1>
<p>PyQt4 and Qt include a comprehensive set of tools for translating applications
into local languages.  For a full description, see the Qt Linguist Manual in
the Qt documentation.</p>
<p>The process of internationalising an application comprises the following
steps.</p>
<ul class="simple">
<li>The programmer uses <strong class="program">pylupdate4</strong> to create or update a <code class="docutils literal"><span class="pre">.ts</span></code>
translation file for each language that the application is to be translated
into.  A <code class="docutils literal"><span class="pre">.ts</span></code> file is an XML file that contains the strings to be
translated and the corresponding translations that have already been made.
<strong class="program">pylupdate4</strong> can be run any number of times during development to
update the <code class="docutils literal"><span class="pre">.ts</span></code> files with the latest strings for translation.</li>
<li>The translator uses Qt Linguist to update the <code class="docutils literal"><span class="pre">.ts</span></code> files with translations
of the strings.</li>
<li>The release manager then uses Qt’s <strong class="program">lrelease</strong> utility to convert the
<code class="docutils literal"><span class="pre">.ts</span></code> files to <code class="docutils literal"><span class="pre">.qm</span></code> files which are compact binary equivalents used by
the application.  If an application cannot find an appropriate <code class="docutils literal"><span class="pre">.qm</span></code> file,
or a particular string hasn’t been translated, then the strings used in the
original source code are used instead.</li>
<li>The release manage may optionally use <strong class="program">pyrcc4</strong> to embed the <code class="docutils literal"><span class="pre">.qm</span></code>
files, along with other application resources such as icons, in a Python
module.  This may make packaging and distribution of the application easier.</li>
</ul>
<div class="section" id="pylupdate4">
<h2><strong class="program">pylupdate4</strong><a class="headerlink" href="#pylupdate4" title="Permalink to this headline">¶</a></h2>
<p><strong class="program">pylupdate4</strong> is PyQt4’s equivalent to Qt’s <strong class="program">lupdate</strong> utility
and is used in exactly the same way.  A Qt <code class="docutils literal"><span class="pre">.pro</span></code> project file is read that
specifies the Python source files and Qt Designer interface files from which
the text that needs to be translated is extracted.  The <code class="docutils literal"><span class="pre">.pro</span></code> file also
specifies the <code class="docutils literal"><span class="pre">.ts</span></code> translation files that <strong class="program">pylupdate4</strong> updates (or
creates if necessary) and are subsequently used by Qt Linguist.</p>
<p><strong class="program">pylupdate4</strong> will only be included if your copy of Qt includes the XML
module.</p>
</div>
<div class="section" id="differences-between-pyqt4-and-qt">
<h2>Differences Between PyQt4 and Qt<a class="headerlink" href="#differences-between-pyqt4-and-qt" title="Permalink to this headline">¶</a></h2>
<p>Qt implements internationalisation support through the <code class="docutils literal"><span class="pre">QTranslator</span></code> class,
and the <code class="docutils literal"><span class="pre">QCoreApplication::translate()</span></code>, <code class="docutils literal"><span class="pre">QObject::tr()</span></code> and
<code class="docutils literal"><span class="pre">QObject::trUtf8()</span></code> methods.  Usually the <code class="docutils literal"><span class="pre">tr()</span></code> method is used to obtain
the correct translation of a message.  The translation process uses a message
context to allow the same message to be translated differently.  <code class="docutils literal"><span class="pre">tr()</span></code> is
actually generated by <code class="docutils literal"><span class="pre">moc</span></code> and uses the hardcoded class name as the context.
On the other hand, <code class="docutils literal"><span class="pre">QApplication::translate()</span></code> allows the context to be
explicitly stated.</p>
<p>Unfortunately, because of the way Qt implements <code class="docutils literal"><span class="pre">tr()</span></code> (and <code class="docutils literal"><span class="pre">trUtf8()</span></code>) it
is not possible for PyQt4 to exactly reproduce its behaviour.  The PyQt4
implementation of <code class="docutils literal"><span class="pre">tr()</span></code> (and <code class="docutils literal"><span class="pre">trUtf8()</span></code>) uses the class name of the
instance as the context.  The key difference, and the source of potential
problems, is that the context is determined dynamically in PyQt4, but is
hardcoded in Qt.  In other words, the context of a translation may change
depending on an instance’s class hierarchy.  For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="n">QtCore</span><span class="o">.</span><span class="n">QObject</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">hello</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">tr</span><span class="p">(</span><span class="s2">&quot;Hello&quot;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">B</span><span class="p">(</span><span class="n">A</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
<span class="n">a</span><span class="o">.</span><span class="n">hello</span><span class="p">()</span>

<span class="n">b</span> <span class="o">=</span> <span class="n">B</span><span class="p">()</span>
<span class="n">b</span><span class="o">.</span><span class="n">hello</span><span class="p">()</span>
</pre></div>
</div>
<p>In the above the message is translated by <code class="docutils literal"><span class="pre">a.hello()</span></code> using a context of
<code class="docutils literal"><span class="pre">A</span></code>, and by <code class="docutils literal"><span class="pre">b.hello()</span></code> using a context of <code class="docutils literal"><span class="pre">B</span></code>.  In the equivalent C++
version the context would be <code class="docutils literal"><span class="pre">A</span></code> in both cases.</p>
<p>The PyQt4 behaviour is unsatisfactory and may be changed in the future.  It is
recommended that <code class="docutils literal"><span class="pre">QCoreApplication.translate()</span></code> be used in preference to
<code class="docutils literal"><span class="pre">tr()</span></code> (and <code class="docutils literal"><span class="pre">trUtf8()</span></code>).  This is guaranteed to work with current and
future versions of PyQt4 and makes it much easier to share message files
between Python and C++ code.  Below is the alternative implementation of <code class="docutils literal"><span class="pre">A</span></code>
that uses <code class="docutils literal"><span class="pre">QCoreApplication.translate()</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="n">QtCore</span><span class="o">.</span><span class="n">QObject</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">hello</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">QtCore</span><span class="o">.</span><span class="n">QCoreApplication</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="s2">&quot;A&quot;</span><span class="p">,</span> <span class="s2">&quot;Hello&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/logo.png" alt="Logo"/>
            </a></p>
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Internationalisation of PyQt4 Applications</a><ul>
<li><a class="reference internal" href="#pylupdate4"><strong class="program">pylupdate4</strong></a></li>
<li><a class="reference internal" href="#differences-between-pyqt4-and-qt">Differences Between PyQt4 and Qt</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="python_shell.html"
                        title="previous chapter">Using PyQt4 from the Python Shell</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="dbus.html"
                        title="next chapter">DBus Support</a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="dbus.html" title="DBus Support"
             >next</a> |</li>
        <li class="right" >
          <a href="python_shell.html" title="Using PyQt4 from the Python Shell"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">PyQt 4.12.1 Reference Guide</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016 Riverbank Computing Limited.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.2.
    </div>
  </body>
</html>