自动化自定义 QTranslator 上下文
Qt lupdate 和 QTranslator 将源字符串分组到独占上下文中。这意味着在一个上下文中定义的翻译将无法在不同的上下文中访问。
C++ 中的默认上下文是已重写 QObject::tr()
的类的名称。声明式 QML 内的默认上下文是不带扩展名的当前文件名。要覆盖翻译上下文,可以在 C++ 中使用 qApp->translate( "context", "source" )
或 qsTranslate( "context", "source" )
或 QML。
我希望能够在大型项目中使用单个通用翻译上下文,并且我发现为每个翻译函数指定翻译上下文非常乏味。是否有任何现有或未来的 Qt 翻译框架扩展可以简化此任务?我正在寻找像 tr( "source" )
和 qsTr( "source" )
一样简单的东西,但使用系统范围或项目范围的默认值语境。有什么想法吗?
Qt lupdate and QTranslator group source strings into exclusive contexts. This means that a translation defined in one context will not be accessible within a different context.
The default context inside C++ is the name of a class that has overridden QObject::tr()
. The default context inside declarative QML is the current filename without extension. To override the translation context, one would use qApp->translate( "context", "source" )
or qsTranslate( "context", "source" )
in C++ or QML.
I want to be able to use a single common translation context across a large project and I am finding that specifying the translation context with every single translation function is very tedious. Is there any existing or future Qt translation framework extensions that would simplify this task? I am looking for something that would be as simple as tr( "source" )
and qsTr( "source" )
, but use a system-wide or project-wide default context. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Q_DECLARE_TR_FUNCTIONS() 宏应用于仅充当上下文的类定义:
其中 CONTEXT_CLASS 可以如您所愿地短,假设 X (希望不会与代码中的任何其他内容)。这将使您的 tr() 语句
不要尝试 #define 来缩短 X::tr,因为翻译工具不会识别它。
You could use the Q_DECLARE_TR_FUNCTIONS() macro applied to a class definition that acts solely as a context:
where CONTEXT_CLASS could be as short as you'd like, let's say X (hoping that doesn't conflict with anything else in your code). That would make your tr() statements
Don't try to #define something to shorten X::tr, as that won't get picked up by the translation tool.
还有比这更容易的事情。使用 qtTrId/qsTrId (Qt/QML) 而不是 tr/qsTr 并将 -idbased 参数添加到 lrelease 调用中。基于 ID 的翻译根本没有上下文。
There's something easier than that. Use qtTrId/qsTrId (Qt/QML) instead of tr/qsTr and add the -idbased parameter to your lrelease calls. ID based translation has no context at all.