用Qt creator找到翻译的目录
Find the directory of a translation with Qt creator
我是Qt creator的新手,我最近学习了如何使用Qt linguist。当我想要加载翻译时,我需要指定一个在我的计算机上没有问题的目录。但是当我把这个Qt项目给别人的时候,目录改变了,翻译没有出现。
我尝试使用不同的方法,如QDir::currentPath(),但它给了我包含应用程序的构建文件夹的路径。我需要访问的是项目所在的文件夹与翻译的文件和翻译的二进制文件(file_fr.)。qm file_es.qm)。
提前感谢您的帮助:-)
解决方案:
为了获得可执行文件的路径,使用以下方法: QCoreApplication::applicationDirPath()
例子:
您正在部署位于可执行路径的lang
文件夹中的翻译文件:
const QString LANGPATH(QCoreApplication::applicationDirPath() + "/lang");
QTranslator *translator = new QTranslator(this);
translator->load("file_fr.qm", LANGPATH);
QCoreApplication::installTranslator(translator);
你的错误:
您使用的是QDir::currentPath()
,但其结果是当前工作目录,而不是包含您的可执行文件的目录。
我设法用Qt资源系统做到了这一点,如果你正在寻找一个简短的教程,你可以找到一个解释如何设置资源的链接。
这是我的代码,如果它可以帮助你:
#include <QApplication>
#include "ClassGenerator.h"
#include "ClassCode.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QString locale = QLocale::system().name().section('_', 0, 0);
QTranslator translator;
translator.load(":/Translation/TPClassGenerator_"+locale+".qm");
app.installTranslator(&translator);
ClassGenerator Window;
Window.show();
return app.exec();
}
正如您所看到的,系统的语言被检索并用于在文件夹资源中选择正确的翻译。
谢谢你的建议:-)
相关文章:
- 需要将一行代码从C++ Qt翻译成PyQt
- 在Visual Studio中更新Qt翻译(.ts文件)时出现未指定错误
- QT QML 翻译重新翻译
- QT 将文本翻译成特定的区域设置
- QT在所有上下文中翻译搜索
- QT-使用内置翻译
- 如何在Qt小部件应用程序中翻译外部文件中的字符串?
- 如何使用 QTableWidgetItem 文本在 QT 中翻译语言翻译
- 如何在 QT 的语言翻译中使用 & 字符串
- 是否可以动态翻译QT应用程序而无需复制代码
- 可选的QT字符串翻译
- 如何翻译使用预构建的QT安装程序框架创建的安装程序,无论OS语言如何
- 即时翻译Qt QML应用程序
- QT 如何查找未翻译的字符串
- 翻译枚举值qt的字符串表示
- CMAKE QT翻译系统的适当工作流程
- 使用Qt的国际化,则翻译"dynamic"内容
- 处理Qiloader时的Qt翻译问题
- Qt插件翻译出现问题
- 如何动态翻译qt应用程序