Use of Unicode in Dynamic Translations related code.

  • Hello,

    I have used Unicode "\xE2\x84\x83" in my code.

    example: TextWidget *test = new TextWidget("\xE2\x84\x83", this); ----> it makes QString m_text = "\xE2\x84\x83" ;
    where m_text is a member variable of TextWidget.

    this TextWidget reacts to a languageChangeEvent.
    In Language change event it returns QObject::tr(m_text);

    In this case, it returns all question mark symbol. "????"

    in my translation file (.ts) I don't have any translation for this Unicode. also Ideally if tr doesn't find any translation then it returns QString::fromUtf8(string) but in my case, it returns all "?????"

    can anybody help for how to use Unicode in a dynamic translation environment?

    Thanks in advance.

  • QObject::tr(m_text);

    Does this even compile?
    The problem is not the non-asci chars, tr arguments must be the string literals to translate, not a variable.

  • Hi,

    Yes, it compiles. I do this

    QByteArray text = m_immutableText.toLocal8Bit();
    m_text = QObject::tr(text);

    Also, I found this solution for this symbol issue :

    QTextCodec *utfCodec = QTextCodec::codecForName("UTF-8"); //creating new utf-8 codec
    QTextCodec::setCodecForLocale(utfCodec); // setting the utf-8 codec for the tr() tags

  • Please read

    tr can't translate variables, it must be used only with literals

    m_text = QObject::tr(text); still doesn't compile, you are using m_text = QObject::tr(text.constData()); but it still can't work

Log in to reply