Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Problem with tr an ä,ü,ö
Forum Updated to NodeBB v4.3 + New Features

[Solved] Problem with tr an ä,ü,ö

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 6.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gandiii
    wrote on last edited by
    #1

    Hello,

    I want to change the Text of a label
    with ui.llabel->changeText(tr("test1")) and also with
    words that contain a ä,ü or ö (I don't know the english word
    for this letters). But then I get some other letters.
    And if I try QString::fromUtf8(tr("test1")), that don't work,
    because the fromUtf8 function wants a const string.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      How about:
      @
      tr(QString::fromUtf8(“test1”))
      @

      Or simply use the UTF chars with tr, without any reference to QString.

      (Z(:^

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gandiii
        wrote on last edited by
        #3

        tr() wants a const QString and fromUtf8 returns a QString.
        Casting also dont work.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Are your source files encoded in UTF-8? Maybe that is why you used to get "then I get some other letters".

          I guess Volker would be better here, he has experience with tr() and UTF-8. I usually code in English only.

          (Z(:^

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            Yucel
            wrote on last edited by
            #5

            You have to make sure your files are encoded in UTF-8.

            Also add following:

            here is part of my main.cpp:

            @
            //-----------------------------------------------------------
            #include <qtextcodec.h>

            int main(int argc, char *argv[])
            {

            QTextCodec::setCodecForTr( QTextCodec::codecForName("UTF-8") );
            QTextCodec::setCodecForCStrings( QTextCodec::codecForName("UTF-8") );
            //-----------------------------------------------------------
            @

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DerManu
              wrote on last edited by
              #6

              Hm, usually the non-ASCII characters should come in with the translation files, and not stand hardcoded in the source file. So write your hardcoded strings in pure ASCII in the source file. If any translation (even the english one) needs non-ASCII characters somewhere, do it with the translation file. Until then it is sensible to replace ä ö ü with ae oe ue and tell the translator(s) to make sure they convert them to umlauts (<- that's the english word for them).

              If you insist on using non-ASCII characters in your source file, use trUtf8. Maybe a better alternative would be to escape them and use regular tr. For example tr("\304") will return Ä. (That's the octal code for the UTF-8-character Ä).
              Those are the codes in hex (0x00C4 = 304 (octal)):
              Ä U+00C4
              Ö U+00D6
              Ü U+00DC
              ä U+00E4
              ö U+00F6
              ü U+00FC

              //EDIT: @Yucel: Well if you use UTF-8 Encoding (which is highly recommended) you shouldn't set your codecs to ISO-8859-9 but to UTF-8. Also on Windows.

              1 Reply Last reply
              0
              • Y Offline
                Y Offline
                Yucel
                wrote on last edited by
                #7

                you are right, I used it as an example from one of my projects. Should have replaced it, before posting.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gandiii
                  wrote on last edited by
                  #8

                  thank you for your responses. I tried it with trUtf8 and
                  it works fine.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved