Qt translation native widgets (QTextEdit right-click menu, etc.)

Original: https://blog.csdn.net/liang19890820/article/details/53738384

Brief

In the process of internationalization, if you use Qt widgets comes with native features, many of which will not be self-translated text, such as: QTextEdit right-click menu (copy, paste).

The best way is to provide relevant Qt ts translating the source module, the developer according to the contents of their own translation to translate. However, look for Qt installation directory, and not related to the translation of the source. If so, then we can solve on their own!

| Copyright: a go, two or three miles, shall not be reproduced without the bloggers allowed.

Add translation source

Qt components associated module widgets, since the translated text widgets belonging to the source, the source into the associated path widgets (e.g., D: \ Qt \ Qt5.7.0 \ 5.7 \ Src \ qtbase \ src \ widgets), open widgets.pro ,Add to:

TRANSLATIONS += widgets.ts
  
  
  • 1

Note: The name can be freely taken - because the content translation module belonging widgets, in order to see to know the name of justice, so named widgets.ts.

Translation extract source

To extract translation source widgets.ts, by the following two ways:

  • Using the command line, lupdate widgets.pro
  • Qt Creator in the system menu, select: Tools -> External -> Qt Linguist -> Update translation (lupdate)

The command line as an example:

D:\Qt\Qt5.7.0\5.7\Src\qtbase\src\widgets>lupdate widgets.pro
Updating ‘widgets.ts’…
Found 218 source text(s) (218 new and 0 already existing)

D:\Qt\Qt5.7.0\5.7\Src\qtbase\src\widgets>

After performing lupdate, widgets.ts will generate a file in the directory widgets.pro.

release

With Qt Linguist open widgets.ts, find the parts need to be translated, translation. QTextEdit to right-click menu (copy, paste), for example, due to the right-click menu QTextEdit achieved by QWidgetTextControl, find QWidgetTextControl.

Translation-related options, such as:

& Undo -> Undo
& Red -> Redo
Cu & t -> Clip
& Copy -> Copy
Copy & Link Location -> Copy Link Address
& Paste -> Paste
Delete -> Delete
Select All -> Select All

Write pictures described here

Save the file after translation, select: File -> Publish, then there will generate a file named widgets.qm of (can also be used lrelease widgets.tsto generate).

Note: Other widgets, such as: QLineEdit and QMessageBox also be translated in the same way.

Load the translation file

Use QTranslator load generated widgets.qm file:

QTranslator translator;  
translator.load(":/qm/widgets.qm");  
app.installTranslator(&translator); 
  
  
  • 1
  • 2
  • 3

Write pictures described here

So far, the program can display the specified language.

Guess you like

Origin blog.csdn.net/a844651990/article/details/84667439