自动化自定义 QTranslator 上下文

发布于 2024-12-28 02:39:14 字数 481 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞳孔里扚悲伤 2025-01-04 02:39:14

您可以使用 Q_DECLARE_TR_FUNCTIONS() 宏应用于仅充当上下文的类定义:

class CONTEXT_CLASS {
    Q_DECLARE_TR_FUNCTIONS(CONTEXT_CLASS)
};

其中 CONTEXT_CLASS 可以如您所愿地短,假设 X (希望不会与代码中的任何其他内容)。这将使您的 tr() 语句

X::tr("source");

不要尝试 #define 来缩短 X::tr,因为翻译工具不会识别它。

You could use the Q_DECLARE_TR_FUNCTIONS() macro applied to a class definition that acts solely as a context:

class CONTEXT_CLASS {
    Q_DECLARE_TR_FUNCTIONS(CONTEXT_CLASS)
};

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

X::tr("source");

Don't try to #define something to shorten X::tr, as that won't get picked up by the translation tool.

茶花眉 2025-01-04 02:39:14

还有比这更容易的事情。使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文