2017-09-05 111 views
0

我很難搞清楚如何使用Qt爲python應用程序創建翻譯文件。關於Qt國際化的問題

我使用python 2.7,Qt版本5.9.1和PyQt4 4.12.1在OSX 10.11.6上創建我的GUI。

現在我只想翻譯一下我的代碼。

對於我所理解的,我必須使用QtLinguist來打開.ts文件,翻譯單詞並創建一個.qm文件,然後由python使用它。

Qt Linguist page,我感到我需要使用一個.pro項目文件,將由pylupdate4閱讀,等等

現在,我做我創建一個.pro文件?

我試圖運行:

$ qmake -project myfile.py 
$ pylupdate4 myfile.pro -ts file.ts 

但由此產生的.pro文件不能被pylupdate4XML error: Parse error at line 1, column 1 [...]

讀取從這個Tutorial,我想:

$ pylupdate4 myfile.py -ts file.ts 

哪創建一個空的.ts文件,Qt語言學家無法打開。

有人可以給我任何提示可能是錯誤的,我在瀏覽器中打開的15個標籤沒有幫助。

這裏是我的Python代碼,如果你需要它:

import sys 
import os.path as osp 
import os 
from PyQt4 import QtGui, QtCore 

class MainWindow(QtGui.QWidget): 

    def __init__(self): 
     super(MainWindow,self).__init__() 

     # Set MainWindow geometry, use settings of last session. If it's first session, 
     # use defaulted settings 
     self.settings = QtCore.QSettings('Paul',QtCore.QSettings.NativeFormat) 
     self.resize(self.settings.value("size", QtCore.QSize(500, 300)).toSize()) 
     self.move(self.settings.value("pos", QtCore.QPoint(5, 5)).toPoint()); 

     self.initUI() 


    def closeEvent(self, e): 
     #Save MainWindow geometry session when closing the window 
     self.settings.setValue("size",self.size()) 
     self.settings.setValue("pos",self.pos()) 
     e.accept() 

    def initUI(self): 

     self.hbox = QtGui.QVBoxLayout(self) # Create Vertival box layout to put the buttons 
     self.myButtons = QtGui.QPushButton('button',self) #create push button 
     self.myButtons.setStyleSheet("""QPushButton { background-color: red; font:bold 20px}""") 
     self.myButtons.setToolTip('Push this button') 
     self.myButtons.setText(self.tr(QtCore.QString('yes'))) 
     comboBox=QtGui.QComboBox(self) #create drop down menu 
     comboBox.addItem('Portugues') 
     comboBox.addItem('English') 
     self.hbox.addWidget(comboBox,1,QtCore.Qt.AlignRight) #add drop down menu to box layout 
     self.hbox.addStretch(3)  # set separation between buttons 
     self.myButtons.clicked.connect(self.buttonClicked) # what should the button do 
     self.hbox.addWidget(self.myButtons,1,QtCore.Qt.AlignRight) #add button to box layout 


     self.setWindowTitle('Test2') 


     self.show() 


    def buttonClicked(self): 

     msbox= QtGui.QMessageBox() 
     choice=msbox.warning(self,'ok',"This button doesn't do anything!!!") 


     if choice == QtGui.QMessageBox.No: 
      print('nanan') 
     else: 
      print('Bye') 
      self.settings.setValue("size",self.size()); 
      self.settings.setValue("pos",self.pos()); 
      sys.exit() 

def main(): 

    app = QtGui.QApplication(sys.argv) 
    translator = QtCore.QTranslator() 
    translator.load("~/basefiles/translations/qt_pt.qm") 
    app.installTranslator(translator) 
    ex = MainWindow() 
    sys.exit(app.exec_()) 


if __name__ == '__main__': 
    main() 
+0

你有什麼版本的pyqt4? – eyllanesc

+0

版本4.12.1,Riverbank下載頁面的最新版本 – Elcook

回答

0

當您使用self.tr你必須把這個字符串,而不是QString的變量,在你的情況下,它的變化:

self.myButtons.setText(self.tr(QtCore.QString('yes'))) 

self.myButtons.setText(self.tr("yes")) 

然後再運行一切。

+0

謝謝你的幫助。這會在以後出現問題,但現在還沒有解決我的問題。我仍然不知道如何創建正確的翻譯文件。 – Elcook

+0

我工作,但讓我們繼續嘗試,嘗試更改該行:'self.myButtons.setText(self.tr(「是」))' – eyllanesc

+0

這工作!現在我可以用我的翻譯編輯創建的.ts文件並創建.qm文件。我確信問題是使用pylupdate4時... – Elcook