Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with plurals (tr())
Qt 6.12 LTS is out! See what's new in the release blog

Problem with plurals (tr())

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.4k 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.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by Perdrix
    #1

    In my source code I have:

    		QString message{ tr("Do you really want to permanently erase %n file(s)?\n"
    			"This operation cannot be reversed or cancelled.",
    			"IDS_WARNING_ERASEFILES", selectedRows.size()) };
    

    I also have a DSS.en.ts

    Which has an entry:

    08c7b568-64e9-48c3-9077-c2076c1e45c4-image.png

    but when I run the application I get:

    6f682e7b-275d-4ba1-9dca-2ecf2c00af55-image.png

    Clearly I'm doing something wrong but I've no clue what!

    This code is called before I create the QMainWindow:

    	//
    	// Retrieve the Qt language name (e.g.) en_GB
    	//
    	language = settings.value("Language").toString();
    
    	//
    	// Language was not defined in our preferences, so select the system default
    	//
    	if (language == "")
    	{
    		language = QLocale::system().name();
    	}
    
    	QTranslator appTranslator;
    	//
    	// Install the language if it actually exists.
    	//
    	if (appTranslator.load("DSS." + language, ":/i18n/"))
    	{
    		QCoreApplication::instance()->installTranslator(&appTranslator);
    	}
    

    There's no data stored for "Language" in the settings (empty registry string), and under the debugger, the language value returned by QLocale::system().name() was "en_GB" which I though would result in the .en translation being loaded

    David

    Christian EhrlicherC 1 Reply Last reply
    0
    • PerdrixP Perdrix

      @Christian-Ehrlicher

      Because that's what the documentation says:

      bool QTranslator::load(const QString &filename, const QString &directory = QString(), const QString &search_delimiters = QString(), const QString &suffix = QString())
      Loads filename + suffix (".qm" if the suffix is not specified), which may be an absolute file name or relative to directory. Returns true if the translation is successfully loaded; otherwise returns false.
      If directory is not specified, the current directory is used (i.e., as currentPath()).
      The previous contents of this translator object are discarded.
      If the file name does not exist, other file names are tried in the following order:

      1. File name without suffix appended.
      2. File name with text after a character in search_delimiters stripped ("_." is the default for search_delimiters if it is an empty string) and suffix.
      3. File name stripped without suffix appended.
      4. File name stripped further, etc.

      For example, an application running in the fr_CA locale (French-speaking Canada) might call load("foo.fr_ca", "/opt/foolib"). load() would then try to open the first existing readable file from this list:

      /opt/foolib/foo.fr_ca.qm
      /opt/foolib/foo.fr_ca
      /opt/foolib/foo.fr.qm
      /opt/foolib/foo.fr
      /opt/foolib/foo.qm
      /opt/foolib/foo

      David

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @Perdrix said in Problem with plurals (tr()):

      File name with text after a character in search_delimiters stripped ("_." is the default for search_delimiters if it is an empty string) and suffix.

      baaah, I hate this stupid automatic search for every possibility.

      You're correct.

      Do you load it in your main.cpp? Otherwise appTranslator will go out of scope.

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

      PerdrixP 1 Reply Last reply
      0
      • PerdrixP Perdrix

        In my source code I have:

        		QString message{ tr("Do you really want to permanently erase %n file(s)?\n"
        			"This operation cannot be reversed or cancelled.",
        			"IDS_WARNING_ERASEFILES", selectedRows.size()) };
        

        I also have a DSS.en.ts

        Which has an entry:

        08c7b568-64e9-48c3-9077-c2076c1e45c4-image.png

        but when I run the application I get:

        6f682e7b-275d-4ba1-9dca-2ecf2c00af55-image.png

        Clearly I'm doing something wrong but I've no clue what!

        This code is called before I create the QMainWindow:

        	//
        	// Retrieve the Qt language name (e.g.) en_GB
        	//
        	language = settings.value("Language").toString();
        
        	//
        	// Language was not defined in our preferences, so select the system default
        	//
        	if (language == "")
        	{
        		language = QLocale::system().name();
        	}
        
        	QTranslator appTranslator;
        	//
        	// Install the language if it actually exists.
        	//
        	if (appTranslator.load("DSS." + language, ":/i18n/"))
        	{
        		QCoreApplication::instance()->installTranslator(&appTranslator);
        	}
        

        There's no data stored for "Language" in the settings (empty registry string), and under the debugger, the language value returned by QLocale::system().name() was "en_GB" which I though would result in the .en translation being loaded

        David

        Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #2

        @Perdrix said in Problem with plurals:

        was "en_GB" which I though

        I also have a DSS.en.ts

        appTranslator.load("DSS." + language

        Why/how should it load DSS.en.ts when language contains 'en_GB' ?

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

        PerdrixP 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @Perdrix said in Problem with plurals:

          was "en_GB" which I though

          I also have a DSS.en.ts

          appTranslator.load("DSS." + language

          Why/how should it load DSS.en.ts when language contains 'en_GB' ?

          PerdrixP Offline
          PerdrixP Offline
          Perdrix
          wrote on last edited by
          #3

          @Christian-Ehrlicher

          Because that's what the documentation says:

          bool QTranslator::load(const QString &filename, const QString &directory = QString(), const QString &search_delimiters = QString(), const QString &suffix = QString())
          Loads filename + suffix (".qm" if the suffix is not specified), which may be an absolute file name or relative to directory. Returns true if the translation is successfully loaded; otherwise returns false.
          If directory is not specified, the current directory is used (i.e., as currentPath()).
          The previous contents of this translator object are discarded.
          If the file name does not exist, other file names are tried in the following order:

          1. File name without suffix appended.
          2. File name with text after a character in search_delimiters stripped ("_." is the default for search_delimiters if it is an empty string) and suffix.
          3. File name stripped without suffix appended.
          4. File name stripped further, etc.

          For example, an application running in the fr_CA locale (French-speaking Canada) might call load("foo.fr_ca", "/opt/foolib"). load() would then try to open the first existing readable file from this list:

          /opt/foolib/foo.fr_ca.qm
          /opt/foolib/foo.fr_ca
          /opt/foolib/foo.fr.qm
          /opt/foolib/foo.fr
          /opt/foolib/foo.qm
          /opt/foolib/foo

          David

          Christian EhrlicherC 1 Reply Last reply
          0
          • PerdrixP Perdrix

            @Christian-Ehrlicher

            Because that's what the documentation says:

            bool QTranslator::load(const QString &filename, const QString &directory = QString(), const QString &search_delimiters = QString(), const QString &suffix = QString())
            Loads filename + suffix (".qm" if the suffix is not specified), which may be an absolute file name or relative to directory. Returns true if the translation is successfully loaded; otherwise returns false.
            If directory is not specified, the current directory is used (i.e., as currentPath()).
            The previous contents of this translator object are discarded.
            If the file name does not exist, other file names are tried in the following order:

            1. File name without suffix appended.
            2. File name with text after a character in search_delimiters stripped ("_." is the default for search_delimiters if it is an empty string) and suffix.
            3. File name stripped without suffix appended.
            4. File name stripped further, etc.

            For example, an application running in the fr_CA locale (French-speaking Canada) might call load("foo.fr_ca", "/opt/foolib"). load() would then try to open the first existing readable file from this list:

            /opt/foolib/foo.fr_ca.qm
            /opt/foolib/foo.fr_ca
            /opt/foolib/foo.fr.qm
            /opt/foolib/foo.fr
            /opt/foolib/foo.qm
            /opt/foolib/foo

            David

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Perdrix said in Problem with plurals (tr()):

            File name with text after a character in search_delimiters stripped ("_." is the default for search_delimiters if it is an empty string) and suffix.

            baaah, I hate this stupid automatic search for every possibility.

            You're correct.

            Do you load it in your main.cpp? Otherwise appTranslator will go out of scope.

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

            PerdrixP 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @Perdrix said in Problem with plurals (tr()):

              File name with text after a character in search_delimiters stripped ("_." is the default for search_delimiters if it is an empty string) and suffix.

              baaah, I hate this stupid automatic search for every possibility.

              You're correct.

              Do you load it in your main.cpp? Otherwise appTranslator will go out of scope.

              PerdrixP Offline
              PerdrixP Offline
              Perdrix
              wrote on last edited by
              #5

              @Christian-Ehrlicher I call SetUILanguage(); from main()

              the code I pasted above is in SetUILanguage.

              I though QCoreApplication::instance()->installTranslator(&appTranslator); would copy the appTranslator into its internals?

              I will move that code to main() and let you know how it goes.

              D.

              C 1 Reply Last reply
              0
              • PerdrixP Perdrix

                @Christian-Ehrlicher I call SetUILanguage(); from main()

                the code I pasted above is in SetUILanguage.

                I though QCoreApplication::instance()->installTranslator(&appTranslator); would copy the appTranslator into its internals?

                I will move that code to main() and let you know how it goes.

                D.

                C Offline
                C Offline
                ChrisW67
                wrote on last edited by
                #6

                @Perdrix said in Problem with plurals (tr()):

                I though QCoreApplication::instance()->installTranslator(&appTranslator); would copy the appTranslator into its internals?

                No, it just stores and uses the pointer you passed to access the QTranslator object. If the translator goes out of scope then it will be destroyed and you may get crashing or, as seems to be the case, reversion to no-translation behaviour. When your SetUILanguage() exits, the QTranslator you allocated on the stack will go out of scope.

                1 Reply Last reply
                1
                • PerdrixP Offline
                  PerdrixP Offline
                  Perdrix
                  wrote on last edited by Perdrix
                  #7

                  OK thanks, it all works now that I have moved the QTranslator objects from the stack.

                  I think I was misled by the fact that it didn't crash and burn ...

                  D.

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • PerdrixP Perdrix

                    OK thanks, it all works now that I have moved the QTranslator objects from the stack.

                    I think I was misled by the fact that it didn't crash and burn ...

                    D.

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @Perdrix said in Problem with plurals (tr()):

                    I think I was misled by the fact that it didn't crash and burn ...

                    Why should it - it's a Qbject so QCoreApplication::installTranslator() simply connects to QObject::destroyed() and cleans up it's internal structures then.

                    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
                    1

                    • Login

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