Made further improvements for l10n:

- cleaned up and formatted module tr (reduced indentation in
   _translate(), simplified and commented out all strip operations
   in translateClass because I see no reason to do it),
   it should be used by all other modules which need _translate()
 - reduced positional args in _translate() calls (which in fact are
   essentially the placeholders for c++ binding) and removed
   redundant sequential arg() calls in the modules
   __init__ and networkstatus
 - made units like kB, MB, kB/s available for translation
This commit is contained in:
Dmitri Bogomolov 2017-03-12 20:28:40 +02:00
parent 6bcbad63b9
commit 247737a8e0
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
3 changed files with 151 additions and 75 deletions

View File

@ -9,7 +9,7 @@ except Exception as err:
logger.critical(logmsg, exc_info=True) logger.critical(logmsg, exc_info=True)
sys.exit() sys.exit()
from tr import _translate from tr import _translate, _codec
from addresses import decodeAddress, addBMIfNotPresent from addresses import decodeAddress, addBMIfNotPresent
import shared import shared
from bitmessageui import Ui_MainWindow from bitmessageui import Ui_MainWindow
@ -585,8 +585,10 @@ class MyForm(settingsmixin.SMainWindow):
addressInKeysFile) addressInKeysFile)
if addressVersionNumber == 1: if addressVersionNumber == 1:
displayMsg = _translate( displayMsg = _translate(
"MainWindow", "One of your addresses, %1, is an old version 1 address. Version 1 addresses are no longer supported. " "MainWindow",
+ "May we delete it now?").arg(addressInKeysFile) "One of your addresses, %1, is an old version 1 address."
" Version 1 addresses are no longer supported."
" May we delete it now?").arg(addressInKeysFile)
reply = QtGui.QMessageBox.question( reply = QtGui.QMessageBox.question(
self, 'Message', displayMsg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) self, 'Message', displayMsg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes: if reply == QtGui.QMessageBox.Yes:
@ -816,16 +818,17 @@ class MyForm(settingsmixin.SMainWindow):
stylesheet = "" stylesheet = ""
if numberOfHours < 48: if numberOfHours < 48:
self.ui.labelHumanFriendlyTTLDescription.setText( self.ui.labelHumanFriendlyTTLDescription.setText(_translate(
_translate("MainWindow", "%n hour(s)", None, QtCore.QCoreApplication.CodecForTr, numberOfHours) + "MainWindow", "%n hour(s), not recommended for chans",
", " + None, _codec, numberOfHours
_translate("MainWindow", "not recommended for chans", None, QtCore.QCoreApplication.CodecForTr) ))
)
stylesheet = "QLabel { color : red; }" stylesheet = "QLabel { color : red; }"
font.setBold(True) font.setBold(True)
else: else:
numberOfDays = int(round(TTL / (24*60*60))) numberOfDays = int(round(TTL / (24*60*60)))
self.ui.labelHumanFriendlyTTLDescription.setText(_translate("MainWindow", "%n day(s)", None, QtCore.QCoreApplication.CodecForTr, numberOfDays)) self.ui.labelHumanFriendlyTTLDescription.setText(_translate(
"MainWindow", "%n day(s)", None, _codec, numberOfDays
))
font.setBold(False) font.setBold(False)
self.ui.labelHumanFriendlyTTLDescription.setStyleSheet(stylesheet) self.ui.labelHumanFriendlyTTLDescription.setStyleSheet(stylesheet)
self.ui.labelHumanFriendlyTTLDescription.setFont(font) self.ui.labelHumanFriendlyTTLDescription.setFont(font)
@ -1068,8 +1071,10 @@ class MyForm(settingsmixin.SMainWindow):
statusText = _translate( statusText = _translate(
"MainWindow", "Forced difficulty override. Send should start soon.") "MainWindow", "Forced difficulty override. Send should start soon.")
else: else:
statusText = _translate("MainWindow", "Unknown status: %1 %2").arg(status).arg( statusText = _translate(
l10n.formatTimestamp(lastactiontime)) "MainWindow", "Unknown status: %1 %2").arg(
status, l10n.formatTimestamp(lastactiontime)
)
newItem = myTableWidgetItem(statusText) newItem = myTableWidgetItem(statusText)
newItem.setToolTip(statusText) newItem.setToolTip(statusText)
newItem.setData(QtCore.Qt.UserRole, QtCore.QByteArray(ackdata)) newItem.setData(QtCore.Qt.UserRole, QtCore.QByteArray(ackdata))
@ -1143,7 +1148,8 @@ class MyForm(settingsmixin.SMainWindow):
tableWidget.horizontalHeader().setSortIndicator( tableWidget.horizontalHeader().setSortIndicator(
3, QtCore.Qt.DescendingOrder) 3, QtCore.Qt.DescendingOrder)
tableWidget.setSortingEnabled(True) tableWidget.setSortingEnabled(True)
tableWidget.horizontalHeaderItem(3).setText(_translate("MainWindow", "Sent", None)) tableWidget.horizontalHeaderItem(3).setText(
_translate("MainWindow", "Sent"))
tableWidget.setUpdatesEnabled(True) tableWidget.setUpdatesEnabled(True)
# Load messages from database file # Load messages from database file
@ -1177,7 +1183,8 @@ class MyForm(settingsmixin.SMainWindow):
3, QtCore.Qt.DescendingOrder) 3, QtCore.Qt.DescendingOrder)
tableWidget.setSortingEnabled(True) tableWidget.setSortingEnabled(True)
tableWidget.selectRow(0) tableWidget.selectRow(0)
tableWidget.horizontalHeaderItem(3).setText(_translate("MainWindow", "Received", None)) tableWidget.horizontalHeaderItem(3).setText(
_translate("MainWindow", "Received"))
tableWidget.setUpdatesEnabled(True) tableWidget.setUpdatesEnabled(True)
# create application indicator # create application indicator
@ -2020,12 +2027,28 @@ class MyForm(settingsmixin.SMainWindow):
toAddress = addBMIfNotPresent(toAddress) toAddress = addBMIfNotPresent(toAddress)
if addressVersionNumber > 4 or addressVersionNumber <= 1: if addressVersionNumber > 4 or addressVersionNumber <= 1:
QtGui.QMessageBox.about(self, _translate("MainWindow", "Address version number"), _translate( QtGui.QMessageBox.about(
"MainWindow", "Concerning the address %1, Bitmessage cannot understand address version numbers of %2. Perhaps upgrade Bitmessage to the latest version.").arg(toAddress).arg(str(addressVersionNumber))) self, _translate(
"MainWindow", "Address version number"),
_translate(
"MainWindow",
"Concerning the address %1, Bitmessage"
" cannot understand address version"
" numbers of %2. Perhaps upgrade"
" Bitmessage to the latest version."
).arg(toAddress, str(addressVersionNumber)))
continue continue
if streamNumber > 1 or streamNumber == 0: if streamNumber > 1 or streamNumber == 0:
QtGui.QMessageBox.about(self, _translate("MainWindow", "Stream number"), _translate( QtGui.QMessageBox.about(
"MainWindow", "Concerning the address %1, Bitmessage cannot handle stream numbers of %2. Perhaps upgrade Bitmessage to the latest version.").arg(toAddress).arg(str(streamNumber))) self, _translate(
"MainWindow", "Stream number"),
_translate(
"MainWindow",
"Concerning the address %1, Bitmessage"
" cannot handle stream numbers of %2."
" Perhaps upgrade Bitmessage to the"
" latest version."
).arg(toAddress, str(streamNumber)))
continue continue
self.statusbar.clearMessage() self.statusbar.clearMessage()
if shared.statusIconColor == 'red': if shared.statusIconColor == 'red':
@ -2699,12 +2722,22 @@ class MyForm(settingsmixin.SMainWindow):
waitForSync = False waitForSync = False
# C PoW currently doesn't support interrupting and OpenCL is untested # C PoW currently doesn't support interrupting and OpenCL is untested
if getPowType() == "python" and (powQueueSize() > 0 or PendingUpload().len() > 0): if getPowType() == "python" and (
reply = QtGui.QMessageBox.question(self, _translate("MainWindow", "Proof of work pending"), powQueueSize() > 0 or PendingUpload().len() > 0
_translate("MainWindow", "%n object(s) pending proof of work", None, QtCore.QCoreApplication.CodecForTr, powQueueSize()) + ", " + ):
_translate("MainWindow", "%n object(s) waiting to be distributed", None, QtCore.QCoreApplication.CodecForTr, PendingUpload().len()) + "\n\n" + reply = QtGui.QMessageBox.question(
_translate("MainWindow", "Wait until these tasks finish?"), self, _translate("MainWindow", "Proof of work pending"),
QtGui.QMessageBox.Yes|QtGui.QMessageBox.No|QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Cancel) _translate(
"MainWindow", "%n object(s) pending proof of work",
None, _codec, powQueueSize()) + ", " +
_translate(
"MainWindow", "%n object(s) waiting to be distributed",
None, _codec, PendingUpload().len()) + "\n\n" +
_translate("MainWindow", "Wait until these tasks finish?"),
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No |
QtGui.QMessageBox.Cancel,
QtGui.QMessageBox.Cancel
)
if reply == QtGui.QMessageBox.No: if reply == QtGui.QMessageBox.No:
waitForPow = False waitForPow = False
elif reply == QtGui.QMessageBox.Cancel: elif reply == QtGui.QMessageBox.Cancel:
@ -2712,7 +2745,7 @@ class MyForm(settingsmixin.SMainWindow):
if PendingDownloadQueue.totalSize() > 0: if PendingDownloadQueue.totalSize() > 0:
reply = QtGui.QMessageBox.question(self, _translate("MainWindow", "Synchronisation pending"), reply = QtGui.QMessageBox.question(self, _translate("MainWindow", "Synchronisation pending"),
_translate("MainWindow", "Bitmessage hasn't synchronised with the network, %n object(s) to be downloaded. If you quit now, it may cause delivery delays. Wait until the synchronisation finishes?", None, QtCore.QCoreApplication.CodecForTr, PendingDownloadQueue.totalSize()), _translate("MainWindow", "Bitmessage hasn't synchronised with the network, %n object(s) to be downloaded. If you quit now, it may cause delivery delays. Wait until the synchronisation finishes?", None, _codec, PendingDownloadQueue.totalSize()),
QtGui.QMessageBox.Yes|QtGui.QMessageBox.No|QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Cancel) QtGui.QMessageBox.Yes|QtGui.QMessageBox.No|QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Cancel)
if reply == QtGui.QMessageBox.Yes: if reply == QtGui.QMessageBox.Yes:
waitForSync = True waitForSync = True
@ -3884,11 +3917,15 @@ class MyForm(settingsmixin.SMainWindow):
self.popMenuInbox.addAction(self.actionReplyChan) self.popMenuInbox.addAction(self.actionReplyChan)
self.popMenuInbox.addAction(self.actionReply) self.popMenuInbox.addAction(self.actionReply)
self.popMenuInbox.addAction(self.actionAddSenderToAddressBook) self.popMenuInbox.addAction(self.actionAddSenderToAddressBook)
self.actionClipboardMessagelist = self.ui.inboxContextMenuToolbar.addAction( self.actionClipboardMessagelist = \
_translate("MainWindow", self.ui.inboxContextMenuToolbar.addAction(
"Copy subject to clipboard" if tableWidget.currentColumn() == 2 else "Copy address to clipboard" _translate(
), "MainWindow",
self.on_action_ClipboardMessagelist) "Copy subject to clipboard"
if tableWidget.currentColumn() == 2
else "Copy address to clipboard"),
self.on_action_ClipboardMessagelist
)
self.popMenuInbox.addAction(self.actionClipboardMessagelist) self.popMenuInbox.addAction(self.actionClipboardMessagelist)
self.popMenuInbox.addSeparator() self.popMenuInbox.addSeparator()
self.popMenuInbox.addAction(self.actionAddSenderToBlackList) self.popMenuInbox.addAction(self.actionAddSenderToBlackList)

