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. Translate content from variable dynamically
Forum Updated to NodeBB v4.3 + New Features

Translate content from variable dynamically

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 683 Views
  • 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.
  • C Offline
    C Offline
    CloudL
    wrote on last edited by CloudL
    #1

    Hi friends,
    i need a way to translate the content of a variable which gets set at runtime.

    For example:

    QString textToTranslate = getTextFromSomewhere();
    
    QLabel *t = new QLabel(tr(textToTranslate));
    

    Now the label should show the translated text.
    For example if textToTranslate is "HOUSE" it should show "House" for english or "Haus" for german, but if textToTranslate becomes "TREE" because the getTextFromSomewhere() returns it at another call it should become "Tree" for english and "Baum" for german...

    Any idea how to handle this "dynamic" translations?

    Greetings
    Marco

    J.HilkJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @CloudL said in Translate content from variable dynamically:

      Any idea how to handle this "dynamic" translations?

      Qt is not google translate - so you have to add your words/sentences you want to translate to your translation file.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • C CloudL

        Hi friends,
        i need a way to translate the content of a variable which gets set at runtime.

        For example:

        QString textToTranslate = getTextFromSomewhere();
        
        QLabel *t = new QLabel(tr(textToTranslate));
        

        Now the label should show the translated text.
        For example if textToTranslate is "HOUSE" it should show "House" for english or "Haus" for german, but if textToTranslate becomes "TREE" because the getTextFromSomewhere() returns it at another call it should become "Tree" for english and "Baum" for german...

        Any idea how to handle this "dynamic" translations?

        Greetings
        Marco

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @CloudL once you loaded the new translation, you have to reset all strings you manually(in your code) set previously.

        Only string inside your UI files or qml files (qsTr() marked, may eventually be reset "automatically" after the translation is loaded.

        so any label created this way

        QString textToTranslate = getTextFromSomewhere();
        
        QLabel *t = new QLabel(tr(textToTranslate));
        

        has to either be recreated, or set via setText

        QString textToTranslate = getTextFromSomewhere();
        
        t->setText(tr(textToTranslate));
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1

        • Login

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