阿拉伯语的布局方向不是基于区域设置确定的(Mac和Linux)

时间:2010-07-29 18:21:12

标签: c++ user-interface qt internationalization qt4

是否有人设法从用户​​的语言设置中推断出正确的布局方向(从左到右和从右到左)?

我无法将我的应用程序本地化到阿拉伯语(沙特阿拉伯)语言环境。检测当前的语言环境,加载和安装适当的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();
}

它还有一个包含必要图像和翻译文件的资源文件。它的定义如下:

<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>

Window类的源代码很简单,相应的.ui文件为任何小部件设置layoutDirection。在英语(美国)语言环境中运行时,窗口如下所示:

English screenshot showing left-to-right layout, proper translation, and image of arrow pointing to the right

当在阿拉伯语(沙特阿拉伯)语言环境中运行时,它看起来像这样:

Arabic screenshot showing wrong left-to-right layout, proper translation, and image of arrow pointing to the right

正如您所看到的,字符串的转换是正确的,箭头的图像指向右侧,这意味着加载了正确的资源。布局方向错误:它应该反映英文版面。

注意。由于我不懂阿拉伯语,因此示例文本是Google翻译的。如果不正确,我道歉。

解决方案

Artyom的答案是正确的,最终让我发现顶级窗口小部件如何从QApplication实例确定其布局方向。 Internationalization with Qt手册说:

  

Qt本身包含数千个字符串,这些字符串也需要翻译成您所定位的语言。您将在qttranslations存储库中找到许多翻译文件。

QApplication code也有一个有趣的方法:

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"));
}

当翻译“QT_LAYOUT_DIRECTION”字符串时,应用程序将自动检测其布局方向,一切正常:

Correct arabic layout on Linux

2 个答案:

答案 0 :(得分:8)

据我所知,布局方向应在Qt中明确设置。

我知道GTK应用程序会自动执行此操作而qt不执行此操作,这是良好的事情。

我解释一下。假设您有以阿拉伯语或希伯来语语言环境启动的未翻译应用程序,从右到左显示所有布局会发生什么,并且所有内容看起来都很丑陋且不自然而且RTL英语叮咬。考虑使用RTL布局的未翻译IDE。

正确执行此操作的诀窍是定义一个特殊的翻译密钥,因此原始密钥具有值LTR,并且仅在已翻译的应用程序中使用阿拉伯语,巴黎语或希伯来语,它将被翻译为RTL。

所以最好是这样的:

a.setLayoutDirection(tr("LTR")=="RTL" ? Qt::RightToLeft : Qt::LeftToRight)

然后在翻译文件中明确定义方向。

所以我的好建议 - 来自希伯来语的发言人,按我所示的方式这样做,不要根据语言环境自动执行此操作,无论实际翻译如何。

答案 1 :(得分:0)

订单是否重要......例如,这是否有效:

<qresource prefix="/" lang="ar">
    <file alias="layoutDirection.qm">translations/layoutDirection_ar.qm</file>
    <file alias="arrow.png">images/arrow-rtl.png</file>
</qresource>

而不是:

<qresource prefix="/" lang="ar">
    <file alias="arrow.png">images/arrow-rtl.png</file>
    <file alias="layoutDirection.qm">translations/layoutDirection_ar.qm</file>
</qresource>