Refactor localisation

The language combo box is generated dynamically from the list of
available translations in the translations directory. This allows the
users to add their own translations without having to change the code.
This commit is contained in:
Peter Šurda 2016-04-15 12:12:49 +02:00
parent a7ec4c0555
commit fd4329bee0
3 changed files with 46 additions and 43 deletions

View File

@ -2349,10 +2349,10 @@ class MyForm(settingsmixin.SMainWindow):
shared.config.set('bitmessagesettings', 'replybelow', str( shared.config.set('bitmessagesettings', 'replybelow', str(
self.settingsDialogInstance.ui.checkBoxReplyBelow.isChecked())) self.settingsDialogInstance.ui.checkBoxReplyBelow.isChecked()))
lang_ind = int(self.settingsDialogInstance.ui.languageComboBox.currentIndex()) lang = str(self.settingsDialogInstance.ui.languageComboBox.itemData(self.settingsDialogInstance.ui.languageComboBox.currentIndex()).toString())
if not languages[lang_ind] == 'other': shared.config.set('bitmessagesettings', 'userlocale', lang)
shared.config.set('bitmessagesettings', 'userlocale', languages[lang_ind]) logger.debug("Setting locale to %s", lang)
change_translation(languages[lang_ind]) change_translation(lang)
if int(shared.config.get('bitmessagesettings', 'port')) != int(self.settingsDialogInstance.ui.lineEditTCPPort.text()): if int(shared.config.get('bitmessagesettings', 'port')) != int(self.settingsDialogInstance.ui.lineEditTCPPort.text()):
if not shared.safeConfigGetBoolean('bitmessagesettings', 'dontconnect'): if not shared.safeConfigGetBoolean('bitmessagesettings', 'dontconnect'):
@ -3870,15 +3870,6 @@ class settingsDialog(QtGui.QDialog):
self.ui.checkBoxReplyBelow.setChecked( self.ui.checkBoxReplyBelow.setChecked(
shared.safeConfigGetBoolean('bitmessagesettings', 'replybelow')) shared.safeConfigGetBoolean('bitmessagesettings', 'replybelow'))
global languages
languages = ['system','en','eo','fr','de','es','ru','no','ar','zh_cn','ja','nl','cs','en_pirate','other']
user_countrycode = str(shared.config.get('bitmessagesettings', 'userlocale'))
if user_countrycode in languages:
curr_index = languages.index(user_countrycode)
else:
curr_index = languages.index('other')
self.ui.languageComboBox.setCurrentIndex(curr_index)
if shared.appdata == shared.lookupExeFolder(): if shared.appdata == shared.lookupExeFolder():
self.ui.checkBoxPortableMode.setChecked(True) self.ui.checkBoxPortableMode.setChecked(True)
else: else:

View File

@ -0,0 +1,40 @@
import glob
import os
from PyQt4 import QtCore, QtGui
from debug import logger
from shared import codePath, config
class LanguageBox(QtGui.QComboBox):
languageName = {"system": "System Settings", "eo": "Esperanto", "en_pirate": "Pirate English"}
def __init__(self, parent = None):
super(QtGui.QComboBox, self).__init__(parent)
self.populate()
def populate(self):
self.languages = []
self.clear()
localesPath = os.path.join (codePath(), 'translations')
configuredLocale = "system"
try:
configuredLocale = config.get('bitmessagesettings', 'userlocale', "system")
except:
pass
self.addItem(QtGui.QApplication.translate("settingsDialog", "System Settings", "system"), "system")
self.setCurrentIndex(0)
self.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
for translationFile in sorted(glob.glob(os.path.join(localesPath, "bitmessage_*.qm"))):
localeShort = os.path.split(translationFile)[1].split("_", 1)[1][:-3]
locale = QtCore.QLocale(QtCore.QString(localeShort))
if localeShort in LanguageBox.languageName:
self.addItem(LanguageBox.languageName[localeShort], localeShort)
elif locale.nativeLanguageName() == "":
self.addItem(localeShort, localeShort)
else:
self.addItem(locale.nativeLanguageName(), localeShort)
for i in range(self.count()):
logger.debug("Checking locale %s at %i", str(self.itemData(i).toString()), i)
if self.itemData(i) == configuredLocale:
logger.debug("Matching locale %s at %i", configuredLocale, i)
self.setCurrentIndex(i)
break

