-
pylupdate6 -ts ./translations/zh_CN.ts .
: update py&ui string into ts file. -
there is 2 ways to translation ts file:
a. with Qt Linguist(recommend for now if you are familiar with github and installed pyqt stuff): run
linguist
and open the ts file, you will get a nice UI for translation and preview.or
b. convert into ods for google sheets, run
./translations/tool_ts_to_ods.py zh_CN.ts
(you need to provide the filename as a command line argument) for later translation in google sheets. After translating in google sheets, run./translations/tool_ods_to_ts.py zh_CN.ods
(you need to provide the filename as a command line argument) to convert back. You will need pyexcel_ods installed (runpip install pyexcel_ods
) -
After ts file is translated, run
lrelease ./translations/zh_CN.ts
: ts to qm -
python will load qm file automatically at startup or manually with
QTranslator.load()
.
In PyQt6, internationalization (i18n) typically involves translating the text of your application into different languages so that users can choose their preferred language to use the application. Here are the general steps to correctly internationalize a PyQt6 application:
-
Prepare Translation Files:
- First, you need to install Qt's internationalization tool (Linguist), which you can find on Qt's official website.
- Use Linguist to create a translation file (.ts file). This file will contain all the text in your application that needs to be translated.
-
Use Translation Files in the Application:
- In your PyQt6 application, use
QtCore.QLocale
to determine the user's preferred language. - Use
QtCore.QTranslator
to load the appropriate translation file based on the user's preferred language setting.
- In your PyQt6 application, use
-
Mark Text for Translation in the User Interface:
- In your PyQt6 interface files (.ui files), use the
tr()
function to wrap the text that needs to be translated.
- In your PyQt6 interface files (.ui files), use the
-
Extract Text and Generate Translations:
- Use the
pylupdate6
tool to extract text marked withtr()
from your PyQt6 project and update it in the translation file (.ts). - Use the Linguist tool to open the translation file and provide translations for each text item.
- Use the
-
Compile Translation Files:
- Use the
lrelease
tool to compile the translation file (.ts) into binary files (.qm).
- Use the
-
Load Translation Files in the Application:
- In your application, use
QtCore.QTranslator
to load the compiled translation file (.qm).
- In your application, use
-
Allow Language Switching in the Application:
- Provide a user interface option for users to choose the language of the application.
- When the user selects a different language, reload the corresponding translation file.
These are the general steps, and the specific implementation may vary based on your application's structure and requirements. Be sure to refer to the PyQt6 documentation for more detailed information and sample code. Additionally, using version control systems like Git to track translation files and work on translations is a good practice.
pylupdate6
is a tool used to extract text strings from Python applications and generate translation files. Here's an example demonstrating how to use pylupdate6
:
Assuming you have a simple Python application that includes text strings that need to be translated. First, make sure that your application uses the tr()
function to mark these text strings for translation, as mentioned earlier.
Suppose your Python application code looks like this:
# myapp.py
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
button = QPushButton("Click me", self)
button.clicked.connect(self.onClick)
def onClick(self):
print("Button clicked")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec())
To use pylupdate6
to extract text strings from this application, you can follow these steps:
-
Ensure that your application's code uses the
tr()
function to mark text that needs to be translated. -
Open a terminal and navigate to the directory containing your application's code.
-
Run the following command to generate a
.ts
file:pylupdate6 main.py -ts zh_CN.ts
This will extract text strings marked with
tr()
frommyapp.py
and save them in thezh_CN.ts
file. -
You can use the Qt Linguist tool to open the
zh_CN.ts
file and provide translations for each extracted text string. -
Once the translations are completed, use the
lrelease
tool to compile the.ts
file into binary.qm
files, which are needed at runtime.lrelease zh_CN.ts
Now, your application is ready to support internationalization, and it can display translated text based on the user's preferred language. Make sure to distribute the .qm
files along with your application and load the appropriate translation file at runtime.