/ src / bitmessageqt / languagebox.py
languagebox.py
 1  """Language Box Module for Locale Settings"""
 2  # pylint: disable=too-few-public-methods,bad-continuation
 3  import glob
 4  import os
 5  
 6  from PyQt4 import QtCore, QtGui
 7  
 8  import paths
 9  from bmconfigparser import config
10  
11  
12  class LanguageBox(QtGui.QComboBox):
13      """LanguageBox class for Qt UI"""
14      languageName = {
15          "system": "System Settings", "eo": "Esperanto",
16          "en_pirate": "Pirate English"
17      }
18  
19      def __init__(self, parent=None):
20          super(QtGui.QComboBox, self).__init__(parent)
21          self.populate()
22  
23      def populate(self):
24          """Populates drop down list with all available languages."""
25          self.clear()
26          localesPath = os.path.join(paths.codePath(), 'translations')
27          self.addItem(QtGui.QApplication.translate(
28              "settingsDialog", "System Settings", "system"), "system")
29          self.setCurrentIndex(0)
30          self.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
31          for translationFile in sorted(
32              glob.glob(os.path.join(localesPath, "bitmessage_*.qm"))
33          ):
34              localeShort = \
35                  os.path.split(translationFile)[1].split("_", 1)[1][:-3]
36              if localeShort in LanguageBox.languageName:
37                  self.addItem(
38                      LanguageBox.languageName[localeShort], localeShort)
39              else:
40                  locale = QtCore.QLocale(localeShort)
41                  self.addItem(
42                      locale.nativeLanguageName() or localeShort, localeShort)
43  
44          configuredLocale = config.safeGet(
45              'bitmessagesettings', 'userlocale', "system")
46          for i in range(self.count()):
47              if self.itemData(i) == configuredLocale:
48                  self.setCurrentIndex(i)
49                  break