Problem with plurals (tr())
-
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:

but when I run the application I get:

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
-
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:- File name without suffix appended.
- 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.
- File name stripped without suffix appended.
- 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/fooDavid
@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.
-
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:

but when I run the application I get:

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
@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' ?
-
@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' ?
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:- File name without suffix appended.
- 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.
- File name stripped without suffix appended.
- 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/fooDavid
-
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:- File name without suffix appended.
- 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.
- File name stripped without suffix appended.
- 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/fooDavid
@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.
-
@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.
@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.
-
@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.
@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.
-
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.
@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.