Results 1 to 5 of 5

Thread: Arabic text rendering; issues with parentheses

  1. #1
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Arabic text rendering; issues with parentheses

    When I set the current language to Arabic in my UI, just for testing purposes, I see that some English strings (displayed in a language selection dialog, if you're wondering why) are rendered in a way I'm not sure will be appropriate. With the non-Arabic translations, for example, a string that would display as "English (United Kingdom)" will be rendered as "(English (United Kingdom" when the Arabic translator is loaded.

    Does anyone know an appropriate way to handle this?

    These strings are added to a QComboBox by asking a QTranslator for the translation of the word "English" (something I've used as a hook for finding the name of the language provided by the translation (just as the 'i18n' example did)):

    Qt Code:
    1. QComboBox* languageCombo = new QComboBox(this);
    2.  
    3. Translator test;
    4. QStringList translations = test.availableTranslations();
    5. QString translation;
    6. foreach (translation, translations) {
    7. // Remove .qm suffix
    8. translation.chop(3);
    9. test.load(translation);
    10. languageCombo->addItem(test.languageName(), translation);
    11. }
    12.  
    13. Client* client = Client::instance();
    14.  
    15. languageCombo->setCurrentIndex(
    16. languageCombo->findText(client->translator()->languageName()));
    To copy to clipboard, switch view to plain text mode 

    Where languageName() just has:

    Qt Code:
    1. return translate("Translator", SourceLanguage, 0, -1);
    To copy to clipboard, switch view to plain text mode 

    Where SourceLanguage is just

    Qt Code:
    1. static const char* SourceLanguage = QT_TRANSLATE_NOOP("Translator", "English");
    To copy to clipboard, switch view to plain text mode 

    Any insights much appreciated.

  2. #2
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Arabic text rendering; issues with parentheses

    Looked in to it a bit more, and found the stuff about determining layout direction of particular translations http://www.qtcentre.org/threads/1137...-autodetection, so this leads me to wonder:

    Are the item text and item data by default stored in columns 0 and 1 of the default model of QComboBox?

    Currently I have the name of the translation file set as the 'item data', so is there any more of a shortcut way to setting the layout direction for particular items in the combo box than by setting a custom item delegate, loading a QTranslator for this item data, and checking the translation for "QT_LAYOUT_DIRECTION" (or whatever other key I use myself).

    Since I've loaded the translator in putting the items in the list, it just seems a waste to have a delegate check this information every time the items are displayed.

    If it is necessary that I use a custom delegate, I'm not even quite sure how to customise this aspect of the display: I've taken a look at the QAbstractItemDelegate documentation and seen it's example paint() method, which calls

    Qt Code:
    1. QApplication::style()->drawControl(QStyle::CE_ProgressBar,
    2. &progressBarOption, painter);
    To copy to clipboard, switch view to plain text mode 

    To draw a progress bar... how would I set LTR or RTL rendering for the item text of the items in a QComboBox?

    ... Actually, just looked further and found QStyle::CE_ComboBoxLabel, which apparently takes a regular QStyleOption (since it's not in the table in drawControl()'s docs...whew), and I see that has a QLayoutDirection member. I suppose that's right?

    So, I suppose any shortcut that may exist (outside setting a custom delegate, which seems wasteful) would be through setting a QStyle::CE_ComboBoxLabel QStyleOption on particular items that I add to the combo box.

    Is there any way to do this?

    EDIT:

    I see that someone has noted here http://comments.gmane.org/gmane.comp....general/31052 that "However, If you want to do this for a non loadede language, IMHO, loading the translation for that language is an overkill, and your hack is 'good enough'.", which is why it seems preferable to do this outside a delegate.
    Last edited by TropicalPenguin; 2nd February 2011 at 18:23.

  3. #3
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Arabic text rendering; issues with parentheses

    For anyone interested, I reached a solution, described below (took half a day to figure out *sigh*, but then I'm new to this).

    I installed a custom delegate on the combo box that would render the strings of LTR languages LTR, and the strings of RTL languages RTL.

    To do this, rather than just setting the path of the translation as the item data, I set a custom structure containing the path and a boolean value (since I'd loaded the translation to add it to the combo box, I didn't want to load it every time the delegate painted to figure out which direction the translation was).

    This structure of course had to be registered with QMetaType using Q_REGISTER_METATYPE, so that it would work with QVariant.

    Finally, the delegate was a subclass of QStyledItemDelegate, with the contents of its paint() method as follows:

    Qt Code:
    1. initStyleOption(&directionOption, index);
    2. directionOption = option;
    3.  
    4. InterfaceTranslationInfo info = qvariant_cast<InterfaceTranslationInfo> (index.data(Qt::UserRole));
    5. if (info.rtl) {
    6. directionOption.direction = Qt::RightToLeft;
    7. } else {
    8. directionOption.direction = Qt::LeftToRight;
    9. }
    10.  
    11. QStyledItemDelegate::paint(painter, directionOption, index);
    To copy to clipboard, switch view to plain text mode 

    Seems easy, but met a lot of dead-ends on the way. If anyone could point me to better ways of doing this, it would still be much appreciated, but perhaps this can be of use to someone.

  4. #4
    Join Date
    Jul 2010
    Posts
    41
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Arabic text rendering; issues with parentheses

    Quote Originally Posted by TropicalPenguin View Post
    ... for example, a string that would display as "English (United Kingdom)" will be rendered as "(English (United Kingdom" when the Arabic translator is loaded.
    one solution is using "Unicode control character". For this example I mean "LRM".

    LRM:
    Code:U+200E LEFT-TO-RIGHT MARK: Left-to-right zero-width character

    for example:
    WITHOUT <LRM>:
    ‫زبان English (United Kingdom) انگلیسی

    WITH <LRM>:
    ‫زبان English (United Kingdom)‎ انگلیسی

    I use <LRM> after closed parenthesis:
    ‫زبان English (United Kingdom)<LRM>‎ انگلیسی

    for more info. on bidirectional algorithm see this:
    Unicode Standard Annex #9: UNICODE BIDIRECTIONAL ALGORITHM

  5. The following user says thank you to srazi for this useful post:

    TropicalPenguin (5th February 2011)

  6. #5
    Join Date
    Oct 2009
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Arabic text rendering; issues with parentheses

    Quote Originally Posted by srazi View Post
    one solution is using "Unicode control character". For this example I mean "LRM".

    LRM:
    Code:U+200E LEFT-TO-RIGHT MARK: Left-to-right zero-width character

    for example:
    WITHOUT <LRM>:
    ‫زبان English (United Kingdom) انگلیسی

    WITH <LRM>:
    ‫زبان English (United Kingdom)‎ انگلیسی

    I use <LRM> after closed parenthesis:
    ‫زبان English (United Kingdom)<LRM>‎ انگلیسی

    for more info. on bidirectional algorithm see this:
    Unicode Standard Annex #9: UNICODE BIDIRECTIONAL ALGORITHM


    Thanks for the reply. LRM is causing eliding issue.
    With out LRM string eliding is correct on right side if string is too long to fit in the given widget.
    After inserting LRM "(" ")" open close parenthesis is working fine. But ellipsis are getting added on left side which is not giving correct meaning for an Arabic reading person.

    Is it possible to resolve this with out using LRM?

Similar Threads

  1. Text Edit rendering
    By VirtualRealty in forum Qt Programming
    Replies: 2
    Last Post: 1st October 2010, 17:16
  2. Text clipped when rendering svg.
    By ocratato in forum Newbie
    Replies: 1
    Last Post: 20th June 2010, 10:03
  3. Replies: 0
    Last Post: 30th November 2008, 16:59
  4. text rendering problem
    By vno4697 in forum Qt Programming
    Replies: 0
    Last Post: 12th August 2008, 14:44
  5. Object::connect: Parentheses expected, slot...
    By bnilsson in forum Qt Programming
    Replies: 5
    Last Post: 5th April 2008, 15:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.