阿拉伯语的布局方向不是根据语言环境确定的(Mac和Linux) [英] Layout direction for Arabic is not determined based on the locale (Mac and Linux)

查看:318
本文介绍了阿拉伯语的布局方向不是根据语言环境确定的(Mac和Linux)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Has anyone managed to have the correct layout direction (left-to-right and right-to-left) be inferred from the user’s language settings?

我在将应用程序本地化为阿拉伯语(沙特阿拉伯)语言环境时遇到麻烦.检测当前语言环境,以及加载和安装适当的QTranslators都可以正常工作. (在Linux上,文本看起来很棒!)我遇到的问题是,无法从系统区域设置推断出全局布局方向.

I am having trouble localizing my application to the Arabic (Saudi Arabia) locale. Detection of the current locale, and loading and installing the appropriate QTranslators both work fine. (The text looks great on Linux!) The issue I have is that the global layout direction is not being inferred from the system locale.

QApplication :: layoutDirection 状态的文档(带有我的重点):

The documentation for QApplication::layoutDirection states (with my highlights):

此属性保存此应用程序的默认布局方向.在系统启动时,默认布局方向取决于应用程序的语言.

这不会发生,但是:无论系统区域设置如何,布局都设置为 Qt :: LeftToRight .我制作了一个显示问题的测试程序.它的主要功能是:

This is not what happens, however: the layout is set to Qt::LeftToRight regardless of the system locale. I made a test program that displays the problem. It’s main function is:

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

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

It also has a resource file containing the necessary images and translation files. It is defined like this:

<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.在英语(美国)语言环境上运行时,窗口如下所示:

The source code of the Window class is trivial and the corresponding .ui file does not set the layoutDirection for any widget. When run on the English (United States) locale, the window looks like this:

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

and when run on the Arabic (Saudi Arabia) locale, it looks like this:

如您所见,字符串的翻译是正确的,箭头图像指向右侧,这意味着已加载正确的资源.布局方向是错误的:它应反映英语布局.

As you can see, the translation of the strings is correct and the image of the arrow points to the right, meaning that the correct resources were loaded. The layout direction is wrong: it should mirror the English layout.

注意.由于我不懂阿拉伯语,因此示例文字是Google翻译的.如果不正确,我深表歉意.

Note. Since I don’t know the Arabic language, the example text is Google-translated. I apologize if it is incorrect.

Artyom 的答案是正确的,最终使我发现了顶级Widget如何从QApplication实例. Qt国际化手册说:

Artyom's answer is correct and eventually led me to discovering how top-level Widgets determine their layout direction from the QApplication instance. The Internationalization with Qt manual says:

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

Qt itself contains several thousands of strings that will also need to be translated into the languages that you are targeting. You will find a number of translation files in the qttranslations repository.

QApplication代码也有一个有趣的方法:

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

翻译"QT_LAYOUT_DIRECTION"字符串后,应用程序将自动检测其布局方向,并且一切正常:

When the "QT_LAYOUT_DIRECTION" string is translated, the application will automatically detect its layout direction and everything works just fine:

推荐答案

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

As far as I know the layout direction should be set explicitly in Qt.

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

I know that GTK application do this automatically and qt does not and this is good thing.

我解释.假设您已经以阿拉伯语或希伯来语语言环境启动了未翻译的应用程序,那么会发生从右到左显示的所有布局,并且一切看上去都很丑陋,而使用RTL英语刺痛的情况却不自然的情况.考虑一下带有RTL布局的未翻译IDE.

I explain. Suppose you have untranslated application started in Arabic or Hebrew locale, what happens all layout displayed from right to left and everything looks ugly and not natural with RTL English stings. Think of untranslated IDE with RTL layout.

做到这一点的诀窍是定义一个特殊的翻译键,因此原始键具有LTR值,并且仅在翻译成阿拉伯语,巴黎语或希伯来语的应用程序中才将其翻译成RTL.

The trick to do this right, is to define a special translation key, so the original key has value LTR and only in translated applications to Arabic, Parisian or Hebrew it is translated to RTL.

所以最好是这样的

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

然后是翻译文件中明确定义的方向.

And then the direction defined explicitly in the translation file.

因此,我的好建议-来自希伯来语的演讲者,请按照我的显示方式进行操作,不要尝试根据区域设置自动进行操作,而不论实际翻译如何.

So my good advice - from Hebrew speaker, do this the way I shown and don't try to do this automatically according to locale and regardless of actual translation.

这篇关于阿拉伯语的布局方向不是根据语言环境确定的(Mac和Linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