View File

@ -8,6 +8,7 @@
# WARNING! All changes made in this file will be lost! # WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from languagebox import LanguageBox
try: try:
_fromUtf8 = QtCore.QString.fromUtf8 _fromUtf8 = QtCore.QString.fromUtf8
@ -88,36 +89,9 @@ class Ui_settingsDialog(object):
self.groupBox.setObjectName(_fromUtf8("groupBox")) self.groupBox.setObjectName(_fromUtf8("groupBox"))
self.formLayout_2 = QtGui.QFormLayout(self.groupBox) self.formLayout_2 = QtGui.QFormLayout(self.groupBox)
self.formLayout_2.setObjectName(_fromUtf8("formLayout_2")) self.formLayout_2.setObjectName(_fromUtf8("formLayout_2"))
self.languageComboBox = QtGui.QComboBox(self.groupBox) self.languageComboBox = LanguageBox(self.groupBox)
self.languageComboBox.setMinimumSize(QtCore.QSize(100, 0)) self.languageComboBox.setMinimumSize(QtCore.QSize(100, 0))
self.languageComboBox.setObjectName(_fromUtf8("languageComboBox")) self.languageComboBox.setObjectName(_fromUtf8("languageComboBox"))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.setItemText(1, _fromUtf8("English"))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.setItemText(2, _fromUtf8("Esperanto"))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.setItemText(3, _fromUtf8("Français"))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.setItemText(4, _fromUtf8("Deutsch"))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.setItemText(5, _fromUtf8("Español"))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.setItemText(6, _fromUtf8("русский"))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.setItemText(7, _fromUtf8("Norsk"))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.setItemText(8, _fromUtf8("العربية"))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.setItemText(9, _fromUtf8("简体中文"))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.setItemText(10, _fromUtf8("日本語"))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.setItemText(11, _fromUtf8("Nederlands"))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.setItemText(12, _fromUtf8("Česky"))
self.languageComboBox.addItem(_fromUtf8(""))
self.languageComboBox.addItem(_fromUtf8(""))
self.formLayout_2.setWidget(0, QtGui.QFormLayout.LabelRole, self.languageComboBox) self.formLayout_2.setWidget(0, QtGui.QFormLayout.LabelRole, self.languageComboBox)
self.formLayout.setWidget(9, QtGui.QFormLayout.FieldRole, self.groupBox) self.formLayout.setWidget(9, QtGui.QFormLayout.FieldRole, self.groupBox)
self.tabWidgetSettings.addTab(self.tabUserInterface, _fromUtf8("")) self.tabWidgetSettings.addTab(self.tabUserInterface, _fromUtf8(""))
@ -467,8 +441,6 @@ class Ui_settingsDialog(object):
self.checkBoxReplyBelow.setText(_translate("settingsDialog", "Reply below Quote", None)) self.checkBoxReplyBelow.setText(_translate("settingsDialog", "Reply below Quote", None))
self.groupBox.setTitle(_translate("settingsDialog", "Interface Language", None)) self.groupBox.setTitle(_translate("settingsDialog", "Interface Language", None))
self.languageComboBox.setItemText(0, _translate("settingsDialog", "System Settings", "system")) self.languageComboBox.setItemText(0, _translate("settingsDialog", "System Settings", "system"))
self.languageComboBox.setItemText(13, _translate("settingsDialog", "Pirate English", "en_pirate"))
self.languageComboBox.setItemText(14, _translate("settingsDialog", "Other (set in keys.dat)", "other"))
self.tabWidgetSettings.setTabText(self.tabWidgetSettings.indexOf(self.tabUserInterface), _translate("settingsDialog", "User Interface", None)) self.tabWidgetSettings.setTabText(self.tabWidgetSettings.indexOf(self.tabUserInterface), _translate("settingsDialog", "User Interface", None))
self.groupBox1.setTitle(_translate("settingsDialog", "Listening port", None)) self.groupBox1.setTitle(_translate("settingsDialog", "Listening port", None))
self.label.setText(_translate("settingsDialog", "Listen for connections on port:", None)) self.label.setText(_translate("settingsDialog", "Listen for connections on port:", None))