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. Loading qm file from ressources fails
Forum Updated to NodeBB v4.3 + New Features

Loading qm file from ressources fails

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 433 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.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by
    #1

    I try to load a qm file from the ressource system but it fails.

    The code:

    LanguageSelector::LanguageSelector(QObject *parent)
        : QObject{parent}, mTranslator{new QTranslator{this}}
    {
        loadEnglish();
    }
    
    void LanguageSelector::loadEnglish()
    {
        QFile file(":/translations/quiz.en.qm");
        if (!file.open(stdout, QIODevice::ReadOnly)) {
            qDebug() << "Can't find it!";
        }
    
        if (!mTranslator->load(QLocale(QLocale::English), QLatin1String("quiz.en"),
                               QLatin1String(":/translations"))) {
            qDebug() << "load english failed";
        }
    }
    

    Strangely I don't get an error on the QFile check as expected but loading the file with QTranslator fails. What could be the Issue?

    The file with the code is in the folder:

    /src

    The ressource file for the languages is directlyy in root:

    translations.qrc

    The translations file:

    <RCC>
        <qresource prefix="/">
            <file>translations/quiz.de.qm</file>
            <file>translations/quiz.en.qm</file>
            <file>translations/quiz.es.qm</file>
        </qresource>
    </RCC>
    
    Pablo J. RoginaP 1 Reply Last reply
    0
    • S sandro4912

      I try to load a qm file from the ressource system but it fails.

      The code:

      LanguageSelector::LanguageSelector(QObject *parent)
          : QObject{parent}, mTranslator{new QTranslator{this}}
      {
          loadEnglish();
      }
      
      void LanguageSelector::loadEnglish()
      {
          QFile file(":/translations/quiz.en.qm");
          if (!file.open(stdout, QIODevice::ReadOnly)) {
              qDebug() << "Can't find it!";
          }
      
          if (!mTranslator->load(QLocale(QLocale::English), QLatin1String("quiz.en"),
                                 QLatin1String(":/translations"))) {
              qDebug() << "load english failed";
          }
      }
      

      Strangely I don't get an error on the QFile check as expected but loading the file with QTranslator fails. What could be the Issue?

      The file with the code is in the folder:

      /src

      The ressource file for the languages is directlyy in root:

      translations.qrc

      The translations file:

      <RCC>
          <qresource prefix="/">
              <file>translations/quiz.de.qm</file>
              <file>translations/quiz.en.qm</file>
              <file>translations/quiz.es.qm</file>
          </qresource>
      </RCC>
      
      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

      @sandro4912 said in Loading qm file from ressources fails:

      mTranslator->load(QLocale(QLocale::English), QLatin1String("quiz.en"), QLatin1String(":/translations"))) {
      

      you may want to check the documentation of QTranslator::load() method

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      S 1 Reply Last reply
      0
      • Pablo J. RoginaP Pablo J. Rogina

        @sandro4912 said in Loading qm file from ressources fails:

        mTranslator->load(QLocale(QLocale::English), QLatin1String("quiz.en"), QLatin1String(":/translations"))) {
        

        you may want to check the documentation of QTranslator::load() method

        S Offline
        S Offline
        sandro4912
        wrote on last edited by
        #3

        @Pablo-J-Rogina

        I checked it but I could not find an issue

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sandro4912
          wrote on last edited by
          #4

          I missed stating the separator in the method. Like this it works:

          void LanguageSelector::loadLanguage(const QLocale::Language &newLanguage)
          {
              if (!mTranslator->load(QLocale(newLanguage), QLatin1String("quiz"),
                                     QLatin1String("."),
                                     QLatin1String(":/translations"))) {
                  auto metaEnum = QMetaEnum::fromType<QLocale::Language>();
                  qDebug() << tr("load language %1 failed")
                                  .arg(metaEnum.valueToKey(newLanguage));
              }
              else {
                  qDebug() << mTranslator->filePath();
              }
          }
          
          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