and*_*ref 5 c++ user-interface qt qt4 internationalization
有没有人设法从用户的语言设置中推断出正确的布局方向(从左到右和从右到左)?
我无法将我的应用程序本地化到阿拉伯语(沙特阿拉伯)语言环境.检测当前的语言环境,加载和安装适当的QTranslator都可以正常工作.(该文本在Linux上看起来很棒!)我遇到的问题是没有从系统区域设置中推断出全局布局方向.
QApplication :: layoutDirection的文档说明(带有我的亮点):
此属性保存此应用程序的默认布局方向.在系统启动时,默认布局方向取决于应用程序的语言.
但事实并非如此:无论系统区域设置如何,布局都设置为Qt :: LeftToRight.我制作了一个显示问题的测试程序.它的主要功能是:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTranslator translator;
translator.load(":/layoutDirection.qm");
a.installTranslator(&translator);
Window w;
w.show();
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
它还有一个包含必要图像和翻译文件的资源文件.它的定义如下:
<RCC>
<!-- Arabic resources. The arrow points to the left. -->
<qresource prefix="/" lang="ar">
<file alias="arrow.png">images/arrow-rtl.png</file>
<file alias="layoutDirection.qm">translations/layoutDirection_ar.qm</file>
</qresource>
<!-- English resources. The arrow points to the right. -->
<qresource prefix="/" lang="en">
<file alias="arrow.png">images/arrow-ltr.png</file>
<file alias="layoutDirection.qm">translations/layoutDirection_en.qm</file>
</qresource>
</RCC>
Run Code Online (Sandbox Code Playgroud)
Window类的源代码是微不足道的和相应的.ui文件不未设置的layoutDirection为任何部件.在英语(美国)语言环境中运行时,窗口如下所示:
英文截图显示从左到右的布局,正确的翻译和指向右侧的箭头图像http://i26.tinypic.com/25txmz6.png
当在阿拉伯语(沙特阿拉伯)语言环境中运行时,它看起来像这样:
阿拉伯语截图显示错误的从左到右布局,正确的翻译和指向右侧的箭头图像http://i28.tinypic.com/11gu7mx.png
如您所见,字符串的转换是正确的,箭头的图像指向右侧,这意味着加载了正确的资源.布局方向错误:它应该反映英文版面.
注意.由于我不懂阿拉伯语,因此示例文本是Google翻译的.如果不正确,我道歉.
Artyom的答案是正确的,最终让我发现了顶级窗口小部件如何从QApplication实例确定其布局方向.在使用Qt国际化手册说:
Qt itself contains over 400 strings that will also need to be translated into the languages that you are targeting. You will find translation files for French, German and Simplified Chinese in $QTDIR/translations, as well as a template for translating to other languages.
The QApplication code has an interesting method, too:
static bool qt_detectRTLLanguage()
{
return force_reverse ^
(QGuiApplication::tr("QT_LAYOUT_DIRECTION",
"Translate this string to the string 'LTR' in left-to-right"
" languages or to 'RTL' in right-to-left languages (such as Hebrew"
" and Arabic) to get proper widget layout.") == QLatin1String("RTL"));
}
Run Code Online (Sandbox Code Playgroud)
When the "QT_LAYOUT_DIRECTION" string is translated, the application will automatically detect its layout direction and everything works just fine:
Correct arabic layout on Linux http://i26.tinypic.com/zv352g.png
据我所知,布局方向应该在Qt中明确设置.
我知道,GTK应用程序自动做到这一点,Qt不,这是很好的事情.
我解释.假设您有以阿拉伯语或希伯来语语言环境启动的未翻译应用程序,从右到左显示所有布局会发生什么,并且所有内容看起来都很难看,并且对于RTL英语叮当来说并不自然.考虑使用RTL布局的未翻译IDE.
这样做的诀窍是定义一个特殊的翻译密钥,因此原始密钥具有值LTR,并且仅在翻译的应用程序中为阿拉伯语,巴黎语或希伯来语,它被翻译为RTL.
所以最好是这样的:
a.setLayoutDirection(tr("LTR")=="RTL" ? Qt::RightToLeft : Qt::LeftToRight)
Run Code Online (Sandbox Code Playgroud)
然后在翻译文件中明确定义方向.
所以我的好建议 - 来自希伯来语的演讲者,按照我展示的方式这样做,不要根据语言环境自动执行此操作,无论实际翻译如何.
归档时间: |
|
查看次数: |
3129 次 |
最近记录: |