KDECore
#include <klocalizedstring.h>
Public Member Functions | |
KLocalizedString () | |
KLocalizedString (const KLocalizedString &rhs) | |
~KLocalizedString () | |
KLocalizedString | inContext (const QString &key, const QString &text) const |
bool | isEmpty () const |
KLocalizedString & | operator= (const KLocalizedString &rhs) |
KLocalizedString | subs (int a, int fieldWidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const |
KLocalizedString | subs (uint a, int fieldWidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const |
KLocalizedString | subs (long a, int fieldWidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const |
KLocalizedString | subs (ulong a, int fieldWidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const |
KLocalizedString | subs (qlonglong a, int fieldWidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const |
KLocalizedString | subs (qulonglong a, int fieldWidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const |
KLocalizedString | subs (double a, int fieldWidth=0, char format= 'g', int precision=-1, const QChar &fillChar=QLatin1Char(' ')) const |
KLocalizedString | subs (QChar a, int fieldWidth=0, const QChar &fillChar=QLatin1Char(' ')) const |
KLocalizedString | subs (const QString &a, int fieldWidth=0, const QChar &fillChar=QLatin1Char(' ')) const |
QString | toString () const |
QString | toString (const QString &catalogName) const |
QString | toString (const KLocale *locale) const |
QString | toString (const KLocale *locale, const QString &catalogName) const |
Static Public Member Functions | |
static void | notifyCatalogsUpdated (const QStringList &languages, const QList< KCatalogName > &catalogs) |
Friends | |
KLocalizedString | ki18n (const char *msg) |
KLocalizedString | ki18nc (const char *ctxt, const char *msg) |
KLocalizedString | ki18ncp (const char *ctxt, const char *singular, const char *plural) |
KLocalizedString | ki18np (const char *singular, const char *plural) |
Detailed Description
Class for producing and handling localized messages.
KLocalizedString handles translation and specific needs of argument substitution and formatting in localized message strings.
Topics:
General Usage
This class should mostly not be used directly, but through wrapper i18n calls which return QString, for localization of user visible messages in applications.
For the most frequent message type, the one without any arguments, you would use simply:
If there are arguments to be substitued into the message, you just add them after the message string:
There can be up to some final number of arguments added like this (i18n is realized by overloaded templates). If you overrun this number, use ki18n* series of calls (described below). You can use several types as arguments, see subs methods.
Sometimes a short message can be ambiguous in English, then you would use the context version, i18nc. There the first string is context, and the second is the message which really gets displayed:
While English diferentiates plural forms only between 1 and else, in other languages it might not be that simple, or it might be simpler. To handle this properly, use plural call, i18np:
Note that the plural form shall be decided by first integer-valued argument, (numImages in the example above). In rare cases when there are two integer arguments, you should take care to order them properly.
Finally, message might need both context and plural, which is provided by i18ncp call:
Be carefull not to use literal string as first argument after message text in basic i18n() call. In debug mode, it will even trigger the static assert, giving error at compile time. This is in order to prevent misnamed calls: it may happen that you add context or plural to previously basic message, but forget to change the name of the call.
All message strings are expected to pass for well-formed XML, whether or not the output device supports some form of markup. Thus, predefined XML entities are always available: <, >, &, ', and ". E.g. if you need a non-tag less-than sign, use < entity instead. The exception to the well-formed XML requirement is the ampersand (&), which is used a lot for marking accelerators, so you should not write it as & (except in the very unlikely case when the construct with the naked ampersand can be interpreted as an entity in itself).
Specialized Usage
There are some situations where i18n* calls are not sufficient or convenient. For one, if you need to substitute many arguments. Or, if you find that you need to defer the substitution. For this you can use the ki18n call which returns a KLocalizedString, substitute arguments using its subs methods, and finalize the translation by calling its toString method. For example:
Another case is when you want extra formatting of arguments, like field width or number of decimals. subs methods can take these formatting parameters. In particular, you should never use some custom way to format arguments, as subs methods will also properly localize them:
There are also context, plural and context-plural variants:
If you need translation using locale (ie. KLocale object) other than the default, you can use overloaded toString method which takes pointer to a locale:
Normally all loaded catalogs are searched for translation, and the first found translation is returned. Sometimes this may lead to clashes, especially when dealing with specialized collection catalogs (country names, language names, etc.) in which messages are not equipped with contexts. In such situations, toString method can take the name of the specific catalog in which to look for translation:
Translators have a capability to script translations at runtime, which is for the most part transparent to the programmer. However, sometimes the programmer may help by providing some dynamic context to the message, using the inContext method of KLocalizedString. Unlike the ordinary context, this one changes at runtime; translators have the means to fetch it and use it to script the translation properly. An example:
Several dynamic contexts, with different keys, can be added like this.
Placeholder Substitution
Hopefully, for the most part placeholders are being substituted the way you would intuitively expect them to be. Nevertheless:
- Placeholders are substituted in one pass, so no need to worry about argument itself containing a placeholder.
- All same-numbered placeholders are substituted with same argument.
- Placeholders directly index arguments: they should be numbered from 1 upwards, without gaps in the sequence so that each argument is indexed. Otherwise you will get error marks in messages at runtime (when compiled in debug mode), and any invalid placeholder will be left unsubstituted. The exception is plural-deciding argument in plural call, where it is allowed to drop its placeholder in either singular or plural form.
- If none of the arguments supplied to a plural call is integer-valued, you will get an error mark in message at runtime (in debug mode).
- Plain number arguments will be normally formatted as if they denote amounts, according to language rules (thousands separation, etc.) But sometimes a number is a numerical identifier (e.g. port number), and to be treated as such, wrap the placeholder with the numid tag:
Further References
KDE Techbase contains a series of tutorials on preparing the code for localization (and on internationalization process in general), where the intended patterns of usage of i18n API are covered in great detail.
All i18n'd messages, whether sent to widgets expecting plain text or allowing Qt rich text (HTML), support the new KDE semantic markup for user interface text, KUIT in short. Semantic markup both increases the consistency of visual presentation for the end user, and provides extra information to translators, so that translations can be of higher quality. KUIT is documented in an Techbase article as well.
- See also
- KLocale
Definition at line 299 of file klocalizedstring.h.
Constructor & Destructor Documentation
|
explicit |
Constructs an empty message, which is not valid for finalization.
Useful when you later need to assign KLocalizedString obtained by one of ki18n* calls.
- See also
- isEmpty()
Definition at line 148 of file klocalizedstring.cpp.
KLocalizedString::KLocalizedString | ( | const KLocalizedString & | rhs | ) |
Copy constructor.
Definition at line 168 of file klocalizedstring.cpp.
KLocalizedString::~KLocalizedString | ( | ) |
Destructor.
Definition at line 182 of file klocalizedstring.cpp.
Member Function Documentation
KLocalizedString KLocalizedString::inContext | ( | const QString & | key, |
const QString & | text | ||
) | const |
Adds dynamic context to the message.
- Parameters
-
key context key text context value
- Returns
- resultant KLocalizedString
Definition at line 916 of file klocalizedstring.cpp.
bool KLocalizedString::isEmpty | ( | ) | const |
Checks whether the message is empty.
This will happen if you just constructed the object via default constructor.
Empty messages are not valid for finalization; if you use toString() on them, you will get error mark instead of empty QString (in debug mode).
- Returns
true
if the message is empty, elsefalse
Definition at line 187 of file klocalizedstring.cpp.
|
static |
Called from KLocale on addition or removal of catalogs.
Definition at line 974 of file klocalizedstring.cpp.
KLocalizedString & KLocalizedString::operator= | ( | const KLocalizedString & | rhs | ) |
Assignment operator.
Definition at line 173 of file klocalizedstring.cpp.
KLocalizedString KLocalizedString::subs | ( | int | a, |
int | fieldWidth = 0 , |
||
int | base = 10 , |
||
const QChar & | fillChar = QLatin1Char(' ') |
||
) | const |
Substitutes an int argument into the message.
- Parameters
-
a the argument fieldWidth width of the formatted field, padded by spaces. Positive value aligns right, negative aligns left base the radix used to represent the number as a string. Valid values range from 2 to 36 fillChar the character used to fill up the empty places when field width is greater than argument width
- Returns
- resultant KLocalizedString
Definition at line 792 of file klocalizedstring.cpp.
KLocalizedString KLocalizedString::subs | ( | uint | a, |
int | fieldWidth = 0 , |
||
int | base = 10 , |
||
const QChar & | fillChar = QLatin1Char(' ') |
||
) | const |
Substitutes an unsigned int argument into the message.
- Parameters
-
a the argument fieldWidth width of the formatted field, padded by spaces. Positive value aligns right, negative aligns left base the radix used to represent the number as a string. Valid values range from 2 to 36 fillChar the character used to fill up the empty places when field width is greater than argument width
- Returns
- resultant KLocalizedString
Definition at line 807 of file klocalizedstring.cpp.
KLocalizedString KLocalizedString::subs | ( | long | a, |
int | fieldWidth = 0 , |
||
int | base = 10 , |
||
const QChar & | fillChar = QLatin1Char(' ') |
||
) | const |
Substitutes a long argument into the message.
- Parameters
-
a the argument fieldWidth width of the formatted field, padded by spaces. Positive value aligns right, negative aligns left base the radix used to represent the number as a string. Valid values range from 2 to 36 fillChar the character used to fill up the empty places when field width is greater than argument width
- Returns
- resultant KLocalizedString
Definition at line 822 of file klocalizedstring.cpp.
KLocalizedString KLocalizedString::subs | ( | ulong | a, |
int | fieldWidth = 0 , |
||
int | base = 10 , |
||
const QChar & | fillChar = QLatin1Char(' ') |
||
) | const |
Substitutes an unsigned long argument into the message.
- Parameters
-
a the argument fieldWidth width of the formatted field, padded by spaces. Positive value aligns right, negative aligns left base the radix used to represent the number as a string. Valid values range from 2 to 36 fillChar the character used to fill up the empty places when field width is greater than argument width
- Returns
- resultant KLocalizedString
Definition at line 837 of file klocalizedstring.cpp.
KLocalizedString KLocalizedString::subs | ( | qlonglong | a, |
int | fieldWidth = 0 , |
||
int | base = 10 , |
||
const QChar & | fillChar = QLatin1Char(' ') |
||
) | const |
Substitutes a long long argument into the message.
- Parameters
-
a the argument fieldWidth width of the formatted field, padded by spaces. Positive value aligns right, negative aligns left base the radix used to represent the number as a string. Valid values range from 2 to 36 fillChar the character used to fill up the empty places when field width is greater than argument width
- Returns
- resultant KLocalizedString
Definition at line 852 of file klocalizedstring.cpp.
KLocalizedString KLocalizedString::subs | ( | qulonglong | a, |
int | fieldWidth = 0 , |
||
int | base = 10 , |
||
const QChar & | fillChar = QLatin1Char(' ') |
||
) | const |
Substitutes an unsigned long long argument into the message.
- Parameters
-
a the argument fieldWidth width of the formatted field, padded by spaces. Positive value aligns right, negative aligns left base the radix used to represent the number as a string. Valid values range from 2 to 36 fillChar the character used to fill up the empty places when field width is greater than argument width
- Returns
- resultant KLocalizedString
Definition at line 867 of file klocalizedstring.cpp.
KLocalizedString KLocalizedString::subs | ( | double | a, |
int | fieldWidth = 0 , |
||
char | format = 'g' , |
||
int | precision = -1 , |
||
const QChar & | fillChar = QLatin1Char(' ') |
||
) | const |
Substitutes a double argument into the message.
- Parameters
-
a the argument fieldWidth width of the formatted field, padded by spaces. Positive value aligns right, negative aligns left format type of floating point formating, like in QString::arg precision number of digits after the decimal separator fillChar the character used to fill up the empty places when field width is greater than argument width
- Returns
- resultant KLocalizedString
Definition at line 882 of file klocalizedstring.cpp.
KLocalizedString KLocalizedString::subs | ( | QChar | a, |
int | fieldWidth = 0 , |
||
const QChar & | fillChar = QLatin1Char(' ') |
||
) | const |
Substitutes a QChar argument into the message.
- Parameters
-
a the argument fieldWidth width of the formatted field, padded by spaces. Positive value aligns right, negative aligns left fillChar the character used to fill up the empty places when field width is greater than argument width
- Returns
- resultant KLocalizedString
Definition at line 894 of file klocalizedstring.cpp.
KLocalizedString KLocalizedString::subs | ( | const QString & | a, |
int | fieldWidth = 0 , |
||
const QChar & | fillChar = QLatin1Char(' ') |
||
) | const |
Substitutes a QString argument into the message.
- Parameters
-
a the argument fieldWidth width of the formatted field, padded by spaces. Positive value aligns right, negative aligns left fillChar the character used to fill up the empty places when field width is greater than argument width
- Returns
- resultant KLocalizedString
Definition at line 903 of file klocalizedstring.cpp.
QString KLocalizedString::toString | ( | ) | const |
Finalizes the translation, creates QString with placeholders substituted.
Translation is obtained from default locale.
If there was any mismatch between placeholders and arguments returned string will contain error marks (in debug mode).
- Returns
- finalized translation
Definition at line 192 of file klocalizedstring.cpp.
- Since
- 4.5
Like toString, but looks for translation only in specific catalog.
- Parameters
-
catalogName the name of the catalog to check for translation
- Returns
- finalized translation
Definition at line 197 of file klocalizedstring.cpp.
Finalizes the translation, creates QString with placeholders substituted.
Translation is obtained from given locale. If locale is NULL, original message is used instead of translated.
If there was any mismatch between placeholders and arguments returned string will contain error marks (in debug mode).
- Parameters
-
locale locale from which translations are to be taken
- Returns
- finalized translation
Definition at line 202 of file klocalizedstring.cpp.
- Since
- 4.5
Like toString, but looks for translation only in specific catalog.
- Parameters
-
locale locale from which translations are to be taken catalogName the name of the catalog to check for translation
- Returns
- finalized translation
Definition at line 207 of file klocalizedstring.cpp.
Friends And Related Function Documentation
|
friend |
Creates localized string from a given message.
Normaly you should use i18n() templates instead, as you need real KLocalizedString object only in special cases. All text arguments must be UTF-8 encoded and must not be empty or NULL.
- Parameters
-
msg message text
- Returns
- created KLocalizedString
Definition at line 924 of file klocalizedstring.cpp.
|
friend |
Creates localized string from a given message, with added context.
Context is only for disambiguation purposes (both for lookup and for translators), it is not part of the message. Normaly you should use i18nc() templates instead, as you need real KLocalizedString object only in special cases. All text arguments must be UTF-8 encoded and must not be empty or NULL.
- Parameters
-
ctxt context text msg message text
- Returns
- created KLocalizedString
Definition at line 929 of file klocalizedstring.cpp.
|
friend |
Creates localized string from a given plural and singular form, with added context.
Context is only for disambiguation purposes (both for lookup and for translators), it is not part of the message. Normaly you should use i18ncp() templates instead, as you need real KLocalizedString object only in special cases. All text arguments must be UTF-8 encoded and must not be empty or NULL.
- Parameters
-
ctxt context text singular message text in singular plural message text in plural
- Returns
- created KLocalizedString
Definition at line 939 of file klocalizedstring.cpp.
|
friend |
Creates localized string from a given plural and singular form.
Normaly you should use i18np() templates instead, as you need real KLocalizedString object only in special cases. All text arguments must be UTF-8 encoded and must not be empty or NULL.
- Parameters
-
singular message text in singular plural message text in plural
- Returns
- created KLocalizedString
Definition at line 934 of file klocalizedstring.cpp.
The documentation for this class was generated from the following files:
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:13 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.