查看完整版本: [-- Qt 5.3.1 & Qt 5.5.1中带QMessageBox及QDialogButtonBox按钮翻译的qt_zh_CN.qm --]

QTCN开发网 -> Qt中文处理 -> Qt 5.3.1 & Qt 5.5.1中带QMessageBox及QDialogButtonBox按钮翻译的qt_zh_CN.qm [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

XChinux 2016-05-12 20:12

Qt 5.3.1 & Qt 5.5.1中带QMessageBox及QDialogButtonBox按钮翻译的qt_zh_CN.qm

自从Qt升级到5.x,程序中的对话框中的标准按钮文字就又变成英文了,尽管已经加载了qt_zh_CN.qm,忍了好久了,今天解决了一下,附上我自己用的Qt 5.3.1&Qt 5.5.1的qt_zh_CN.qm文件。下面是增加翻译方法:
1. 假设$QTDIR为Qt安装目录,对应源码目录为$QTDIR/src,那么找到qt_zh_CN.ts文件,我们要在里面增加翻译内容,该文件位于$QTDIR/src/qttranslations/translations目录下.
2. 找到QDialogButtonBox中标准按钮翻译的原始出处,在$QTDIR/src/qtbase/src/gui/kernel/qplatformtheme.cpp文件中,在该文件中,找到defaultStandardButtonText()函数的实现处,就能看到翻译调用:

QString QPlatformTheme::defaultStandardButtonText(int button)
{
    switch (button) {
    case QPlatformDialogHelper::Ok:
        return QCoreApplication::translate("QPlatformTheme", "OK");
    case QPlatformDialogHelper::Save:
        return QCoreApplication::translate("QPlatformTheme", "Save");
    case QPlatformDialogHelper::SaveAll:
        return QCoreApplication::translate("QPlatformTheme", "Save All");
    case QPlatformDialogHelper::Open:
        return QCoreApplication::translate("QPlatformTheme", "Open");
    case QPlatformDialogHelper::Yes:
        return QCoreApplication::translate("QPlatformTheme", "&Yes");
    case QPlatformDialogHelper::YesToAll:
        return QCoreApplication::translate("QPlatformTheme", "Yes to &All");
    case QPlatformDialogHelper::No:
        return QCoreApplication::translate("QPlatformTheme", "&No");
    case QPlatformDialogHelper::NoToAll:
        return QCoreApplication::translate("QPlatformTheme", "N&o to All");
    case QPlatformDialogHelper::Abort:
        return QCoreApplication::translate("QPlatformTheme", "Abort");
    case QPlatformDialogHelper::Retry:
        return QCoreApplication::translate("QPlatformTheme", "Retry");
    case QPlatformDialogHelper::Ignore:
        return QCoreApplication::translate("QPlatformTheme", "Ignore");
    case QPlatformDialogHelper::Close:
        return QCoreApplication::translate("QPlatformTheme", "Close");
    case QPlatformDialogHelper::Cancel:
        return QCoreApplication::translate("QPlatformTheme", "Cancel");
    case QPlatformDialogHelper::Discard:
        return QCoreApplication::translate("QPlatformTheme", "Discard");
    case QPlatformDialogHelper::Help:
        return QCoreApplication::translate("QPlatformTheme", "Help");
    case QPlatformDialogHelper::Apply:
        return QCoreApplication::translate("QPlatformTheme", "Apply");
    case QPlatformDialogHelper::Reset:
        return QCoreApplication::translate("QPlatformTheme", "Reset");
    case QPlatformDialogHelper::RestoreDefaults:
        return QCoreApplication::translate("QPlatformTheme", "Restore Defaults");
    default:
        break;
    }
    return QString();
}

3. 打开Qt Command Prompt,切换到$QTDIR/src/qtbase/bin目录下,执行下面的命令:

lupdate ../src/gui/kernel/qplatformtheme.cpp -ts  qplatformtheme.ts

4. 将qplatformtheme.ts文件中context节点(包括context)内容全部插入到前面的qt_zh_CN.ts中的末尾根节点</TS>之前
5. 修改qt_zh_CN.ts中新加入到这些内容并做翻译,最后翻译完类似下面的内容(但行号随着Qt版本的不同而不同):

<context>
    <name>QPlatformTheme</name>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="657"/>
        <source>OK</source>
        <translation>确定</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="659"/>
        <source>Save</source>
        <translation>保存</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="661"/>
        <source>Save All</source>
        <translation>全部保存</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="663"/>
        <source>Open</source>
        <translation>打开</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="665"/>
        <source>&Yes</source>
        <translation>是(&Y)</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="667"/>
        <source>Yes to &All</source>
        <translation>全是(&A)</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="669"/>
        <source>&No</source>
        <translation>否(&N)</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="671"/>
        <source>N&o to All</source>
        <translation>全否(&O)</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="673"/>
        <source>Abort</source>
        <translation>中止</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="675"/>
        <source>Retry</source>
        <translation>重试</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="677"/>
        <source>Ignore</source>
        <translation>忽略</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="679"/>
        <source>Close</source>
        <translation>关闭</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="681"/>
        <source>Cancel</source>
        <translation>取消</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="683"/>
        <source>Discard</source>
        <translation>丢弃</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="685"/>
        <source>Help</source>
        <translation>帮助</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="687"/>
        <source>Apply</source>
        <translation>应用</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="689"/>
        <source>Reset</source>
        <translation>重置</translation>
    </message>
    <message>
        <location filename="../src/gui/kernel/qplatformtheme.cpp" line="691"/>
        <source>Restore Defaults</source>
        <translation>还原默认值</translation>
    </message>
</context>

6. 最后一步了,生成qt_zh_CN.qm文件,在Qt Command Prompt中,切换到QTDIR目录,执行下面命令,即可生成新的qt_zh_CN.qm文件:

lrelease ../src/qttranslations/translations/qt_zh_CN.ts -qm translations/qt_zh_CN.qm


      这样,qt_zh_CN.qm生成好了,那在Qt程序中将这个qt_zh_CN.qm文件加载入翻译就可以了。

ded231 2016-05-13 09:08
赞一个!

闲来误事 2016-07-18 21:40
赞一个
正在学习多语言翻译,各种对话框,都搞不清楚修改哪里统一切换语言了


查看完整版本: [-- Qt 5.3.1 & Qt 5.5.1中带QMessageBox及QDialogButtonBox按钮翻译的qt_zh_CN.qm --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled