Important: Please read the Qt Code of Conduct -

[Solved] Issue with translation tr("string")

  • Hi

    I am successfully using linguist for translating all the labels on buttons for a small app, which works perfectly.
    I am now trying to translate some strings by wrapping tr:

    @DebugShow(tr("IP address of module: ") + GetLocalIP(), DBG_DIRECT, Qt::black, Qt::white);@

    DebugShow is a function that uses the given arguments to format as simple html and send to a QPlainTextEdit which works perfect, just not translated.
    Linguist does show the string, I translated it, but the translation does not appear.
    Any idea?

  • Hi,
    I do not believe you are able to copy the string to each other with the + operator. To use the linquist you need to use .arg option:
    QString MyString(tr("Your text here: %1).arg(AddedString));

  • .. I've rerun lupdate and lrelease many times ..

    I've used a QPlainTextEdit because this is the easiest way to have the "latest" 100 (settable with maximumBlockcount) messages stored scrollable in FIFO style in a debugwindows and be able to have colored messages (foreground/background) and further attributes for grouping and showing importance.
    IMHO, this works perfect and is a good thing.

  • Hi, See my editted last post. use the .arg option for QString.

  • That is not the issue. Concatenate with the + works perfect. The issue is the same with this as well:

    @DebugShow(tr("BlaBlaBal"), DBG_DIRECT, Qt::black, Qt::white);@

  • Does only this string not work? Or do all non gui string fail? If so, it would seem to be a install of your translator. Do you set the translator run-time in an menu action? Do you set it before MainWindow is created?
    Did you implement a languageEvent handler??

  • Is the translator definitely installed? You have loaded it with the right translation file, and it didn't return any error values?

  • Jeroen,
    That was the correct hint.

    The calls like
    @DebugShow(tr("BlaBlaBal"), DBG_DIRECT, Qt::black, Qt::white);@
    are in the constructor:

    ui(new Ui::QTGUI_MainWindow),

    as well as being called later from code.
    The calls later from code are working. It's just that the translator is not yet active when the MainWindow is created. I'll need to change this .. somehow.

    From main:
    @ MyApplication program(argc, argv);
    QDesktopWidget *desk = program.desktop();
    QTGUI_MainWindow window;


    return program.exec();@

    setLanguage installs (or removes/reinstalls) the Translator.

    Thanks so far.

  • In my code, I load the translator at the earliest opportunity; directly after the QApplication is created.

    If your code to load the translator is part of the class function setLanguage, that's too late. Load the translator before creating your MainWindow.

  • I've moved it to before the MainWindow is created ... and it now works :-)
    Thank you guys!

Log in to reply