Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt linguist not get text from c++ code.



  • Hi all,
    How I can get text in Qt linguist from c++ model field in qml, can I do something like qsTr(model.name);?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why not just translate that in the model code ?



  • @SGaist said in Qt linguist not get text from c++ code.:

    Hi and welcome to devnet,
    Why not just translate that in the model code ?

    I need to add this variable in c++ model, and translate with Qt linguist with *.ts files, how I can add to .ts file my variable from C++ model? It's should be something like this:
    switch (role) {
    case TextRole:
    return tr(m_data.at(index.row()));

    after this we can lupdate all *.ts files, and it added new field from this function?


  • Lifetime Qt Champion

    How are you filling m_data ?



  • Actually, I have some .xml file with some name and phrase which I should to translate I getting this string phrase to some struct and use it in class with qmlRegisterType() going to qml, and have some getter with Q_PROPERTY(QString name READ name CONSTANT) and call it in qml text: name_of_register_type.name
    What I should to add to get this name in my generic .ts file for Qt Linguist?


  • Lifetime Qt Champion

    That's not the role of Linguist.

    Is that XML file something you change often ?



  • I don't change often this file it's something like config file with names and phrase for application.


  • Lifetime Qt Champion

    In that case you should either translate this XML file directly and load the appropriate one when switching your application language or generate a C++ file from it and then use Qt Linguist on that one.



  • Hi!

    @S_max07 You might wrap a text in XML file with syntax recognized by lupdate, i.e.:

    •<?xml version="1.0">
    <someKey>translate("someContext",  "text to translate")</someKey>
    

    when that key is handled by C++, trim it and then translate:

    QXmlStreamReader xml;
    if (xml.name() == QLatin1String("someKey"))
          auto key = QApplication::translate("someContext",  xml.readElementText()
                                        .replace(QLatin1String("translate(\"someContext\", \""), QString())
                                        .replace(QLatin1String("\")"), QString()).toLocal8Bit());
    

    and invoke lupdate which also looks up for *.xml extensions:

    lupdate -extensions cpp,h,qml,xml -recursive src_dir xml_dir -ts some_file.ts
    

Log in to reply