macOS平台下Qt應用程序菜單翻譯及調整


一、翻譯    

在macOS平台上,系統會為應用程序菜單添加一些額外的菜單項。先來看一些典型的例子:

這個是Qt Creator的菜單,系統為應用程序菜單添加了一些桌面顯示操作相關的菜單項;

這個是Qt Designer的應用程序菜單,也添加了一些額外的菜單項。並且我們可以注意到,這些菜單項在中文語言環境是翻譯好的。那么我們自己的應用程序,怎么去讓它們也翻譯好呢?

Qt應用程序的國際化都是通過ts文件來做翻譯的。通過lupdate程序掃描源代碼中被tr()宏包裹的字符串,得到需要翻譯的字符串。但是這些系統添加的字符串,並沒有存在我們的源碼當中。這個應該如何處理嗯?通過網上的一番搜尋發現,有一個直接且簡單的方法,直接在ts文件里面添加這些字符串並做好翻譯即可。如下所示:

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <!DOCTYPE TS>
  3 <TS version="2.1" language="zh_CN">
  4 <context>
  5     <name>demoMainWindow</name>
  6     <message>
  7         <location filename="../mainwindow.ui" line="14"/>
  8         <source>MainWindow</source>
  9         <translation>主窗口</translation>
 10     </message>
 11     <message>
 12         <location filename="../mainwindow.ui" line="28"/>
 13         <source>File</source>
 14         <translation>文件</translation>
 15     </message>
 16     <message>
 17         <location filename="../mainwindow.ui" line="40"/>
 18         <source>Edit</source>
 19         <translation>編輯</translation>
 20     </message>
 21     <message>
 22         <location filename="../mainwindow.ui" line="50"/>
 23         <source>View</source>
 24         <translation>視圖</translation>
 25     </message>
 26     <message>
 27         <location filename="../mainwindow.ui" line="57"/>
 28         <source>Settings</source>
 29         <translation>設置</translation>
 30     </message>
 31     <message>
 32         <location filename="../mainwindow.ui" line="65"/>
 33         <source>Help</source>
 34         <translation>幫助</translation>
 35     </message>
 36     <message>
 37         <location filename="../mainwindow.ui" line="81"/>
 38         <source>Open File</source>
 39         <translation>打開文件</translation>
 40     </message>
 41     <message>
 42         <location filename="../mainwindow.ui" line="86"/>
 43         <source>Open Project</source>
 44         <translation>打開工程</translation>
 45     </message>
 46     <message>
 47         <location filename="../mainwindow.ui" line="91"/>
 48         <source>Open Folder</source>
 49         <translation>打開文件夾</translation>
 50     </message>
 51     <message>
 52         <location filename="../mainwindow.ui" line="96"/>
 53         <source>Recent</source>
 54         <translation>最近文件</translation>
 55     </message>
 56     <message>
 57         <location filename="../mainwindow.ui" line="101"/>
 58         <source>Save</source>
 59         <translation>保存</translation>
 60     </message>
 61     <message>
 62         <location filename="../mainwindow.ui" line="106"/>
 63         <source>Quit</source>
 64         <translation>退出</translation>
 65     </message>
 66     <message>
 67         <location filename="../mainwindow.ui" line="111"/>
 68         <source>Redo</source>
 69         <translation>重做</translation>
 70     </message>
 71     <message>
 72         <location filename="../mainwindow.ui" line="116"/>
 73         <source>Undo</source>
 74         <translation>撤銷</translation>
 75     </message>
 76     <message>
 77         <location filename="../mainwindow.ui" line="121"/>
 78         <source>Cut</source>
 79         <translation>剪切</translation>
 80     </message>
 81     <message>
 82         <location filename="../mainwindow.ui" line="126"/>
 83         <source>Paste</source>
 84         <translation>粘貼</translation>
 85     </message>
 86     <message>
 87         <location filename="../mainwindow.ui" line="131"/>
 88         <source>Delete</source>
 89         <translation>刪除</translation>
 90     </message>
 91     <message>
 92         <location filename="../mainwindow.ui" line="136"/>
 93         <source>Tile</source>
 94         <translation>平鋪</translation>
 95     </message>
 96     <message>
 97         <location filename="../mainwindow.ui" line="141"/>
 98         <source>Stack</source>
 99         <translation>堆疊</translation>
100     </message>
101     <message>
102         <location filename="../mainwindow.ui" line="146"/>
103         <source>Preference</source>
104         <translation>偏好設置</translation>
105     </message>
106     <message>
107         <location filename="../mainwindow.ui" line="151"/>
108         <source>Log</source>
109         <translation>日志</translation>
110     </message>
111     <message>
112         <location filename="../mainwindow.ui" line="156"/>
113         <source>Tools</source>
114         <translation>工具</translation>
115     </message>
116     <message>
117         <location filename="../mainwindow.ui" line="161"/>
118         <source>About Demo</source>
119         <translation>關於Demo</translation>
120     </message>
121     <message>
122         <location filename="../mainwindow.ui" line="166"/>
123         <source>Check for updates</source>
124         <translation>檢查更新</translation>
125     </message>
126     <message>
127         <location filename="../mainwindow.ui" line="171"/>
128         <source>Website</source>
129         <translation>官網</translation>
130     </message>
131     <message>
132         <location filename="../mainwindow.ui" line="176"/>
133         <source>Tutorial</source>
134         <translation>教程</translation>
135     </message>
136 </context>
137 <context>
138     <name>MAC_APPLICATION_MENU</name>
139     <message>
140         <source>Services</source>
141         <translation>服務</translation>
142     </message>
143     <message>
144         <source>Hide %1</source>
145         <translation>隱藏 %1</translation>
146     </message>
147     <message>
148         <source>Hide Others</source>
149         <translation>隱藏其他</translation>
150     </message>
151     <message>
152         <source>Show All</source>
153         <translation>顯示所有</translation>
154     </message>
155     <message>
156         <source>Preferences...</source>
157         <translation>偏好設置…</translation>
158     </message>
159     <message>
160         <source>Quit %1</source>
161         <translation>退出 %1</translation>
162     </message>
163     <message>
164         <source>About %1</source>
165         <translation>關於 %1</translation>
166     </message>
167 </context>
168 
169 </TS>
View Code

手動添加一個context,並在該context下面添加翻譯。然后用lupdate更新ts文件即可。這樣,我們自己的應用程序也可以翻譯好了:

二、菜單項重排

為了契合不同平台的使用習慣,Qt給菜單添加了Menu Role這樣一個定義。通過定義不同的Menu Role,mac系統會調整應用程序的位置,以保證平台習慣的統一。Qt庫本身已經定義了幾個常用的Role:

這些Role類型的菜單項都會被系統重新調整位置到應用菜單里。如果我們還有一些其他的菜單項,也想放到應用菜單里面,應該怎么做呢?其實也非常簡單,直接將菜單的Role設置為ApplicationSpecificRole即可。按照設置順序,各個菜單項將依次出現在應用菜單中。比如說上面截圖中的Check for Updates菜單項,就是通過設置ApplicationSpecificRole定義來實現的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM