Steve Borho avatar Steve Borho committed bf1160f Merge

Merge with stable

Comments (0)

Files changed (6)

 import shutil
 import subprocess
 import cgi
+import tempfile
+import re
 from fnmatch import fnmatch
 from distutils import log
 from distutils.core import setup, Command
 class build_qt(Command):
     description = "build PyQt GUIs (.ui) and resources (.qrc)"
     user_options = [('force', 'f', 'forcibly compile everything'
-                     ' (ignore file timestamps)')]
-    boolean_options = ('force',)
+                     ' (ignore file timestamps)'),
+                    ('frozen', None, 'include resources for frozen exe')]
+    boolean_options = ('force', 'frozen')
 
     def initialize_options(self):
         self.force = None
+        self.frozen = False
 
     def finalize_options(self):
         self.set_undefined_options('build', ('force', 'force'))
         finally:
             os.unlink(qrc_file)
 
+    def _build_translations(self, basepath):
+        """Build translations_rc.py which inclues qt_xx.qm"""
+        from PyQt4.QtCore import QLibraryInfo
+        trpath = unicode(QLibraryInfo.location(QLibraryInfo.TranslationsPath))
+        d = tempfile.mkdtemp()
+        try:
+            for e in os.listdir(trpath):
+                if re.match(r'qt_[a-z]{2}(_[A-Z]{2})?\.ts$', e):
+                    r = os.system('lrelease "%s" -qm "%s"'
+                                  % (os.path.join(trpath, e),
+                                     os.path.join(d, e[:-3] + '.qm')))
+                    if r > 0:
+                        self.warn('Unable to generate Qt message file'
+                                  ' from %s' % e)
+            self.build_rc(os.path.join(basepath, 'translations_rc.py'),
+                          d, '/translations')
+        finally:
+            shutil.rmtree(d)
+
     def run(self):
         self._wrapuic()
         basepath = join(os.path.dirname(__file__), 'tortoisehg', 'hgqt')
         self.build_rc(os.path.join(basepath, 'icons_rc.py'),
                       os.path.join(os.path.dirname(__file__), 'icons'),
                       '/icons')
+        if self.frozen:
+            self._build_translations(basepath)
         for dirpath, _, filenames in os.walk(basepath):
             for filename in filenames:
                 if filename.endswith('.ui'):
 demandimport.ignore.append('win32com.shell')
 demandimport.ignore.append('tortoisehg.util.config')
 demandimport.ignore.append('icons_rc')
+demandimport.ignore.append('translations_rc')
 demandimport.enable()
 
 # Verify we can reach TortoiseHg sources first

tortoisehg/hgqt/__init__.py

 # load icon resources
 import icons_rc
+
+# load Qt translations for frozen environment
+try:
+    import translations_rc
+except ImportError:
+    pass

tortoisehg/hgqt/qtlib.py

     assert name in _fontdefaults
     return _fontcache[name]
 
+def gettranslationpath():
+    """Return path to Qt's translation file (.qm)"""
+    if getattr(sys, 'frozen', False):
+        return ':/translations'
+    else:
+        return QLibraryInfo.location(QLibraryInfo.TranslationsPath)
+
 def CommonMsgBox(icon, title, main, text='', buttons=QMessageBox.Ok,
                  labels=[], parent=None, defaultbutton=None):
     msg = QMessageBox(parent)

tortoisehg/hgqt/run.py

             self._mainapp.setOrganizationName('TortoiseHg')
             self._mainapp.setOrganizationDomain('tortoisehg.org')
             self._mainapp.setApplicationVersion(thgversion.version())
+            self._installtranslator()
             qtlib.setup_font_substitutions()
             qtlib.fix_application_font()
             qtlib.configstyles(ui)
         finally:
             self._mainapp = None
 
+    def _installtranslator(self):
+        if not i18n.language:
+            return
+        t = QTranslator(self._mainapp)
+        t.load('qt_' + i18n.language, qtlib.gettranslationpath())
+        self._mainapp.installTranslator(t)
+
     def _opendialog(self, dlgfunc, ui, *args, **opts):
         dlg = dlgfunc(ui, *args, **opts)
         if not dlg:

tortoisehg/util/i18n.py

 
 def setlanguage(lang=None):
     """Change translation catalog to the specified language"""
-    global t
+    global t, language
     if not lang:
         lang = _defaultlanguage()
     opts = {}
         opts['languages'] = (lang,)
     t = gettext.translation('tortoisehg', paths.get_locale_path(),
                             fallback=True, **opts)
+    language = lang or locale.getdefaultlocale(_localeenvs)[0]
 setlanguage()
 
 def availablelanguages():
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.