[SOLVED]How to display strings in language other than english



  • This program works fine, if i copy a string suppose in arabic "عدنان" in to textbox, pushing the button displays it in arabic in the label.
    @void MainWindow::on_pushButton_clicked()
    {
    ui->label->setText(ui->lineEdit->text());
    }@

    But if I do something like this:
    @void MainWindow::on_pushButton_clicked()
    {
    QString name("عدنان");
    ui->label->setText(name);
    }@

    It displays some garbled text!



  • This is because the implicit C string - QString conversion uses Latin-1 by default.

    There are many solutions to choose from:

    1. Internationalization

    Use Latin-character strings exclusively in your source, and use tr() to wrap string literals you wish to translate. Then, provide a translation file. This is best suited if you wish to (or plan to) make a multi-lingual application.

    See "documentation":http://doc.qt.digia.com/latest/internationalization.html

    1. Encode the C string explicitly

    Example:

    @
    /* This file should be saved with UTF-8 */
    #include <QApplication>
    #include <QLabel>
    #include <QTextCodec>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");

    QLabel l(codec->toUnicode("عدنان"));
    l.show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    You might want to use this approach if your program is one-shot. But this really is not a good choice in most cases.

    3) Overwrite the default C string - QString conversion rule

    Usually an appropriate alternative to method 2. Example:

    @
    /* This file should be saved with UTF-8 */
    #include <QApplication>
    #include <QLabel>
    #include <QTextCodec>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

    QLabel l("عدنان");
    l.show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    setCodecForCStrings() needs to be called only once in your application, and it overwrites all string conversion codec rule in the application.

    And you can use encodings other than UTF-8, of course. You just need to match the codec used with the encoding of your source file(s).



  • [quote author="uranusjr" date="1349562294"]
    And you can use encodings other than UTF-8, of course. You just need to match the codec used with the encoding of your source file(s).[/quote]

    The full story is actually a bit more complicated as it involves the execution charset, not the source charset. But most of the times the mapping is 1:1.

    http://qt-project.org/wiki/Strings_and_encodings_in_Qt



  • QString::fromUtf8() saves the day. There is also QObject::trUtf8() if you are planning to internationalize. uranusjs's answer involving QTextCodec is also great.



  • Thanks a ton! It worked like a charm.
    @QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.