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

KDECore

KLocalizedString Class Reference

#include <klocalizedstring.h>

List of all members.


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;
   ...
   ki18n("Welcome").toString(myLocale);

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 <caslav.ilic@gmx.net>

Definition at line 256 of file klocalizedstring.h.


Public Member Functions

KLocalizedString inContext (const QString &key, const QString &text) const
bool isEmpty () const
 KLocalizedString (const KLocalizedString &rhs)
 KLocalizedString ()
KLocalizedString & operator= (const KLocalizedString &rhs)
KLocalizedString subs (const QString &a, int fieldWidth=0, const QChar &fillChar=QLatin1Char(' ')) const
KLocalizedString subs (QChar a, int fieldWidth=0, 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 (qulonglong 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 (ulong 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 (uint a, int fieldWidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const
KLocalizedString subs (int a, int fieldWidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const
QString toString (const KLocale *locale) const
QString toString () const
 ~KLocalizedString ()

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)

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 154 of file klocalizedstring.cpp.

KLocalizedString::KLocalizedString ( const KLocalizedString &  rhs  ) 

Copy constructor.

Definition at line 174 of file klocalizedstring.cpp.

KLocalizedString::~KLocalizedString (  ) 

Destructor.

Definition at line 188 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 894 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 193 of file klocalizedstring.cpp.

void KLocalizedString::notifyCatalogsUpdated ( const QStringList &  languages,
const QList< KCatalogName > &  catalogs 
) [static]

Definition at line 951 of file klocalizedstring.cpp.

KLocalizedString & KLocalizedString::operator= ( const KLocalizedString &  rhs  ) 

Assignment operator.

Definition at line 179 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 881 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 872 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 862 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 847 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 832 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 817 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 802 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 787 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 772 of file klocalizedstring.cpp.

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

Finalizes the translation, creates QString with placeholders substituted.

Translations 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 203 of file klocalizedstring.cpp.

QString KLocalizedString::toString (  )  const

Finalizes the translation, creates QString with placeholders substituted.

Translations 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 198 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:
msg message text
Returns:
created KLocalizedString

Definition at line 902 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:
ctxt context text
msg message text
Returns:
created KLocalizedString

Definition at line 907 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:
ctxt context text
singular message text in singular
plural message text in plural
Returns:
created KLocalizedString

Definition at line 917 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:
singular message text in singular
plural message text in plural
Returns:
created KLocalizedString

Definition at line 912 of file klocalizedstring.cpp.


The documentation for this class was generated from the following files:
  • klocalizedstring.h
  • klocalizedstring.cpp

KDECore

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal