/ src / tr.py
tr.py
 1  """
 2  Translating text
 3  """
 4  import os
 5  
 6  try:
 7      import state
 8  except ImportError:
 9      import state
10  
11  
12  class translateClass:
13      """
14      This is used so that the translateText function can be used
15      when we are in daemon mode and not using any QT functions.
16      """
17      # pylint: disable=old-style-class,too-few-public-methods
18      def __init__(self, context, text):
19          self.context = context
20          self.text = text
21  
22      def arg(self, _):
23          """Replace argument placeholders"""
24          if '%' in self.text:
25              # This doesn't actually do anything with the arguments
26              # because we don't have a UI in which to display this information anyway.
27              return translateClass(self.context, self.text.replace('%', '', 1))
28          return self.text
29  
30  
31  def _translate(context, text, disambiguation=None, encoding=None, n=None):
32      # pylint: disable=unused-argument
33      return translateText(context, text, n)
34  
35  
36  def translateText(context, text, n=None):
37      """Translate text in context"""
38      try:
39          enableGUI = state.enableGUI
40      except AttributeError:  # inside the plugin
41          enableGUI = True
42      if enableGUI:
43          try:
44              from PyQt4 import QtCore, QtGui
45          except Exception as err:
46              print('PyBitmessage requires PyQt unless you want to run it as a daemon'
47                    ' and interact with it using the API.'
48                    ' You can download PyQt from http://www.riverbankcomputing.com/software/pyqt/download'
49                    ' or by searching Google for \'PyQt Download\'.'
50                    ' If you want to run in daemon mode, see https://bitmessage.org/wiki/Daemon')
51              print(('Error message:', err))
52              os._exit(0)  # pylint: disable=protected-access
53          if n is None:
54              return QtGui.QApplication.translate(context, text)
55          return QtGui.QApplication.translate(context, text, None, QtCore.QCoreApplication.CodecForTr, n)
56      else:
57          if '%' in text:
58              return translateClass(context, text.replace('%', '', 1))
59          return text