Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7572517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:56:44+00:00 2026-05-30T15:56:44+00:00

There are two forms of the QObject::tr() function I have been using for translating

  • 0

There are two forms of the QObject::tr() function I have been using for translating strings which contain numbers:

tr("There are %n foo(s)", 0, foo.size());
tr("%1: %2").arg(QLocale().toString(bar.size())).arg(bar.name());

These account for most instances where you want a number in a string except when you want to do localisation and pluralisation in the same string. Of course you could break the translation up, but you have to make a guess about whether that string will break in the same places in all languages (a decision I’m not qualified to make most of the time).

The QLocale documentation states that…

QString::arg() uses the default locale to format a number when its position specifier in the format string contains an ‘L’, e.g. “%L1”

… so the second form could be rewritten as …

tr("%L1: %2").arg(bar.size()).arg(bar.name());

… and this suggests you could rewrite the first form as…

tr("There are %Ln foo(s)", 0, foo.size());

This will compile and it shows up in Linguist as a pluralisation, but does anyone know if this will also localise the number? I can’t find anything which says either way in any documentation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:56:45+00:00Added an answer on May 30, 2026 at 3:56 pm

    Yes, it will work:

    #include <QtCore>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
    //    QLocale::setDefault(QLocale(QLocale::German));
        QLocale::setDefault(QLocale(QLocale::English));
    
        QTranslator qtTranslator;
    //    qtTranslator.load("foo_de", qApp->applicationDirPath());
        qtTranslator.load("foo_en", qApp->applicationDirPath());
        a.installTranslator(&qtTranslator);
    
        int foo_count = 123456;
    
        QString str1 = QObject::tr("There are %n foo(s)", "foo", foo_count);
        QString str2 = QObject::tr("There are %Ln foo(s)", "foo", foo_count);
    
        qDebug() << str1;
        qDebug() << str2;
    
        return a.exec();
    }
    

    Output if running English:

    "There are 123456 foos"
    "There are 123,456 foos"
    

    Output if running German (with apologies to any actual German speakers out there):

    "Es gibt 123456 fooen"
    "Es gibt 123.456 fooen"
    

    The only weird things are:

    • The translator must keep the %Ln in both the singular and plural translations, not just the usual %n.
    • Linguist will complain for the plural form that “Translation does not contain the necessary %n place marker.” This is a bug.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to pass values between two forms. Let's say, I have
Now, I have two forms, called form1 and form2, in the form1 there's a
I have two forms (Form1 and Form2). On Form1 there is a 'public int
I have two forms using a ListView component. In the first form it works
Using Spring 3 I have two forms: adding Item and invalidating Item I have
I have a problem concerning delegates in a Windows Forms application. There are two
I wonder if anyone can advise? I have a JSP which contains two forms,
Backdrop: There are two forms, the main application form and a form to edit
In System.Windows.Forms.Application there are two properties called LocalUserAppDataPath and UserAppDataPath . On this computer
I have two forms, form A and form B. These forms must differ in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.