• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDECore

Public Member Functions | Static Public Member Functions | Friends | List of all members
KLocalizedString Class Reference

#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
  • Specialized Usage
  • Placeholder Substitution
  • Further References

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:

QString msg = i18n("Just plain info");

If there are arguments to be substitued into the message, you just add them after the message string:

QString msg = i18n("%1 has scored %2", playerName, score);

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:

QString msg = i18nc("Player name - score", "%1 - %2", playerName, score);

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:

QString msg = i18np("One image in album %2", "%1 images in album %2",
numImages, albumName);

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:

QString msg = i18ncp("Personal file", "One file", "%1 files", numFiles);

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: &lt;, &gt;, &amp;, &apos;, and &quot;. E.g. if you need a non-tag less-than sign, use &lt; 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 &amp; (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:

KLocalizedString ks;
case (reportSource) {
SRC_ENG: ks = ki18n("Engineering reports: %1"); break;
SRC_HEL: ks = ki18n("Helm reports: %1"); break;
SRC_SON: ks = ki18n("Sonar reports: %1"); break;
default: ks = ki18n("General report: %1");
}
QString msg = ks.subs(reportText).toString();

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:

QString s = i18n("Rounds: %1", myNumberFormat(n, 8)); // bad, number not localized
QString s = ki18n("Rounds: %1").subs(n, 8).toString(); // good, number localized

There are also context, plural and context-plural variants:

QString s = ki18nc("No function", "None").toString();
QString s = ki18np("File found", "%1 files found").subs(n).toString();
QString s = ki18ncp("Personal file", "One file", "%1 files").subs(n).toString();

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:

KLocale *myLocale;
...
QString msg = ki18n("Welcome").toString(myLocale);

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:

QString trName = ki18n("Georgia").toString("countries");

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:

KLocalizedString ks = ki18nc("%1 is user name; may have "
"dynamic context gender=[male,female]",
"%1 went offline");
if (knownUsers.contains(user) && !knownUsers[user].gender.isEmpty()) {
ks = ks.inContext("gender", knownUsers[user].gender);
}
QString msg = ks.subs(user).toString();

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:
    QString msg = i18n("Using port <numid>%1</numid>", port);

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
Author
Chusslove Illich <casla.nosp@m.v.il.nosp@m.ic@gm.nosp@m.x.ne.nosp@m.t>

Definition at line 299 of file klocalizedstring.h.

Constructor & Destructor Documentation

KLocalizedString::KLocalizedString ( )
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
keycontext key
textcontext 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, else false

Definition at line 187 of file klocalizedstring.cpp.

void KLocalizedString::notifyCatalogsUpdated ( const QStringList &  languages,
const QList< KCatalogName > &  catalogs 
)
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
athe argument
fieldWidthwidth of the formatted field, padded by spaces. Positive value aligns right, negative aligns left
basethe radix used to represent the number as a string. Valid values range from 2 to 36
fillCharthe 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
athe argument
fieldWidthwidth of the formatted field, padded by spaces. Positive value aligns right, negative aligns left
basethe radix used to represent the number as a string. Valid values range from 2 to 36
fillCharthe 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
athe argument
fieldWidthwidth of the formatted field, padded by spaces. Positive value aligns right, negative aligns left
basethe radix used to represent the number as a string. Valid values range from 2 to 36
fillCharthe 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
athe argument
fieldWidthwidth of the formatted field, padded by spaces. Positive value aligns right, negative aligns left
basethe radix used to represent the number as a string. Valid values range from 2 to 36
fillCharthe 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
athe argument
fieldWidthwidth of the formatted field, padded by spaces. Positive value aligns right, negative aligns left
basethe radix used to represent the number as a string. Valid values range from 2 to 36
fillCharthe 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
athe argument
fieldWidthwidth of the formatted field, padded by spaces. Positive value aligns right, negative aligns left
basethe radix used to represent the number as a string. Valid values range from 2 to 36
fillCharthe 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
athe argument
fieldWidthwidth of the formatted field, padded by spaces. Positive value aligns right, negative aligns left
formattype of floating point formating, like in QString::arg
precisionnumber of digits after the decimal separator
fillCharthe 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
athe argument
fieldWidthwidth of the formatted field, padded by spaces. Positive value aligns right, negative aligns left
fillCharthe 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
athe argument
fieldWidthwidth of the formatted field, padded by spaces. Positive value aligns right, negative aligns left
fillCharthe 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.

QString KLocalizedString::toString ( const QString &  catalogName) const
Since
4.5

Like toString, but looks for translation only in specific catalog.

Parameters
catalogNamethe name of the catalog to check for translation
Returns
finalized translation

Definition at line 197 of file klocalizedstring.cpp.

QString KLocalizedString::toString ( const KLocale *  locale) const

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
localelocale from which translations are to be taken
Returns
finalized translation

Definition at line 202 of file klocalizedstring.cpp.

QString KLocalizedString::toString ( const KLocale *  locale,
const QString &  catalogName 
) const
Since
4.5

Like toString, but looks for translation only in specific catalog.

Parameters
localelocale from which translations are to be taken
catalogNamethe name of the catalog to check for translation
Returns
finalized translation

Definition at line 207 of file klocalizedstring.cpp.

Friends And Related Function Documentation

KLocalizedString ki18n ( const char *  msg)
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
msgmessage text
Returns
created KLocalizedString

Definition at line 924 of file klocalizedstring.cpp.

KLocalizedString ki18nc ( const char *  ctxt,
const char *  msg 
)
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
ctxtcontext text
msgmessage text
Returns
created KLocalizedString

Definition at line 929 of file klocalizedstring.cpp.

KLocalizedString ki18ncp ( const char *  ctxt,
const char *  singular,
const char *  plural 
)
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
ctxtcontext text
singularmessage text in singular
pluralmessage text in plural
Returns
created KLocalizedString

Definition at line 939 of file klocalizedstring.cpp.

KLocalizedString ki18np ( const char *  singular,
const char *  plural 
)
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
singularmessage text in singular
pluralmessage 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:
  • klocalizedstring.h
  • klocalizedstring.cpp
This file is part of the KDE documentation.
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.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal