kdelibs/kdecore/localization/klocale.h

609 lines
22 KiB
C
Raw Normal View History

/* This file is part of the KDE libraries
Copyright (C) 2023 Ivailo Monev <xakepa10@gmail.com>
2014-11-13 01:04:59 +02:00
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2, as published by the Free Software Foundation.
2014-11-13 01:04:59 +02:00
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
2014-11-13 01:04:59 +02:00
#ifndef KLOCALE_H
#define KLOCALE_H
#include <klocalizedstring.h>
#include <ksharedconfig.h>
#include <QStringList>
#include <QLocale>
#include <QDateTime>
2014-11-13 01:04:59 +02:00
class KDateTime;
class KLocalePrivate;
/**
*
* KLocale provides support for country specific stuff like
* the national language.
*
* KLocale supports translating, as well as specifying the format
* for numbers, time, and date.
2014-11-13 01:04:59 +02:00
*
* Use KGlobal::locale() to get pointer to the global KLocale object,
* containing the applications current locale settings.
*
* For example, to format the date May 17, 1995 in the current locale, use:
*
* \code
* QString date = KGlobal::locale()->formatDate(QDate(1995,5,17));
* \endcode
*
* @author Stephan Kulow <coolo@kde.org>, Preston Brown <pbrown@kde.org>,
* Hans Petter Bieker <bieker@kde.org>, Lukas Tinkl <lukas.tinkl@suse.cz>
* @short class for supporting locale settings and national language
*/
class KDECORE_EXPORT KLocale
{
public:
/**
* Constructs a KLocale with the given catalog name
*
* The constructor looks for an entry Language in the group Locale in the configuration file.
* If not specified in the configuration file or the one specified in the configuration fiel
* is not valid the environment variables LC_ALL, LC_CTYPE and LANG will be checked (in that
* order). If KLocale is not able to use any of the specified languages, the default language
* (en_US) will be used.
2014-11-13 01:04:59 +02:00
*
* If you specify a configuration file, it has to be valid until the KLocale object is
* destroyed because it may be synced and reparsed.
2014-11-13 01:04:59 +02:00
*
* @param catalog the name of the main language file
* @param config a configuration file with a Locale group detailing locale-related preferences
* (such as language and formatting options).
2014-11-13 01:04:59 +02:00
*/
explicit KLocale(const QString &catalog, KSharedConfig::Ptr config = KSharedConfig::Ptr());
2014-11-13 01:04:59 +02:00
/**
* Constructs a KLocale with the given catalog name
*
* Allows you to override the language for this locale.
2014-11-13 01:04:59 +02:00
*
* @param catalog the name of the main language file
* @param language the complete name for the locale, e.g. "en" for English
* @param config a configuration file with a Locale group detailing locale-related
* preferences (such as language and formatting options).
2014-11-13 01:04:59 +02:00
*/
KLocale(const QString &catalog, const QString &language, KConfig *config = nullptr);
2014-11-13 01:04:59 +02:00
/**
* Copy constructor
*/
KLocale(const KLocale &rhs);
2014-11-13 01:04:59 +02:00
/**
* Assignment operator
*/
KLocale& operator=(const KLocale &rhs);
2014-11-13 01:04:59 +02:00
/**
* Destructor
*/
~KLocale();
2014-11-13 01:04:59 +02:00
/**
* Returns the language code used by this object. Note that this may be different from
* the language used for translations.
2014-11-13 01:04:59 +02:00
*
* Use languageCodeToName(language) to get human readable, localized language name.
2014-11-13 01:04:59 +02:00
*
* @return the currently used language code
2014-11-13 01:04:59 +02:00
*
* @see languageCodeToName
2014-11-13 01:04:59 +02:00
*/
QString language() const;
2014-11-13 01:04:59 +02:00
/**
* Returns the language codes selected by user, ordered by decreasing priority. Includes
* additional languages such as the locale name (e.g. "en_US") and the locale language
* (e.g. "en") for broader matches.
2014-11-13 01:04:59 +02:00
*
* Use languageCodeToName(language) to get human readable, localized language name.
2014-11-13 01:04:59 +02:00
*
* @return list of language codes
2014-11-13 01:04:59 +02:00
*
* @see languageCodeToName
2014-11-13 01:04:59 +02:00
*/
QStringList languageList() const;
2014-11-13 01:04:59 +02:00
/**
* These binary units are used in KDE by the formatByteSize() functions.
2014-11-13 01:04:59 +02:00
*
* There are several different units standards:
* 1) SI (i.e. metric), powers-of-10.
* 2) IEC, powers-of-2, with specific units KiB, MiB, etc.
* 3) JEDEC, powers-of-2, used for solid state memory sizing which is why you see flash cards
* labels as e.g. 4GB. These (ab)use the metric units. Although JEDEC only defines KB,
* MB, GB, if JEDEC is selected all units will be powers-of-2 with metric prefixes for
* clarity in the event of sizes larger than 1024 GB.
2014-11-13 01:04:59 +02:00
*
* Although 3 different dialects are possible this enum only uses metric names since adding all
* 3 different names of essentially the same unit would be pointless. Use BinaryUnitDialect to
* control the exact units returned.
2014-11-13 01:04:59 +02:00
*
* @since 4.4
* @see binaryUnitDialect
2014-11-13 01:04:59 +02:00
*/
enum BinarySizeUnits {
/// Auto-choose a unit such that the result is in the range [0, 1000 or 1024)
DefaultBinaryUnits = -1,
2014-11-13 01:04:59 +02:00
// The first real unit must be 0 for the current implementation!
UnitByte = 0, ///< B 1 byte
UnitKiloByte = 1, ///< KiB/KB/kB 1024/1000 bytes.
UnitMegaByte = 2, ///< MiB/MB/MB 2^20/10^06 bytes.
UnitGigaByte = 3, ///< GiB/GB/GB 2^30/10^09 bytes.
UnitTeraByte = 4, ///< TiB/TB/TB 2^40/10^12 bytes.
UnitPetaByte = 5, ///< PiB/PB/PB 2^50/10^15 bytes.
UnitExaByte = 6, ///< EiB/EB/EB 2^60/10^18 bytes.
UnitZettaByte = 7, ///< ZiB/ZB/ZB 2^70/10^21 bytes.
UnitYottaByte = 8, ///< YiB/YB/YB 2^80/10^24 bytes.
LastBinaryUnit = UnitYottaByte
2014-11-13 01:04:59 +02:00
};
/**
* This enum chooses what dialect is used for binary units.
2014-11-13 01:04:59 +02:00
*
* On the other hand network transmission rates are typically in metric so Default, Metric, or
* IEC (which is unambiguous) should be chosen.
2014-11-13 01:04:59 +02:00
*
* Although JEDEC abuses the metric prefixes and can therefore be confusing, it has been
* used to describe *memory* sizes for quite some time and programs should therefore use either
* Default, JEDEC, or IEC 60027-2 for memory sizes.
2014-11-13 01:04:59 +02:00
*
* @since 4.4
* @see binaryUnitDialect
2014-11-13 01:04:59 +02:00
*/
enum BinaryUnitDialect {
IECBinaryDialect = 0, ///< KDE Default, KiB, MiB, etc. 2^(10*n)
JEDECBinaryDialect = 1, ///< KDE 3.5 default, KB, MB, etc. 2^(10*n)
MetricBinaryDialect = 2, ///< SI Units, kB, MB, etc. 10^(3*n)
LastBinaryDialect = MetricBinaryDialect
};
2014-11-13 01:04:59 +02:00
/**
* Returns the user's configured binary unit dialect. e.g. if MetricBinaryDialect is returned
* then the values configured for how much a set of bytes are worth would be 10^(3*n) and KB
* (1000 bytes == 1 KB), in this case.
2014-11-13 01:04:59 +02:00
*
* @since 4.4
* @return User's configured binary unit dialect
* @see BinaryUnitDialect
2014-11-13 01:04:59 +02:00
*/
BinaryUnitDialect binaryUnitDialect() const;
2014-11-13 01:04:59 +02:00
/**
* @since 4.23
2014-11-13 01:04:59 +02:00
*
* Returns the currently selected date format.
2014-11-13 01:04:59 +02:00
*
* @return Current date format.
2014-11-13 01:04:59 +02:00
*/
QString dateFormat(QLocale::FormatType format) const;
2014-11-13 01:04:59 +02:00
/**
* @since 4.23
2014-11-13 01:04:59 +02:00
*
* Returns the currently selected time format.
2014-11-13 01:04:59 +02:00
*
* @return Current time format.
2014-11-13 01:04:59 +02:00
*/
QString timeFormat(QLocale::FormatType format) const;
2014-11-13 01:04:59 +02:00
/**
* @since 4.23
2014-11-13 01:04:59 +02:00
*
* Returns the currently selected date and time format.
2014-11-13 01:04:59 +02:00
*
* @return Current date and time format.
2014-11-13 01:04:59 +02:00
*/
QString dateTimeFormat(QLocale::FormatType format) const;
2014-11-13 01:04:59 +02:00
/**
* Returns which measuring system we use.
2014-11-13 01:04:59 +02:00
*
* @return The preferred measuring system
2014-11-13 01:04:59 +02:00
*/
QLocale::MeasurementSystem measureSystem() const;
2014-11-13 01:04:59 +02:00
/**
* Given a double, converts that to a numeric string containing the localized numeric
* equivalent.
2014-11-13 01:04:59 +02:00
*
* Given 123456.78F, returns "123,456.78" (for some European country). If precision isn't
* specified or is < 0, then the precision is automatically chosen based on the number. This
* function is a wrapper that is provided for convenience.
2014-11-13 01:04:59 +02:00
*
* @param num The number to convert
* @param precision Number of decimal places used.
*
* @return The number as a localized string
* @see formatNumber(const QString, bool, int)
*/
QString formatNumber(double num, int precision = -1) const;
/**
* Given a string representing a number, converts that to a numeric
* string containing the localized numeric equivalent.
*
* e.g. given 123456.78F, return "123,456.78" (for some European country).
*
* If precision isn't specified or is < 0, then the precision is zero.
2014-11-13 01:04:59 +02:00
*
* @param numStr The number to format, as a string.
* @param round Round fractional digits. (default true)
* @param precision Number of fractional digits used for rounding. Unused if round=false.
*
* @return The number as a localized string
*/
QString formatNumber(const QString &numStr, bool round = true, int precision = -1) const;
/**
* Given an integer, converts that to a numeric string containing the localized numeric
* equivalent.
2014-11-13 01:04:59 +02:00
*
* Given 123456L, returns "123,456" (for some European country).
2014-11-13 01:04:59 +02:00
*
* @param num The number to convert
*
* @return The number as a localized string
*/
QString formatLong(long num) const;
/**
* Converts @p size from bytes to the string representation using the
* user's default binary unit dialect. The default unit dialect is
* IEC 60027-2.
*
* Example:
* formatByteSize(1024) returns "1.0 KiB" by default.
*
* @param size size in bytes
* @return converted size as a string - e.g. 123.4 KiB , 12.0 MiB
* @see BinaryUnitDialect
*/
QString formatByteSize(double size) const;
/**
* @since 4.4
*
* Converts @p size from bytes to the appropriate string representation
* using the binary unit dialect @p dialect and the specific units @p specificUnit.
*
* Example:
* formatByteSize(1000, unit, KLocale::UnitKiloByte) returns:
* for KLocale::MetricBinaryDialect, "1.0 kB",
* for KLocale::IECBinaryDialect, "0.9 KiB",
* for KLocale::JEDECBinaryDialect, "0.9 KB".
*
* @param size size in bytes
* @param precision number of places after the decimal point to use. KDE uses
* 1 by default so when in doubt use 1.
* @param specificUnit specific unit size to use in result. Use
* DefaultBinaryUnits to automatically select a unit that will return
* a sanely-sized number.
* @return converted size as a translated string including the units.
* E.g. "1.23 KiB", "2 GB" (JEDEC), "4.2 kB" (Metric).
* @see BinaryUnitDialect
*/
QString formatByteSize(double size, int precision,
BinarySizeUnits specificUnit = KLocale::DefaultBinaryUnits) const;
/**
* Given a number of milliseconds, converts that to a string containing the localized
* equivalent.
2014-11-13 01:04:59 +02:00
*
* Given formatDuration(60000), returns "1.0 minutes"
2014-11-13 01:04:59 +02:00
*
* @param mSec Time duration in milliseconds
* @return converted duration as a string - e.g. "5.5 seconds" "23.0 minutes"
*/
QString formatDuration(unsigned long mSec) const;
/**
* Returns a string formatted to the current locale's conventions regarding dates.
2014-11-13 01:04:59 +02:00
*
* @param date the date to be formatted
* @param format category of date format to use
2014-11-13 01:04:59 +02:00
*
* @return the date as a string
2014-11-13 01:04:59 +02:00
*/
QString formatDate(const QDate &date, QLocale::FormatType format = QLocale::ShortFormat) const;
2014-11-13 01:04:59 +02:00
/**
*
* Returns a string formatted to the current locale's conventions regarding times.
2014-11-13 01:04:59 +02:00
*
* @param time The time to be formatted.
* @param format category of time format to use
*
* @return The time as a string
2014-11-13 01:04:59 +02:00
*/
QString formatTime(const QTime &time, QLocale::FormatType format = QLocale::ShortFormat) const;
2014-11-13 01:04:59 +02:00
/**
*
* Returns a string formatted to the current locale's conventions regarding dates and times.
2014-11-13 01:04:59 +02:00
*
* @param datetime The date and time to be formatted.
* @param format category of date and time format to use
*
* @return The date and time as a string
2014-11-13 01:04:59 +02:00
*/
QString formatDateTime(const QDateTime &datetime, QLocale::FormatType format = QLocale::ShortFormat) const;
2014-11-13 01:04:59 +02:00
/**
* Converts a localized numeric string to a double.
*
* @param numStr the string we want to convert.
* @param ok the boolean that is set to false if it's not a number. If @p ok is null, it will
* be ignored
2014-11-13 01:04:59 +02:00
*
* @return The string converted to a double
2014-11-13 01:04:59 +02:00
*/
double readNumber(const QString &numStr, bool *ok = nullptr) const;
2014-11-13 01:04:59 +02:00
/**
* Converts a localized date string to a QDate.
*
* The bool pointed by ok will be invalid if the date entered was not valid.
*
* @param str the string we want to convert.
* @param format category of date format to use
2014-11-13 01:04:59 +02:00
*
* @return The string converted to a QDate
2014-11-13 01:04:59 +02:00
*/
QDate readDate(const QString &str, QLocale::FormatType format = QLocale::ShortFormat) const;
2014-11-13 01:04:59 +02:00
/**
* Converts a localized time string to a QTime.
*
* The bool pointed to by @p ok will be set to false if the time entered was not valid.
2014-11-13 01:04:59 +02:00
*
* @param str the string we want to convert.
* @param format category of time format to use
2014-11-13 01:04:59 +02:00
*
* @return The string converted to a QTime
2014-11-13 01:04:59 +02:00
*/
QTime readTime(const QString &str, QLocale::FormatType format = QLocale::ShortFormat) const;
2014-11-13 01:04:59 +02:00
/**
* Adds another catalog to search for translation lookup. This function is useful for extern
* libraries and/or code, that provide their own messages.
2014-11-13 01:04:59 +02:00
*
* If the catalog does not exist for the chosen language, it will be ignored and en_US will be
* used.
2014-11-13 01:04:59 +02:00
*
* @param catalog The catalog to add.
2014-11-13 01:04:59 +02:00
*/
void insertCatalog(const QString &catalog);
2014-11-13 01:04:59 +02:00
/**
* Removes a catalog for translation lookup.
*
* @param catalog The catalog to remove.
* @see insertCatalog()
2014-11-13 01:04:59 +02:00
*/
void removeCatalog(const QString &catalog);
2014-11-13 01:04:59 +02:00
/**
* Sets the active catalog for translation lookup.
2014-11-13 01:04:59 +02:00
*
* @param catalog The catalog to activate.
2014-11-13 01:04:59 +02:00
*/
void setActiveCatalog(const QString &catalog);
2014-11-13 01:04:59 +02:00
/**
* @since 4.5
*
* Raw translation from all loaded catalogs, with given context. Context + message are used as
* the lookup key in the catalog.
*
* Never use this directly to get message translations. See @p i18n and @p ki18n calls related
* to KLocalizedString.
*
* @param ctxt the context. May be null. Must be UTF-8 encoded.
* @param msg the message. Must not be null or empty. Must be UTF-8 encoded.
* @param lang language in which the translation was found. If no translation was found,
* KLocale::defaultLanguage() is reported. If null, the language is not reported.
* @param trans raw translation, or original if not found. If no translation was found,
* original message is reported. If null, the translation is not reported.
*
* @see KLocalizedString
2014-11-13 01:04:59 +02:00
*/
void translateRaw(const char *ctxt, const char *msg, QString *lang, QString *trans) const;
2014-11-13 01:04:59 +02:00
/**
* @since 4.5
2014-11-13 01:04:59 +02:00
*
* Raw translation from all loaded catalogs, with given context and singular/plural form.
* Context + singular form is used as the lookup key in the catalog.
2014-11-13 01:04:59 +02:00
*
* Never use this directly to get message translations. See @p i18n and @p ki18n calls related
* to KLocalizedString.
2014-11-13 01:04:59 +02:00
*
* @param ctxt the context. May be null. Must be UTF-8 encoded.
* @param singular the singular form. Must not be null or empty. Must be UTF-8 encoded.
* @param plural the plural form. Must not be null. Must be UTF-8 encoded.
* @param n number on which the forms are decided.
* @param lang language in which the translation was found. If no translation was found,
* KLocale::defaultLanguage() is reported. If null, the language is not reported.
* @param trans raw translation, or original if not found. If no translation was found,
* original message is reported (either plural or singular, as determined by
* @p n). If null, the translation is not reported.
2014-11-13 01:04:59 +02:00
*
* @see KLocalizedString
2014-11-13 01:04:59 +02:00
*/
void translateRaw(const char *ctxt, const char *singular, const char *plural,
unsigned long n, QString *lang, QString *trans) const;
2014-11-13 01:04:59 +02:00
/**
* Translates a message as a QTranslator is supposed to. The parameters are similar to @p i18n,
* but the result value has other semantics (it can be empty QString)
2014-11-13 01:04:59 +02:00
*/
QString translateQt(const char *context, const char *sourceText) const;
2014-11-13 01:04:59 +02:00
/**
* Convert a known language code to a human readable, localized form. If an unknown language
* code is supplied, empty string is returned; this will never happen if the code has been
* obtained by one of the KLocale methods.
2014-11-13 01:04:59 +02:00
*
* @param language the language code
2014-11-13 01:04:59 +02:00
*
* @return the human readable and localized form if the code is known, empty otherwise
*
* @see language
* @see languageList
* @see allLanguagesList
* @see installedLanguages
2014-11-13 01:04:59 +02:00
*/
QString languageCodeToName(const QString &language) const;
2014-11-13 01:04:59 +02:00
/**
* Convert a known country code to a human readable, localized form.
*
* If an unknown country code is supplied, empty string is returned; this will never happen
* if the code has been obtained by one of the KLocale methods.
2014-11-13 01:04:59 +02:00
*
* @param country the country code
2014-11-13 01:04:59 +02:00
*
* @return the human readable and localized form of the country name
2014-11-13 01:04:59 +02:00
*/
QString countryCodeToName(const QString &country) const;
2014-11-13 01:04:59 +02:00
/**
* Copies the catalogs of this object to an other KLocale object.
2014-11-13 01:04:59 +02:00
*
* @param locale the destination KLocale object
2014-11-13 01:04:59 +02:00
*/
void copyCatalogsTo(KLocale *locale);
2014-11-13 01:04:59 +02:00
/**
* @since 4.1
2014-11-13 01:04:59 +02:00
*
* Tries to find a path to the localized file for the given original path. This is intended
* mainly for non-text resources (images, sounds, etc.), whereas text resources should be
* handled in more specific ways.
*
* The possible localized paths are checked in turn by priority of set languages, in form of
* dirname/l10n/ll/basename, where dirname and basename are those of the original path, and ll
* is the language code.
*
* KDE core classes which resolve paths internally (e.g. KStandardDirs) will usually perform
* this lookup behind the scene. In general, you should pipe resource paths through this method
* only on explicit translators' request, or when a resource is an obvious candidate for
* localization (e.g. a splash screen or a custom icon with some text drawn on it).
*
* @param filePath path to the original file
2014-11-13 01:04:59 +02:00
*
* @return path to the localized file if found, original path otherwise
2014-11-13 01:04:59 +02:00
*/
QString localizedFilePath(const QString &filePath) const;
2014-11-13 01:04:59 +02:00
/**
* @since 4.2
*
* Removes accelerator marker from a UI text label.
*
* Accelerator marker is not always a plain ampersand (&), so it is not enough to just remove
* it by @c QString::remove(). The label may contain escaped markers ("&&") which must be
* resolved and skipped, as well as CJK-style markers ("Foo (&F)") where the whole parenthesis
* construct should be removed. Therefore always use this function to remove accelerator marker
* from UI labels.
2014-11-13 01:04:59 +02:00
*
* @param label UI label which may contain an accelerator marker
* @return label without the accelerator marker
2014-11-13 01:04:59 +02:00
*/
QString removeAcceleratorMarker(const QString &label) const;
2014-11-13 01:04:59 +02:00
/**
* @since 4.8
2014-11-13 01:04:59 +02:00
*
* Reparse locale configuration files for the current selected language.
2014-11-13 01:04:59 +02:00
*/
void reparseConfiguration();
2014-11-13 01:04:59 +02:00
/**
* @since 4.23
2014-11-13 01:04:59 +02:00
*
* Converts the KLocale object to QLocale one.
2014-11-13 01:04:59 +02:00
*/
QLocale toLocale() const;
2014-11-13 01:04:59 +02:00
/**
* Provides list of all known language codes.
2014-11-13 01:04:59 +02:00
*
* Use languageCodeToName(language) to get human readable, localized language names.
2014-11-13 01:04:59 +02:00
*
* @return list of all language codes
*
* @see languageCodeToName
* @see installedLanguages
*/
static QStringList allLanguagesList();
2014-11-13 01:04:59 +02:00
/**
* @since 4.6
*
* Provides list of all installed KDE Language Translations.
*
* Use languageCodeToName(language) to get human readable, localized language names.
2014-11-13 01:04:59 +02:00
*
* @return list of all installed language codes
*
* @see languageCodeToName
*/
static QStringList installedLanguages();
2014-11-13 01:04:59 +02:00
/**
* Checks whether or not the active catalog is found for the given language.
2014-11-13 01:04:59 +02:00
*
* @param language language to check
2014-11-13 01:04:59 +02:00
*/
static bool isApplicationTranslatedInto(const QString &language);
2014-11-13 01:04:59 +02:00
/**
* Parses locale string into distinct parts.
*
2014-11-13 01:04:59 +02:00
* The format of locale is language_COUNTRY@modifier.CHARSET
*
* @param locale the locale string to split
* @param language set to the language part of the locale
* @param country set to the country part of the locale
* @param modifier set to the modifer part of the locale
* @param charset set to the charset part of the locale
*/
static void splitLocale(const QString &locale, QString &language, QString &country,
QString &modifier, QString &charset);
/**
* Returns the name of the internal language.
*
* @return Name of the default language
*/
static QString defaultLanguage();
private:
friend class KLocalePrivate;
KLocalePrivate *d;
2014-11-13 01:04:59 +02:00
};
#endif // KLOCALE_H