日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

詳解pyqt中解決國際化tr()函數不起作用的問題_python

作者:之一Yo ? 更新時間: 2022-04-12 編程語言

前言

有些時候我們在父類中使用了 self.tr('XXX'),使用 Qt Linguist 完成翻譯并導出 qm 文件后,發現子類中仍然是英文原文。比如下面這段代碼:

class AlbumCardBase(QWidget):
? ? """ 專輯卡基類 """

? ? def __init__(self, parent=None):
? ? ? ? super().__init__(parent=parent)
? ? ? ? self.playButton = BlurButton(
? ? ? ? ? ? self,
? ? ? ? ? ? (30, 65),
? ? ? ? ? ? ":/images/album_tab_interface/Play.png",
? ? ? ? ? ? self.coverPath,
? ? ? ? ? ? self.tr('Play')
? ? ? ? )
? ? ? ? self.addToButton = BlurButton(
? ? ? ? ? ? self,
? ? ? ? ? ? (100, 65),
? ? ? ? ? ? ":/images/album_tab_interface/Add.png",
? ? ? ? ? ? self.coverPath,
? ? ? ? ? ? self.tr('Add to')
? ? ? ? )

父類 AlbumCardBase 中有兩處使用了 tr 函數,分別翻譯為 播放 和 添加到,但是在子類中這些文本仍然會顯示為 Play 和 Add to,下面來看看如何解決這個問題。

解決過程

生成的 ts 文件中,有這樣一段代碼:

<context>
? ? <name>AlbumCardBase</name>
? ? <message>
? ? ? ? <location filename="../../components/album_card/album_card_base.py" line="50"/>
? ? ? ? <source>Add to</source>
? ? ? ? <translation>添加到</translation>
? ? </message>
? ? <message>
? ? ? ? <location filename="../../components/album_card/album_card_base.py" line="43"/>
? ? ? ? <source>Play</source>
? ? ? ? <translation>播放</translation>
? ? </message>
</context>

可以看到上述代碼描述了源文的位置和內容以及譯文,但是只對父類 AlbumCardBase 起作用。要想對子類應用上述規則,只需粘貼再修改 <name> 標簽中的類名即可,比如 AlbumCard 為子類,那么只需添加下述代碼:

<context>
? ? <name>AlbumCard</name>
? ? <message>
? ? ? ? <location filename="../../components/album_card/album_card_base.py" line="50"/>
? ? ? ? <source>Add to</source>
? ? ? ? <translation>添加到</translation>
? ? </message>
? ? <message>
? ? ? ? <location filename="../../components/album_card/album_card_base.py" line="43"/>
? ? ? ? <source>Play</source>
? ? ? ? <translation>播放</translation>
? ? </message>
</context>

完成上述步驟后導出 qm 文件即可。

原文鏈接:https://www.cnblogs.com/zhiyiYo/p/15862675.html

欄目分類
最近更新