View File

@ -1,8 +1,7 @@
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
import time import time
import shared import shared
from tr import _translate, _codec
from tr import _translate
from inventory import Inventory, PendingDownloadQueue, PendingUpload from inventory import Inventory, PendingDownloadQueue, PendingUpload
import knownnodes import knownnodes
import l10n import l10n
@ -27,9 +26,10 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin):
header.setSortIndicator(0, QtCore.Qt.AscendingOrder) header.setSortIndicator(0, QtCore.Qt.AscendingOrder)
self.startup = time.localtime() self.startup = time.localtime()
self.labelStartupTime.setText(_translate("networkstatus", "Since startup on %1").arg( self.labelStartupTime.setText(_translate(
l10n.formatTimestamp(self.startup))) "networkstatus", "Since startup on %1"
).arg(l10n.formatTimestamp(self.startup)))
self.UISignalThread = UISignaler.get() self.UISignalThread = UISignaler.get()
QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL(
"updateNumberOfMessagesProcessed()"), self.updateNumberOfMessagesProcessed) "updateNumberOfMessagesProcessed()"), self.updateNumberOfMessagesProcessed)
@ -54,33 +54,42 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin):
self.timer.stop() self.timer.stop()
def formatBytes(self, num): def formatBytes(self, num):
for x in [_translate("networkstatus", "byte(s)", None, QtCore.QCoreApplication.CodecForTr, num), "kB", "MB", "GB"]: for x in [_translate("networkstatus", "byte(s)", None, _codec, num),
_translate("networkstatus", "kB"),
_translate("networkstatus", "MB"),
_translate("networkstatus", "GB")]:
if num < 1000.0: if num < 1000.0:
return "%3.0f %s" % (num, x) return "%3.0f %s" % (num, x)
num /= 1000.0 num /= 1000.0
return "%3.0f %s" % (num, 'TB') return "%3.0f %s" % (num, _translate("networkstatus", "TB"))
def formatByteRate(self, num): def formatByteRate(self, num):
num /= 1000 num /= 1000
return "%4.0f kB" % num return "%4.0f" % num
def updateNumberOfObjectsToBeSynced(self): def updateNumberOfObjectsToBeSynced(self):
self.labelSyncStatus.setText(_translate("networkstatus", "Object(s) to be synced: %n", None, QtCore.QCoreApplication.CodecForTr, network.stats.pendingDownload() + network.stats.pendingUpload())) self.labelSyncStatus.setText(_translate(
"networkstatus", "Object(s) to be synced: %n", None, _codec,
network.stats.pendingDownload() + network.stats.pendingUpload()
))
def updateNumberOfMessagesProcessed(self): def updateNumberOfMessagesProcessed(self):
self.updateNumberOfObjectsToBeSynced() self.updateNumberOfObjectsToBeSynced()
self.labelMessageCount.setText(_translate( self.labelMessageCount.setText(_translate(
"networkstatus", "Processed %n person-to-person message(s).", None, QtCore.QCoreApplication.CodecForTr, shared.numberOfMessagesProcessed)) "networkstatus", "Processed %n person-to-person message(s).",
None, _codec, shared.numberOfMessagesProcessed))
def updateNumberOfBroadcastsProcessed(self): def updateNumberOfBroadcastsProcessed(self):
self.updateNumberOfObjectsToBeSynced() self.updateNumberOfObjectsToBeSynced()
self.labelBroadcastCount.setText(_translate( self.labelBroadcastCount.setText(_translate(
"networkstatus", "Processed %n broadcast message(s).", None, QtCore.QCoreApplication.CodecForTr, shared.numberOfBroadcastsProcessed)) "networkstatus", "Processed %n broadcast message(s).",
None, _codec, shared.numberOfBroadcastsProcessed))
def updateNumberOfPubkeysProcessed(self): def updateNumberOfPubkeysProcessed(self):
self.updateNumberOfObjectsToBeSynced() self.updateNumberOfObjectsToBeSynced()
self.labelPubkeyCount.setText(_translate( self.labelPubkeyCount.setText(_translate(
"networkstatus", "Processed %n public key(s).", None, QtCore.QCoreApplication.CodecForTr, shared.numberOfPubkeysProcessed)) "networkstatus", "Processed %n public key(s).",
None, _codec, shared.numberOfPubkeysProcessed))
def updateNumberOfBytes(self): def updateNumberOfBytes(self):
""" """
@ -88,9 +97,15 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin):
sent and received by 2. sent and received by 2.
""" """
self.labelBytesRecvCount.setText(_translate( self.labelBytesRecvCount.setText(_translate(
"networkstatus", "Down: %1/s Total: %2").arg(self.formatByteRate(network.stats.downloadSpeed()), self.formatBytes(network.stats.receivedBytes()))) "networkstatus", "Down: %1 kB/s Total: %2").arg(
self.formatByteRate(network.stats.downloadSpeed()),
self.formatBytes(network.stats.receivedBytes()))
)
self.labelBytesSentCount.setText(_translate( self.labelBytesSentCount.setText(_translate(
"networkstatus", "Up: %1/s Total: %2").arg(self.formatByteRate(network.stats.uploadSpeed()), self.formatBytes(network.stats.sentBytes()))) "networkstatus", "Up: %1 kB/s Total: %2").arg(
self.formatByteRate(network.stats.uploadSpeed()),
self.formatBytes(network.stats.sentBytes()))
)
def updateNetworkStatusTab(self, outbound, add, destination): def updateNetworkStatusTab(self, outbound, add, destination):
if outbound: if outbound:
@ -151,8 +166,9 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin):
self.tableWidgetConnectionCount.setUpdatesEnabled(True) self.tableWidgetConnectionCount.setUpdatesEnabled(True)
self.tableWidgetConnectionCount.setSortingEnabled(True) self.tableWidgetConnectionCount.setSortingEnabled(True)
self.labelTotalConnections.setText(_translate( self.labelTotalConnections.setText(_translate(
"networkstatus", "Total Connections: %1").arg(str(self.tableWidgetConnectionCount.rowCount()))) "networkstatus", "Total Connections: %1"
# FYI: The 'singlelistener' thread sets the icon color to green when it receives an incoming connection, meaning that the user's firewall is configured correctly. ).arg(str(self.tableWidgetConnectionCount.rowCount())))
# FYI: The 'singlelistener' thread sets the icon color to green when it receives an incoming connection, meaning that the user's firewall is configured correctly.
if self.tableWidgetConnectionCount.rowCount() and shared.statusIconColor == 'red': if self.tableWidgetConnectionCount.rowCount() and shared.statusIconColor == 'red':
self.window().setStatusIcon('yellow') self.window().setStatusIcon('yellow')
elif self.tableWidgetConnectionCount.rowCount() == 0 and shared.statusIconColor != "red": elif self.tableWidgetConnectionCount.rowCount() == 0 and shared.statusIconColor != "red":
@ -161,12 +177,14 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin):
# timer driven # timer driven
def runEveryTwoSeconds(self): def runEveryTwoSeconds(self):
self.labelLookupsPerSecond.setText(_translate( self.labelLookupsPerSecond.setText(_translate(
"networkstatus", "Inventory lookups per second: %1").arg(str(Inventory().numberOfInventoryLookupsPerformed/2))) "networkstatus", "Inventory lookups per second: %1"
).arg(str(Inventory().numberOfInventoryLookupsPerformed/2)))
Inventory().numberOfInventoryLookupsPerformed = 0 Inventory().numberOfInventoryLookupsPerformed = 0
self.updateNumberOfBytes() self.updateNumberOfBytes()
self.updateNumberOfObjectsToBeSynced() self.updateNumberOfObjectsToBeSynced()
def retranslateUi(self): def retranslateUi(self):
super(NetworkStatus, self).retranslateUi() super(NetworkStatus, self).retranslateUi()
self.labelStartupTime.setText(_translate("networkstatus", "Since startup on %1").arg( self.labelStartupTime.setText(_translate(
l10n.formatTimestamp(self.startup))) "networkstatus", "Since startup on %1"
).arg(l10n.formatTimestamp(self.startup)))

View File

@ -1,39 +1,60 @@
import os import os
import textwrap
import shared import shared
# This is used so that the translateText function can be used when we are in daemon mode and not using any QT functions. _codec = None
# This is used so that the translateText function can be used
# when we are in daemon mode and not using any Qt functions.
class translateClass: class translateClass:
def __init__(self, context, text): def __init__(self, context, text):
self.context = context
self.text = text self.text = text
def arg(self,argument):
if '%' in self.text:
return translateClass(self.context, self.text.replace('%','',1)) # This doesn't actually do anything with the arguments because we don't have a UI in which to display this information anyway.
else:
return self.text
def _translate(context, text, disambiguation = None, encoding = None, n = None): def __str__(self):
return translateText(context, text, n) return unicode(self.text)
def translateText(context, text, n = None): def arg(self, *args):
# if args and '%' in self.text:
# try:
# ''.join(args)
# except TypeError:
# args = None
# self.text = self.text.replace('%', '', len(args) if args else 1)
# This doesn't actually do anything with the arguments because
# we don't have a UI in which to display this information anyway.
return self
def _translate(context, text, disambiguation=None, encoding=None, n=None):
return translateText(context, text, disambiguation, n)
def translateText(context, text, disambiguation=None, n=None):
try: try:
is_daemon = shared.thisapp.daemon is_daemon = shared.thisapp.daemon
except AttributeError: # inside the plugin except AttributeError: # inside the plugin
is_daemon = False is_daemon = False
if not is_daemon: if is_daemon:
try: return translateClass(context, text)
from PyQt4 import QtCore, QtGui
except Exception as err: try:
print 'PyBitmessage requires PyQt unless you want to run it as a daemon and interact with it using the API. You can download PyQt from http://www.riverbankcomputing.com/software/pyqt/download or by searching Google for \'PyQt Download\'. If you want to run in daemon mode, see https://bitmessage.org/wiki/Daemon' from PyQt4 import QtCore, QtGui
print 'Error message:', err except Exception as err:
os._exit(0) print textwrap.dedent("""
if n is None: PyBitmessage requires PyQt unless you want to run it as a daemon
return QtGui.QApplication.translate(context, text) and interact with it using the API. You can download PyQt from
else: http://www.riverbankcomputing.com/software/pyqt/download
return QtGui.QApplication.translate(context, text, None, QtCore.QCoreApplication.CodecForTr, n) or by searching Google for 'PyQt Download'.
else: If you want to run in daemon mode, see
if '%' in text: https://bitmessage.org/wiki/Daemon
return translateClass(context, text.replace('%','',1)) """)
else: print 'Error message:', err
return text os._exit(0)
return QtGui.QApplication.translate(
context, text, disambiguation,
QtCore.QCoreApplication.CodecForTr, n
) if n is not None \
else QtGui.QApplication.translate(context, text, disambiguation)