m (Change of context property name in Designer in Qt 4.5.) |
|||
| Line 57: | Line 57: | ||
</code> | </code> | ||
| − | The ambiguity warning can also be issued for <tt>.ui</tt>, <tt>.rc</tt> and <tt>.kcfg</tt> files. In <tt>.ui</tt> files, text labels can have the <tt>comment</tt> attribute (accessible in Qt Designer as a property to the label), which can be used for adding contexts same as the first argument of the <tt>i18nc()</tt> call. In <tt>.rc</tt> and <tt>.kcfg</tt> files, contexts are added via the <tt>context</tt> attribute. | + | The ambiguity warning can also be issued for <tt>.ui</tt>, <tt>.rc</tt> and <tt>.kcfg</tt> files. In <tt>.ui</tt> files, text labels can have the <tt>comment</tt> attribute (accessible in Qt Designer as a "disambiguation" property to the label, or "comment" prior to Qt 4.5), which can be used for adding contexts same as the first argument of the <tt>i18nc()</tt> call. In <tt>.rc</tt> and <tt>.kcfg</tt> files, contexts are added via the <tt>context</tt> attribute. |
== Number Formatting == | == Number Formatting == | ||
Contents |
There are small technical details of i18n which are not that easy to keep in mind at all times, as well as number of i18n recommendations to uphold during the development. To help you with this, the Krazy code checker also looks for some frequently encountered i18n issues. This article documents these issues as reported by Krazy, for cases when you are not sure what the remedy should be.
The i18n API is very strict about congruence between the %number placeholders in the message, and the arguments actually supplied to substitute them. Effectively, the placeholders directly index arguments, albeit one- rather than zero-based.
i18n("Found key: %1", key); // correct
i18n("Found key: %1").arg(key); // ***wrong
i18n("Line: %1 Column: %2", lineNo, colNo); // correct
i18n("Line: %1 Column: %3", lineNo, colNo); // ***wrong
i18np("Found a file in folder %2",
"Found %1 files in folder %2", nfiles, folder); // correct
i18np("Found a file in folder %2",
"Found some files in folder %2", nfiles, folder); // also correct
i18np("Found a file in folder %1",
"Found some files in folder %1", folder, nfiles); // ***wrong
English is a rather noninflected language compared to many others; single English word can frequently be noun, verb, or adjective, while retaining its original form. This presents frequent problems for the translator while translating into inflected languages when the original message is short, especially single-worded. The solution is to add context to the message via i18nc() call.
titleFinal = title.isEmpty() ?
i18n("Unknown") : title; // ambiguous
titleFinal = title.isEmpty() ?
i18nc("An unknown title", "Unknown") : title; // clarified
While you are at adding contexts, consider providing the appropriate KUIT context marker as well, which will further zero-in translators' job:
titleFinal = title.isEmpty() ?
i18nc("@item:intable An unknown title", "Unknown") : title;
// way to go!
The ambiguity warning can also be issued for .ui, .rc and .kcfg files. In .ui files, text labels can have the comment attribute (accessible in Qt Designer as a "disambiguation" property to the label, or "comment" prior to Qt 4.5), which can be used for adding contexts same as the first argument of the i18nc() call. In .rc and .kcfg files, contexts are added via the context attribute.
The number-valued (either integer or real) arguments to i18n messages are formatted automatically into given language, without programmer's intervention. Using other methods to format numbers into strings may circumvent proper formatting for the language.
i18n("Number of pages: %1", numPages); // good, localized amount format
i18n("Connected to port %1.", port); // bad, amount format not desired
i18n("Connected to port %1.", QString::number(port)); // bad, not localized
i18n("Connected to port <numid>%1</numid>.", port); // good
Even when the complete message is a single number, it should be i18n'd, with a proper context:
result = QString::number(z); // bad
result = i18nc("Atomic number", "%1", z); // good
When the number is to be formatted in a special way (field width, number of decimals, etc.) into the message, still neither QString::number() nor KLocale::formatNumber() should be used, but ki18n*() series of calls with subs() methods (see KLocalizedString documentation):
i18n("Percent complete: %1", QString::number(percent, 'f', 1)); // bad
ki18n("Percent complete: %1").subs(percent, 0, 'f', 1).toString(); // good
Every i18n message in KDE4 is effectively XML markup. HTML tags come from Qt's rich text, and can be used only in rich-text capable widgets; KUIT tags are new KDE4 semantic markup, which should be preferred to HTML, and can be used in any i18n message (plain or rich-text output is decided on the basis of context marker).
i18n("headers go into <includes>"); // ***error in XML markup
i18n("headers go into <includes>"); // no markup problem, but...
i18n("headers go into <placeholder>includes</placeholder>"); // better
i18n("@info:whatsthis",
"...this cannot be undone."); // Krazy complains
i18n("@info:whatsthis",
"...this <emphasis>cannot</emphasis> be undone."); // fine
i18n("@info",
"Really delete %1?", filename); // complains
i18n("@info",
"Really delete <filename>%1</filename>?", filename); // fine
Semantic context markers give great deal of information to translators of where and how the message is used at runtime. Providing them for any future messages is strongly encouraged, and even equipping existing messages when not in message freeze is welcomed. Krazy helps with the following checks.
When Krazy complains about ambiguity in i18n translation in an UI file. You can either add a comment="" attribute using QtDesigner or using a text editor.
For any questions or suggestions, Krazy i18n checks are presently maintained by Chusslove Illich <caslav.ilic@gmx.net>.