KDE Core/KLocale/Frameworks: Difference between revisions

From KDE Community Wiki
m (Fixed spelling)
 
(73 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== KDE Frameworks 5 Migration ==
== KDE Frameworks 5 Migration ==


This page summarises the steps required for the migration form KLocale to Qlocale in KDE Frameworks 5.
This page summarises the steps required for the migration from KLocale to Qlocale in KDE Frameworks 5.


=== Global locale ===
=== Split Locale and Translations ===


Qt does not have an equivalent to KGlobal::locale(), i.e. application level locale settings, it only has the Qt default and system locales which apply at system-wide level.
KLocale merges localisation and translation.  In Qt these are separate.  In Frameworks we will follow this model and split KLocale into 2.  We will use QLocale everywhere for the localisation methods.  We will use QTranslator / tr() in the tier 1 libraries where translation needs should be simple, but still hook this into the KDE translators workflow. In KDE applications we will use KLocalizedString (or a replacement) and i18n().


Qt also doesn't offer setXxx() api in QLocale to allow apps to override a setting while running, and probably never will.  Usually in KDE this is abused for things like temp changes to the date format when the normal format options api should be used, or a different locale object created.  This will be the correct way to do so in Frameworks.
A initial step has been made by replacing many <code>#include <klocale.h></code> references with <code>klocalizedstring.h</code> when i18n() is the only function required by a class.  Some other redundant includes will also be removed to leave only necessary code in place.
 
Later KLocalizedString (or replacement) will need porting to use QLocale instead of KLocale.
 
It should probably be made a requirement for all libraries migrated to the tiers that the KLocale => QLocale change is made.
 
=== Replace KDE Global Locale ===
 
KDE4 provides an app with its own KLocale instance which applies throughout the application instance and can be manipulated within that application instance.  KGlobal::locale() returns the applications locale, which defaults to the KDE Locale unless overridden using KGlobal::setLocale() or the applications config file.  This allows apps to seamlessly run with an alternative locale than the system locale and is a feature we need to retain.
 
Qt5 provides a System Locale and a Default Locale.  Both are declared as global static variables in qlocale.ccp with file scope:
 
#ifndef QT_NO_SYSTEMLOCALE
QT_BEGIN_NAMESPACE
class QSystemLocale;
static QSystemLocale *QSystemLocale_globalSystemLocale();
QT_END_NAMESPACE
#endif
 
static const QLocalePrivate *default_lp = 0;
static uint default_number_options = 0;
 
The System Locale is what the system is currently set to and remains the same unless updated by the system, but the Default Locale can be changed by the application:
 
static void setDefault(const QLocale &locale);
 
void QLocale::setDefault(const QLocale &locale)
{
    default_lp = locale.d();
    default_number_options = locale.numberOptions();
}
 
This suggest the Qt Default Locale functions exactly the same as the KDE Global Locale in applying per application instance and will take the place of the KDE Global Locale.
 
To confirm:
* This does apply at application level
* This does apply to any Qt based libraries called by the application
 
The default QLocale() constructor returns the Qt Default Locale which is usually the System Locale unless changed using QLocale::setDefault().  QLocale::system() will always return the System Locale which may differ from the Default, so should be avoided unless explicitly required.
 
This implies that all code accessing KGlobal::locale().xxx() should simply be changed to be QLocale().yyy().  Due to the static privates the repeated construction should not be a performance issue, but apps can create a local var if needed, or we could request a QApplication::locale()?
 
Running with an envvar locale override will force Qt to override the system locale with the chosen locale under UNIX, behaviour under other platforms is unclear.
 
QLocale doesn't offer setXxx() api to allow apps to temporarily override a setting while running, and probably never will.  Usually in KDE this is abused for things like temp changes to the date format when the normal format options api should be used, or a different locale object created.


For example:
For example:


  KGlobal::locale()->setDateFormat("%y-%M-%d");
  KGlobal::locale()->setDateFormat("%y-%M-%d");
  label->setText(KGlobal::formatDate(myDate));
  label = KGlobal::formatDate(myDate);


When it should be:
When it should be:


  label->setText(KGlobal::formatDate(myDate, "%y-%M-%d"));
  label = KGlobal::formatDate(myDate, "%y-%M-%d");


And under Qt5 will become:
And under Qt5 will become:


  label->setText(QLocale().toString(myDate, "yyyy-MM-dd"));
  label = QLocale().toString(myDate, "yyyy-MM-dd");


I doubt there are few if any valid use cases that can't be easily recoded.  If the app wants say the date format to persist they can just save it in a local string and use that whenever they call the method.


Side-note: QLocale::setDefault() could be used in KDE4 Workspace with a custom QSystemLocale to force all Qt apps to use the KDE settings.


KGlobal::locale() returns the application's locale, which defaults to the system locale unless set different using KLocale::setLocale()
=== API Migration ===


QLocale() constructs the default locale which is the system locale unless set different using QLocale::setDefaultLocale()
While Qt 4.8 / Qt 5.0 provides most of the API needed, this are usually with very different method names, enums, and format codes.  Some APi will not be available before Qt 5.1.
QLocale::system() will always return the system locale.


Migration is likely to be a very manual affair.  A conversion table is provided below.  Note method footprints are simplified for clarity.


So change all KGlobal::locale().xxx() references to QLocale().xxx()???
The setters are documented at the bottom, most are not used anywhere, in the KDE repo. Only those setters that are used have conversions documented below.


Would we want a QApplication::locale() ???
Some interim migration tools may be required in a temporary class, such as ISO code conversions and splitLocale(), etc.  The need for this can be assessed as we go.


Side-note: QLocale::setDefaultLocale() could be used in KDE4 Workspace with a custom QSystemLocale to force all Qt apps to use the KDE settings!
This will be a lot of work for apps, so while frameworks switches to using QLocale directly, it may be desirable to provide a transition version of the KLocale api that uses QLocale as its backend and translates between the two.


=== API Migration ===
For example, the app code:


==== Locale ====
label = KGlobal::locale()->formatDate(date, "%y-%M-%d);


KLocale(const QString &catalog, KSharedConfig::Ptr config=KSharedConfig::Ptr())
could be replaced with:
KLocale(const QString &catalog, const QString &language, const QString &country=QString(), KConfig *config=0)


void    reparseConfiguration()
label = K5Locale()->formatDate(date, "%y-%M-%d);


static void    splitLocale(const QString &locale, QString &language, QString &country, QString &modifier, QString &charset)
with K5Locale in turn translating the format string to the format required by QLocale::toString().


==== Countries ====
==== Locale ====
 
QLocale uses an enum value where KDE uses the ISO Code.  Support may be added to Qt to convert between the two, or this may be provided in the planned QtIsoCodes addon.


{|
{| style="font-family:monospace"
|-
|-
! KLocale
! KLocale
Line 57: Line 100:
! Qt 5.1
! Qt 5.1
|-
|-
|QStringList    allCountriesList () const
| KLocale(QString catalog, KSharedConfig::Ptr config)
| Use QLocale() - Config no longer used - Set catalog in translation system
|
|
|-
| KLocale(QString catalog, QString language, QString country, KConfig *config)
| QLocale(Language language, Country country) - Config no longer used - Set catalog in translation system
|
|
|-
|-
|QString        country() const
| void reparseConfiguration()
| No longer used
|
|
|-
| static void splitLocale(QString locale, QString language, QString country, QString modifier, QString charset)
| Not public, need to instantiate a locale and obtain that way.
| Qt to be make public?
|}
==== Countries ====
QLocale uses an enum value where KDE uses a QString ISO Code.  Support may be added to Qt 5.1 to convert between the two, or this may be provided in the planned QtIsoCodes addon.
{| style="font-family:monospace"
|-
! KLocale API
! kdelibs usage
! Qt 5.0
! Qt 5.1
|-
| ISO codes as strings
|
|
| enum Country
| Conversion methods to be added
|-
| allCountriesList()
| -
| Not in Qt - loop from QLocale::AnyCountry to QLocale::LastCountry
| Qt to add
|-
| QString country()
| klocalizedstring.cpp
| Country country() or name.split('_').at(1)
| Provide ISO conversion methods
|-
| QString countryCodeToName(QString country)
| Various
| QString nativeCountryName() or QString countryToString(Country country)
| Add nativeCountryName(Country country) ???
|-
|-
|QString         countryCodeToName(const QString &country) const
| QString defaultCountry()
|
| -
| QLocale::C
|
|
|-
|-
|static QString defaultCountry()
| bool setCountry(QString country, KConfig *config)
|QLocale::C
| -
| Create new instance QLocale(Language language, Country country)
|
|
|-
|-
bool            setCountry (const QString &country, KConfig *config)
| QString        countryDivisionCode () const
| -
|
|
|
|
|-
|-
|QString         countryDivisionCode () const
| bool            setCountryDivisionCode (const QString &countryDivision)
| Not used anywhere
| -
|
|
|-
bool            setCountryDivisionCode (const QString &countryDivision)
| Not used anywhere
|
|
|}
|}
Line 88: Line 172:
====Languages====
====Languages====


QLocale uses an enum value where KDE uses the ISO Code.  Support may be added to Qt to convert between the two, or this may be provided in the planned QtIsoCodes addon.
QLocale uses an enum value where KDE uses a QString ISO Code.  Support may be added to Qt 5.1 to convert between the two, or this may be provided in the planned QtIsoCodes addon.  Need some more thought with regard to localisation and translation split.


QStringList    allLanguagesList() const


QString         language() const
{| style="font-family:monospace"
QString         languageCodeToName(const QString &language) const
|-
! KLocale
! kdelibs
! Qt 5.0
! Qt 5.1
|-
| ISO codes as strings
|
| enum Language
| Conversion methods to be added
|-
| QStringList    allLanguagesList() const
| kswitchlanguagedialog_p.cpp
| None, loop from QLocale::AnyLanguage to QLocale::LastLanguage
| To be added
|-
| QString language()
| Various
| Language language() or name.split('_').at(0)
| Provide ISO conversion methods
|-
| QString languageCodeToName(QString language)
| Various
| nativeLanguageName() or English languageToString(Language language)
| Add nativeLanguageName(Language language) ???
|-
| QStringList languageList()
| Various
| uiLanguages()
|
|-
| static QString  defaultLanguage()
| Various
| QLocale::C
|
|-
| bool setLanguage(QStringList languages)
| kswitchlanguagedialog_p.cpp
|
| KDE Platform module
|-
| bool setLanguage(const QString &language, KConfig *config)
| -
| Create new QLocale(Language language, Country country)
|
|}


QStringList    languageList() const
====Calendar / Date / Time====
=> uiLanguages()
static QString  defaultLanguage()


bool            setLanguage(const QStringList &languages)
Note that formatDate() and readDate() should actually be accessed via the calendar() instance or KLocalizedDate and these will be documented separately.
bool            setLanguage(const QString &language, KConfig *config)


====Digit Sets====


Not supported directly in Qt 5.0, some conversion happens implicitly, defer to 5.1 if really needed.
{| style="font-family:monospace"
|-
! KLocale API
! kdelibs usage
! Qt 5.0
! Qt 5.1
|-
| enum CalendarSystem
|
|
|
|-
| enum DateFormat
|
|
|
|-
| enum DateTimeComponent
|
|
|
|-
| enum DateTimeComponentFormat
|
|
|
|-
| enum DateTimeFormatOption
|
|
|
|-
| enum DateTimeFormatStandard
|
|
|
|-
| enum DateTimeParseMode
|
|
|
|-
| enum ReadDateFlags
|
|
|
|-
| enum ReadTimeFlags
|
|
|
|-
| enum TimeFormatOption
|
|
|
|-
| enum TimeProcessingOption
|
|
|
|-
| enum WeekNumberSystem
|
|
|
|-
| KCalendarSystem *calendar()
|
|
|
|-
| CalendarSystem calendarSystem()
| Only calendar related classes
| -
| Should be added
|-
| void setCalendarSystem(CalendarSystem calendarSystem)
| Only calendar related classes
| -
| Use api parm
|-
| QString calendarType()
| Deprecated
| -
| -
|-
| void setCalendar(QString calendarType)
| Deprecated
| -
| -
|-
| QString dateFormat()
| Only calendar related classes
| QString dateFormat() - but different formats!
| QString dateFormat(FullPattern) - but different formats!
|-
| void setDateFormat(QString format)
| Only calendar related classes
| Use api parm
|
|-
| QString dateFormatShort()
| Only calendar related classes
| QString dateFormat(ShortFormat) - but different formats!
| QString dateFormat(ShortPattern) - but different formats!
|-
| void setDateFormatShort(QString format)
| Only calendar related classes
| Use api parm
|
|-
| QString timeFormat()
| Only calendar related classes
| QString timeFormat() - but different formats!
| QString timeFormat(FullPattern) - but different formats!
|-
| void setTimeFormat(QString format)
| Only calendar related classes
| Use api parm
|
|-
| bool dateMonthNamePossessive()
| Only calendar related classes
| Remove
|
|-
| QString dayPeriodText(QTime &time, DateTimeComponentFormat format)
| Only calendar related classes
| amText() and pmText()
| New Qt dayPeriodText() api
|-
| int weekDayOfPray()
| Only calendar related classes
| Remove, try replace with weekend days instead
|
|-
| WeekNumberSystem weekNumberSystem()
| Only calendar related classes
| Not supported
| New Qt api
|-
| int weekStartDay()
| Only calendar related classes
| QList<Qt::DayOfWeek> weekdays()
|
|-
| void setWeekStartDay(int day)
| -
| -
| Use new api parm
|-
| int workingWeekEndDay()
| Only calendar related classes
| QList<Qt::DayOfWeek> weekdays()
|
|-
| int workingWeekStartDay()
| Only calendar related classes
| QList<Qt::DayOfWeek> weekdays()
|
|-
| bool use12Clock()
| -
| Remove
|
|-
| QString formatDate(QDate date, DateFormat format)
| templateinterface.cpp, khtmlview.cpp, knewstuff2/ui/itemsview.cpp
| QString toString(QDate date, FormatType format)
| QString toString(QDate date, FormatPattern format)
|-
| QDate readDate(QString str, bool *ok)
| kdatetable.cpp
| QDate toDate(QString string, FormatType format) - for ok uses date.isValid()
| QDate toDate(QString string, FormatPattern format) - for ok uses date.isValid()
|-
| QDate readDate(QString intstr, const QString fmt, bool *ok)
| -
| QDate toDate(QString string, QString format) - for ok uses date.isValid() - different formats!
|
|-
| QDate readDate(QString str, ReadDateFlags flags, bool *ok)
| -
| Not supported, use other methods
| New Qt api
|-
| QString formatLocaleTime(QTime pTime, TimeFormatOptions options)
| -
| QString toString(QTime time, QString format) or QString toString(QTime time, FormatType format)
| New Qt api???
|-
| QTime readLocaleTime(QString str, bool *ok, TimeFormatOptions options, TimeProcessingOptions processing)
| -
| QTime toTime(QString string, FormatType format) or QTime toTime(QString string, QString format)
| New Qt api???
|-
| QString formatTime(QTime pTime, bool includeSecs, bool isDuration)
| ktexteditor/templateinterface.cpp, ktimecombobox.cpp, kio/global.cpp
| QString toString(QTime time, QString format) or QString toString(QTime time, FormatType format)
| New Qt api???
|-
| QTime readTime(QString str, bool *ok)
| ktimecombobox.cpp
| QTime toTime(QString string, FormatType format) or QTime toTime(QString string, QString format)
| New Qt api???
|-
| QTime readTime(QString str, ReadTimeFlags flags, bool *ok)
| -
| QTime toTime(QString string, FormatType format) or QTime toTime(QString string, QString format)
| New Qt api???
|-
| QString formatDuration(unsigned long mSec)
| -
| Not supported
| New Qt api
|-
| QString prettyFormatDuration(unsigned long mSec)
| kdeui/jobs/kwidgetjobtracker.cpp
| Not supported
| New Qt api
|-
| QString formatDateTime(QDateTime dateTime, DateFormat format, bool includeSecs)
| Various
| QString toString(QDateTime dateTime, FormatType format)
| QString toString(QDateTime dateTime, FormatPattern format)
|-
| QString formatDateTime(KDateTime dateTime, DateFormat format, DateTimeFormatOptions options)
| -
| ???
| ???
|}


QList<DigitSet> allDigitSetsList () const
====Numbers====
DigitSet        digitSet () const
DigitSet        monetaryDigitSet () const


void            setDigitSet (DigitSet digitSet)
Note that the QString methods now only apply the C locale, to localise properly you must use the QLocale methods.


QString        digitSetToName (DigitSet digitSet, bool withDigits=false) const
QString        convertDigits (const QString &str, DigitSet digitSet, bool ignoreContext=false) const


====Calendar / Date / Time====
{| style="font-family:monospace"
|-
! KLocale
! kdelibs
! Qt 5.0
! Qt 5.1
|-
| int decimalPlaces()
| -
| Not in Qt
| Add to Qt
|-
| void setDecimalPlaces(int digits)
| -
| Use precision parm of toString() methods - note defaults to 6 places!
| -
|-
| QString decimalSymbol()
| kdatetime.cpp, knumvalidator.h, knumvalidator.cpp
| QChar decimalPoint()
| Add to Qt
|-
| QString negativeSign()
| knumvalidator.h, knumvalidator.cpp
| QChar negativeSign()
| Add to Qt
|-
| QString positiveSign()
| knumvalidator.h, knumvalidator.cpp
| QChar positiveSign()
| Add to Qt
|-
| QString thousandsSeparator()
| knumvalidator.h, knumvalidator.cpp
| QChar groupSeparator()
| Add to Qt
|-
| QString formatLong(long num)
| kio/kfileitemdelegate.cpp
|
|
|-
| QString formatNumber(double num, int precision)
| About 9 uses
|
|
|-
| QString formatNumber(QString numStr, bool round, int precision)
| None?
|
|
|-
| double readNumber(QString numStr, bool *ok)
| kfontchooser.cpp
|
|
|}


const KCalendarSystem *    calendar () const
====Currency / Money====
KLocale::CalendarSystem    calendarSystem () const
QString    calendarType () const


QString    dateFormat () const
Basic functions supported in Qt 5.0, more in 5.1, advanced features of KCurrencyCode to be moved to new QtIsoCodes library.
QString    dateFormatShort () const
bool    dateMonthNamePossessive () const
DigitSet    dateTimeDigitSet () const
QString    dayPeriodText (const QTime &time, DateTimeComponentFormat format=DefaultComponentFormat) const
QString    formatDate (const QDate &date, DateFormat format=LongDate) const
QString    formatDateTime (const QDateTime &dateTime, DateFormat format=ShortDate, bool includeSecs=false) const
QString    formatDateTime (const KDateTime &dateTime, DateFormat format=ShortDate, DateTimeFormatOptions options=0) const
QString    formatDuration (unsigned long mSec) const
QString    formatLocaleTime (const QTime &pTime, TimeFormatOptions options=KLocale::TimeDefault) const
QString    formatTime (const QTime &pTime, bool includeSecs=false, bool isDuration=false) const
QString    prettyFormatDuration (unsigned long mSec) const


QDate      readDate (const QString &str, bool *ok=0) const
{| style="font-family:monospace"
QDate      readDate (const QString &intstr, const QString &fmt, bool *ok=0) const
|-
QDate      readDate (const QString &str, ReadDateFlags flags, bool *ok=0) const
! KLocale
QTime      readLocaleTime (const QString &str, bool *ok=0, TimeFormatOptions options=KLocale::TimeDefault, TimeProcessingOptions processing=ProcessNonStrict) const
! kdelibs
QTime      readTime (const QString &str, bool *ok=0) const
! Qt 5.0
QTime      readTime (const QString &str, ReadTimeFlags flags, bool *ok=0) const
! Qt 5.1
|-
| KCurrencyCode *currency()
|
|
|
|-
| QString currencyCode()
|
|
|
|-
| static QString defaultCurrencyCode()
|
|
|
|-
| QStringList currencyCodeList()
|
|
|
|-
| QString currencySymbol()
|
|
|
|-
| int monetaryDecimalPlaces()
|
|
|
|-
| QString monetaryDecimalSymbol()
|
|
|
|-
| QString monetaryThousandsSeparator()
|
|
|
|-
| SignPosition negativeMonetarySignPosition()
|
|
|
|-
| bool negativePrefixCurrencySymbol()
|
|
|
|-
| SignPosition positiveMonetarySignPosition()
|
|
|
|-
| bool positivePrefixCurrencySymbol()
|
|
|
|-
| QString formatMoney(double num, QString currency, int precision)
|
|
|
|-
| double readMoney(QString numStr, bool *ok)
|
|
|
|}


void        setCalendar (const QString &calendarType)
====Digit Sets====
void        setCalendarSystem (KLocale::CalendarSystem calendarSystem)
void        setDateFormat (const QString &format)
void        setDateFormatShort (const QString &format)
void        setDateMonthNamePossessive (bool possessive)
void        setDateTimeDigitSet (DigitSet digitSet)
void        setTimeFormat (const QString &format)
void        setWeekDayOfPray (int day)
void        setWeekNumberSystem (KLocale::WeekNumberSystem weekNumberSystem)
void        setWeekStartDay (int day)
void        setWorkingWeekEndDay (int day)
void        setWorkingWeekStartDay (int day)


bool    use12Clock () const
Not supported directly in Qt 5.0, some conversion happens implicitly, defer to 5.1 if really needed.  In ICU fully supported implicitly.
=> Remove
int    weekDayOfPray () const
=> Remove, try replace with weeend days instead


KLocale::WeekNumberSystem  weekNumberSystem ()
{| style="font-family:monospace"
KLocale::WeekNumberSystem  weekNumberSystem () const
|-
int    weekStartDay () const
! KLocale
int    workingWeekEndDay () const
! kdelibs
int    workingWeekStartDay () const
! Qt 5.0
QString     timeFormat () const
! Qt 5.1
|-
| QList<DigitSet> allDigitSetsList()
|
|
|
|-
| DigitSet digitSet()
|
|
|
|-
| DigitSet monetaryDigitSet()
|
|
|
|-
| DigitSet dateTimeDigitSet()
|
|
|
|-
| QString digitSetToName(DigitSet digitSet, bool withDigits)
|
|
|
|-
| QString convertDigits(const QString &str, DigitSet digitSet, bool ignoreContext)
|
|
|
|}


====Numbers====
====Binary Units====


int        decimalPlaces() const
Not supported in QLocale or ICU.  Should be able to add api to Qt 5.1.  May require transition code independent from KLocale?
QString    decimalSymbol() const
int        fracDigits() const
QString    negativeSign() const
QString    positiveSign() const
QString    thousandsSeparator() const


void        setDecimalPlaces(int digits)
void        setDecimalSymbol(const QString &symbol)
void        setFracDigits(int digits)
void        setNegativeSign(const QString &sign)
void        setPositiveSign(const QString &sign)
void        setThousandsSeparator(const QString &separator)


QString    formatLong(long num) const
{| style="font-family:monospace"
QString     formatNumber(double num, int precision=-1) const
|-
QString     formatNumber(const QString &numStr, bool round=true, int precision=-1) const
! KLocale
! kdelibs
! Qt 5.0
! Qt 5.1
|-
| enum BinarySizeUnits
|
|
|
|-
| enum BinaryUnitDialect
|
|
|
|-
| BinaryUnitDialect binaryUnitDialect()
|
|
|
|-
| QString formatByteSize(double size)
|
|
|
|-
| QString formatByteSize(double size, int precision, BinaryUnitDialect dialect, BinarySizeUnits specificUnit)
|
|
|
|}


double      readNumber(const QString &numStr, bool *ok=0) const
==== Measurement Systems ====


====Currency / Money====
KLocale::pageSize() is widely misunderstood in KDE to mean the printers default paper size, i.e. what is actually in the printer, which should come from QPrnterInfo instead.  It is actually the default page size for apps like KWord to use.  No equivalent in QLocale but is in CLDR/ICU so will be added to Qt.  In the interim the measure system could be an analog, i.e. Imperial = Letter, otherwise A4.


KCurrencyCode *currency () const
Need more checks on use of setPageSize, many false positives from QPrinter.
QString        currencyCode () const
QStringList    currencyCodeList () const
QString        currencySymbol () const
int            monetaryDecimalPlaces () const
QString        monetaryDecimalSymbol () const
QString        monetaryThousandsSeparator () const
SignPosition  negativeMonetarySignPosition () const
bool          negativePrefixCurrencySymbol () const
SignPosition  positiveMonetarySignPosition () const
bool          positivePrefixCurrencySymbol () const
static QString  defaultCurrencyCode ()


void    setCurrencyCode (const QString &newCurrencyCode)
{| style="font-family:monospace"
void    setCurrencySymbol (const QString &symbol)
|-
void    setMonetaryDecimalPlaces (int digits)
! KLocale
void    setMonetaryDecimalSymbol (const QString &symbol)
! kdelibs
void    setMonetaryDigitSet (DigitSet digitSet)
! Qt 5.0
void   setMonetaryThousandsSeparator (const QString &separator)
! Qt 5.1
void    setNegativeMonetarySignPosition (SignPosition signpos)
|-
void    setNegativePrefixCurrencySymbol (bool prefix)
| enum MeasureSystem { Metric, Imperial }
void    setPositiveMonetarySignPosition (SignPosition signpos)
|
void    setPositivePrefixCurrencySymbol (bool prefix)
| enum MeasurementSystem { MetricSystem, ImperialSystem }
|
|-
| MeasureSystem  measureSystem()
| -
| MeasurementSystem measurementSystem()
|
|-
| int pageSize()
| -
| Not in Qt, use meaurementSystem() == ImperialSystem as analog
| Add to Qt
|-
| void setPageSize(int paperFormat)
|
| Not in Qt
|
|}


QString    formatMoney (double num, const QString &currency=QString(), int precision=-1) const
==== Encoding ====


double readMoney (const QString &numStr, bool *ok=0) const
Not too sure if this belongs in Locale or Translation? Is this something for QString?


====Binary Units====


Not supported in QLocale.
{| style="font-family:monospace"
|-
! KLocale
! kdelibs
! Qt 5.0
! Qt 5.1
|-
| QTextCodec *codecForEncoding()
|
|
|
|-
| const QByteArray encoding()
|
|
|
|-
| bool setEncoding(int mibEnum)
|
|
|
|-
| int encodingMib()
|
|
|
|-
| int fileEncodingMib()
|
|
|
|}


BinaryUnitDialect  binaryUnitDialect () const
====Translations====
QString    formatByteSize (double size) const
QString    formatByteSize (double size, int precision, BinaryUnitDialect dialect=KLocale::DefaultBinaryDialect, BinarySizeUnits specificUnit=KLocale::DefaultBinaryUnits) const
void    setBinaryUnitDialect (BinaryUnitDialect newDialect)


====Measurement Systems====
Replace with QTranslator / tr() at lowest level, or new KTranslator at higher level.


MeasureSystem  measureSystem () const
{| style="font-family:monospace"
void   setMeasureSystem (MeasureSystem value)
|-
 
! KLocale
int    pageSize () const
! kdelibs
void   setPageSize (int paperFormat)
! Qt 5.0
 
! Qt 5.1
====Encoding====
|-
 
| QStringList installedLanguages()
QTextCodec *    codecForEncoding () const
|
const QByteArray    encoding () const
|
int    encodingMib () const
|
int    fileEncodingMib () const
|-
bool    nounDeclension () const
| bool isApplicationTranslatedInto(QString language)
QString    removeAcceleratorMarker (const QString &label) const
|
bool    setEncoding (int mibEnum)
|
|
|-
| bool useTranscript()
|
|
|
|-
| QString localizedFilePath(QString filePath)
|
|
|
|-
| static QString  langLookup(QString fname, const char *rtype)
|
|
|
|-
| bool nounDeclension()
|
|
|
|-
| QString removeAcceleratorMarker(QString label)
|
|
|
|-
| void copyCatalogsTo(KLocale *locale)
|
|
|
|-
| void insertCatalog(QString catalog)
|
|
|
|-
| void removeCatalog(QString catalog)
|
|
|
|-
| void setActiveCatalog(QString catalog)
|
|
|
|-
| static void setMainCatalog(const char *catalog)
|
|
|
|-
| QString translateQt(const char *context, const char *sourceText, const char *comment)
|
|
|
|-
| void translateRaw(const char *singular, const char *plural, unsigned long n, QString *lang, QString *trans)
|
|
|
|-
| void translateRaw(const char *ctxt, const char *singular, const char *plural, unsigned long n, QString *lang, QString *trans)
|
|
|
|-
| void translateRaw(const char *msg, QString *lang, QString *trans)
|
|
|
|-
| void translateRaw(const char *ctxt, const char *msg, QString *lang, QString *trans)
|
|
|
|-
| void translateRawFrom(const char *catname, const char *msg, QString *lang, QString *trans)
|
|
|
|-
| void translateRawFrom(const char *catname, const char *singular, const char *plural, unsigned long n, QString *lang, QString *trans)
|
|
|
|-
| void translateRawFrom(const char *catname, const char *ctxt, const char *msg, QString *lang, QString *trans)
|
|
|
|-
| void translateRawFrom(const char *catname, const char *ctxt, const char *singular, const char *plural, unsigned long n, QString *lang, QString *trans)
|
|
|
|}


==== Setters Usage ====


====Translations====
Search using lxr of all places using KLocale setters.


Replace with QTranslator / tr() at lowest level, or new KTranslator at higher level.
Many hits, need more checks:


QStringList    installedLanguages () const
setCountry()
bool            isApplicationTranslatedInto (const QString &language)
setEncoding()
setLanguage()
setLanguage()
setPageSize()


void            copyCatalogsTo (KLocale *locale)
Some uses, most are very simple to port away in KDE 4.9:
void            insertCatalog (const QString &catalog)
void            removeCatalog (const QString &catalog)
void            setActiveCatalog (const QString &catalog)
static void    setMainCatalog (const char *catalog)


bool            useTranscript () const
setCalendar() - Date widgets, etc
setCalendarSystem() - Date widgets, etc
setDateFormat() - Only kdm, kcm
setDateFormatShort() - Clock Applet
setTimeFormat() - Some uses
setWeekStartDay() - KMyMoney


QString        translateQt(const char *context, const char *sourceText, const char *comment) const
No real code, only kcm and tests
void            translateRaw(const char *singular, const char *plural, unsigned long n, QString *lang, QString *trans) const
void            translateRaw(const char *ctxt, const char *singular, const char *plural, unsigned long n, QString *lang, QString *trans) const
void            translateRaw(const char *msg, QString *lang, QString *trans) const
void            translateRaw(const char *ctxt, const char *msg, QString *lang, QString *trans) const
void            translateRawFrom(const char *catname, const char *msg, QString *lang, QString *trans) const
void            translateRawFrom(const char *catname, const char *singular, const char *plural, unsigned long n, QString *lang, QString *trans) const
void            translateRawFrom(const char *catname, const char *ctxt, const char *msg, QString *lang, QString *trans) const
void            translateRawFrom(const char *catname, const char *ctxt, const char *singular, const char *plural, unsigned long n, QString *lang, QString *trans) const


QString        localizedFilePath (const QString &filePath) const
setBinaryUnitDialect()
static QString langLookup (const QString &fname, const char *rtype="html")
setCountryDivisionCode()
setCurrencyCode()
setCurrencySymbol()
setDateMonthNamePossessive()
setDateTimeDigitSet()
setDecimalPlaces()
setDecimalSymbol()
setDigitSet()
setFracDigits()
setMeasureSystem()
setMonetaryDecimalPlaces()
setMonetaryDecimalSymbol()
setMonetaryDigitSet()
setMonetaryThousandsSeparator()
setNegativeMonetarySignPosition()
setNegativePrefixCurrencySymbol()
setNegativeSign()
setPositiveMonetarySignPosition()
setPositivePrefixCurrencySymbol()
setPositiveSign()
setThousandsSeparator()
setWeekDayOfPray()
setWeekNumberSystem()
setWorkingWeekEndDay()
  setWorkingWeekStartDay()

Latest revision as of 10:02, 2 October 2016

KDE Frameworks 5 Migration

This page summarises the steps required for the migration from KLocale to Qlocale in KDE Frameworks 5.

Split Locale and Translations

KLocale merges localisation and translation. In Qt these are separate. In Frameworks we will follow this model and split KLocale into 2. We will use QLocale everywhere for the localisation methods. We will use QTranslator / tr() in the tier 1 libraries where translation needs should be simple, but still hook this into the KDE translators workflow. In KDE applications we will use KLocalizedString (or a replacement) and i18n().

A initial step has been made by replacing many #include <klocale.h> references with klocalizedstring.h when i18n() is the only function required by a class. Some other redundant includes will also be removed to leave only necessary code in place.

Later KLocalizedString (or replacement) will need porting to use QLocale instead of KLocale.

It should probably be made a requirement for all libraries migrated to the tiers that the KLocale => QLocale change is made.

Replace KDE Global Locale

KDE4 provides an app with its own KLocale instance which applies throughout the application instance and can be manipulated within that application instance. KGlobal::locale() returns the applications locale, which defaults to the KDE Locale unless overridden using KGlobal::setLocale() or the applications config file. This allows apps to seamlessly run with an alternative locale than the system locale and is a feature we need to retain.

Qt5 provides a System Locale and a Default Locale. Both are declared as global static variables in qlocale.ccp with file scope:

#ifndef QT_NO_SYSTEMLOCALE
QT_BEGIN_NAMESPACE
class QSystemLocale;
static QSystemLocale *QSystemLocale_globalSystemLocale();
QT_END_NAMESPACE
#endif
static const QLocalePrivate *default_lp = 0;
static uint default_number_options = 0;

The System Locale is what the system is currently set to and remains the same unless updated by the system, but the Default Locale can be changed by the application:

static void setDefault(const QLocale &locale);
void QLocale::setDefault(const QLocale &locale)
{
    default_lp = locale.d();
    default_number_options = locale.numberOptions();
}

This suggest the Qt Default Locale functions exactly the same as the KDE Global Locale in applying per application instance and will take the place of the KDE Global Locale.

To confirm:

  • This does apply at application level
  • This does apply to any Qt based libraries called by the application

The default QLocale() constructor returns the Qt Default Locale which is usually the System Locale unless changed using QLocale::setDefault(). QLocale::system() will always return the System Locale which may differ from the Default, so should be avoided unless explicitly required.

This implies that all code accessing KGlobal::locale().xxx() should simply be changed to be QLocale().yyy(). Due to the static privates the repeated construction should not be a performance issue, but apps can create a local var if needed, or we could request a QApplication::locale()?

Running with an envvar locale override will force Qt to override the system locale with the chosen locale under UNIX, behaviour under other platforms is unclear.

QLocale doesn't offer setXxx() api to allow apps to temporarily override a setting while running, and probably never will. Usually in KDE this is abused for things like temp changes to the date format when the normal format options api should be used, or a different locale object created.

For example:

KGlobal::locale()->setDateFormat("%y-%M-%d");
label = KGlobal::formatDate(myDate);

When it should be:

label = KGlobal::formatDate(myDate, "%y-%M-%d");

And under Qt5 will become:

label = QLocale().toString(myDate, "yyyy-MM-dd");

I doubt there are few if any valid use cases that can't be easily recoded. If the app wants say the date format to persist they can just save it in a local string and use that whenever they call the method.

Side-note: QLocale::setDefault() could be used in KDE4 Workspace with a custom QSystemLocale to force all Qt apps to use the KDE settings.

API Migration

While Qt 4.8 / Qt 5.0 provides most of the API needed, this are usually with very different method names, enums, and format codes. Some APi will not be available before Qt 5.1.

Migration is likely to be a very manual affair. A conversion table is provided below. Note method footprints are simplified for clarity.

The setters are documented at the bottom, most are not used anywhere, in the KDE repo. Only those setters that are used have conversions documented below.

Some interim migration tools may be required in a temporary class, such as ISO code conversions and splitLocale(), etc. The need for this can be assessed as we go.

This will be a lot of work for apps, so while frameworks switches to using QLocale directly, it may be desirable to provide a transition version of the KLocale api that uses QLocale as its backend and translates between the two.

For example, the app code:

label = KGlobal::locale()->formatDate(date, "%y-%M-%d);

could be replaced with:

label = K5Locale()->formatDate(date, "%y-%M-%d);

with K5Locale in turn translating the format string to the format required by QLocale::toString().

Locale

KLocale Qt 5.0 Qt 5.1
KLocale(QString catalog, KSharedConfig::Ptr config) Use QLocale() - Config no longer used - Set catalog in translation system
KLocale(QString catalog, QString language, QString country, KConfig *config) QLocale(Language language, Country country) - Config no longer used - Set catalog in translation system
void reparseConfiguration() No longer used
static void splitLocale(QString locale, QString language, QString country, QString modifier, QString charset) Not public, need to instantiate a locale and obtain that way. Qt to be make public?

Countries

QLocale uses an enum value where KDE uses a QString ISO Code. Support may be added to Qt 5.1 to convert between the two, or this may be provided in the planned QtIsoCodes addon.


KLocale API kdelibs usage Qt 5.0 Qt 5.1
ISO codes as strings enum Country Conversion methods to be added
allCountriesList() - Not in Qt - loop from QLocale::AnyCountry to QLocale::LastCountry Qt to add
QString country() klocalizedstring.cpp Country country() or name.split('_').at(1) Provide ISO conversion methods
QString countryCodeToName(QString country) Various QString nativeCountryName() or QString countryToString(Country country) Add nativeCountryName(Country country) ???
QString defaultCountry() - QLocale::C
bool setCountry(QString country, KConfig *config) - Create new instance QLocale(Language language, Country country)
QString countryDivisionCode () const -
bool setCountryDivisionCode (const QString &countryDivision) -

Languages

QLocale uses an enum value where KDE uses a QString ISO Code. Support may be added to Qt 5.1 to convert between the two, or this may be provided in the planned QtIsoCodes addon. Need some more thought with regard to localisation and translation split.


KLocale kdelibs Qt 5.0 Qt 5.1
ISO codes as strings enum Language Conversion methods to be added
QStringList allLanguagesList() const kswitchlanguagedialog_p.cpp None, loop from QLocale::AnyLanguage to QLocale::LastLanguage To be added
QString language() Various Language language() or name.split('_').at(0) Provide ISO conversion methods
QString languageCodeToName(QString language) Various nativeLanguageName() or English languageToString(Language language) Add nativeLanguageName(Language language) ???
QStringList languageList() Various uiLanguages()
static QString defaultLanguage() Various QLocale::C
bool setLanguage(QStringList languages) kswitchlanguagedialog_p.cpp KDE Platform module
bool setLanguage(const QString &language, KConfig *config) - Create new QLocale(Language language, Country country)

Calendar / Date / Time

Note that formatDate() and readDate() should actually be accessed via the calendar() instance or KLocalizedDate and these will be documented separately.


KLocale API kdelibs usage Qt 5.0 Qt 5.1
enum CalendarSystem
enum DateFormat
enum DateTimeComponent
enum DateTimeComponentFormat
enum DateTimeFormatOption
enum DateTimeFormatStandard
enum DateTimeParseMode
enum ReadDateFlags
enum ReadTimeFlags
enum TimeFormatOption
enum TimeProcessingOption
enum WeekNumberSystem
KCalendarSystem *calendar()
CalendarSystem calendarSystem() Only calendar related classes - Should be added
void setCalendarSystem(CalendarSystem calendarSystem) Only calendar related classes - Use api parm
QString calendarType() Deprecated - -
void setCalendar(QString calendarType) Deprecated - -
QString dateFormat() Only calendar related classes QString dateFormat() - but different formats! QString dateFormat(FullPattern) - but different formats!
void setDateFormat(QString format) Only calendar related classes Use api parm
QString dateFormatShort() Only calendar related classes QString dateFormat(ShortFormat) - but different formats! QString dateFormat(ShortPattern) - but different formats!
void setDateFormatShort(QString format) Only calendar related classes Use api parm
QString timeFormat() Only calendar related classes QString timeFormat() - but different formats! QString timeFormat(FullPattern) - but different formats!
void setTimeFormat(QString format) Only calendar related classes Use api parm
bool dateMonthNamePossessive() Only calendar related classes Remove
QString dayPeriodText(QTime &time, DateTimeComponentFormat format) Only calendar related classes amText() and pmText() New Qt dayPeriodText() api
int weekDayOfPray() Only calendar related classes Remove, try replace with weekend days instead
WeekNumberSystem weekNumberSystem() Only calendar related classes Not supported New Qt api
int weekStartDay() Only calendar related classes QList<Qt::DayOfWeek> weekdays()
void setWeekStartDay(int day) - - Use new api parm
int workingWeekEndDay() Only calendar related classes QList<Qt::DayOfWeek> weekdays()
int workingWeekStartDay() Only calendar related classes QList<Qt::DayOfWeek> weekdays()
bool use12Clock() - Remove
QString formatDate(QDate date, DateFormat format) templateinterface.cpp, khtmlview.cpp, knewstuff2/ui/itemsview.cpp QString toString(QDate date, FormatType format) QString toString(QDate date, FormatPattern format)
QDate readDate(QString str, bool *ok) kdatetable.cpp QDate toDate(QString string, FormatType format) - for ok uses date.isValid() QDate toDate(QString string, FormatPattern format) - for ok uses date.isValid()
QDate readDate(QString intstr, const QString fmt, bool *ok) - QDate toDate(QString string, QString format) - for ok uses date.isValid() - different formats!
QDate readDate(QString str, ReadDateFlags flags, bool *ok) - Not supported, use other methods New Qt api
QString formatLocaleTime(QTime pTime, TimeFormatOptions options) - QString toString(QTime time, QString format) or QString toString(QTime time, FormatType format) New Qt api???
QTime readLocaleTime(QString str, bool *ok, TimeFormatOptions options, TimeProcessingOptions processing) - QTime toTime(QString string, FormatType format) or QTime toTime(QString string, QString format) New Qt api???
QString formatTime(QTime pTime, bool includeSecs, bool isDuration) ktexteditor/templateinterface.cpp, ktimecombobox.cpp, kio/global.cpp QString toString(QTime time, QString format) or QString toString(QTime time, FormatType format) New Qt api???
QTime readTime(QString str, bool *ok) ktimecombobox.cpp QTime toTime(QString string, FormatType format) or QTime toTime(QString string, QString format) New Qt api???
QTime readTime(QString str, ReadTimeFlags flags, bool *ok) - QTime toTime(QString string, FormatType format) or QTime toTime(QString string, QString format) New Qt api???
QString formatDuration(unsigned long mSec) - Not supported New Qt api
QString prettyFormatDuration(unsigned long mSec) kdeui/jobs/kwidgetjobtracker.cpp Not supported New Qt api
QString formatDateTime(QDateTime dateTime, DateFormat format, bool includeSecs) Various QString toString(QDateTime dateTime, FormatType format) QString toString(QDateTime dateTime, FormatPattern format)
QString formatDateTime(KDateTime dateTime, DateFormat format, DateTimeFormatOptions options) - ??? ???

Numbers

Note that the QString methods now only apply the C locale, to localise properly you must use the QLocale methods.


KLocale kdelibs Qt 5.0 Qt 5.1
int decimalPlaces() - Not in Qt Add to Qt
void setDecimalPlaces(int digits) - Use precision parm of toString() methods - note defaults to 6 places! -
QString decimalSymbol() kdatetime.cpp, knumvalidator.h, knumvalidator.cpp QChar decimalPoint() Add to Qt
QString negativeSign() knumvalidator.h, knumvalidator.cpp QChar negativeSign() Add to Qt
QString positiveSign() knumvalidator.h, knumvalidator.cpp QChar positiveSign() Add to Qt
QString thousandsSeparator() knumvalidator.h, knumvalidator.cpp QChar groupSeparator() Add to Qt
QString formatLong(long num) kio/kfileitemdelegate.cpp
QString formatNumber(double num, int precision) About 9 uses
QString formatNumber(QString numStr, bool round, int precision) None?
double readNumber(QString numStr, bool *ok) kfontchooser.cpp

Currency / Money

Basic functions supported in Qt 5.0, more in 5.1, advanced features of KCurrencyCode to be moved to new QtIsoCodes library.

KLocale kdelibs Qt 5.0 Qt 5.1
KCurrencyCode *currency()
QString currencyCode()
static QString defaultCurrencyCode()
QStringList currencyCodeList()
QString currencySymbol()
int monetaryDecimalPlaces()
QString monetaryDecimalSymbol()
QString monetaryThousandsSeparator()
SignPosition negativeMonetarySignPosition()
bool negativePrefixCurrencySymbol()
SignPosition positiveMonetarySignPosition()
bool positivePrefixCurrencySymbol()
QString formatMoney(double num, QString currency, int precision)
double readMoney(QString numStr, bool *ok)

Digit Sets

Not supported directly in Qt 5.0, some conversion happens implicitly, defer to 5.1 if really needed. In ICU fully supported implicitly.

KLocale kdelibs Qt 5.0 Qt 5.1
QList<DigitSet> allDigitSetsList()
DigitSet digitSet()
DigitSet monetaryDigitSet()
DigitSet dateTimeDigitSet()
QString digitSetToName(DigitSet digitSet, bool withDigits)
QString convertDigits(const QString &str, DigitSet digitSet, bool ignoreContext)

Binary Units

Not supported in QLocale or ICU. Should be able to add api to Qt 5.1. May require transition code independent from KLocale?


KLocale kdelibs Qt 5.0 Qt 5.1
enum BinarySizeUnits
enum BinaryUnitDialect
BinaryUnitDialect binaryUnitDialect()
QString formatByteSize(double size)
QString formatByteSize(double size, int precision, BinaryUnitDialect dialect, BinarySizeUnits specificUnit)

Measurement Systems

KLocale::pageSize() is widely misunderstood in KDE to mean the printers default paper size, i.e. what is actually in the printer, which should come from QPrnterInfo instead. It is actually the default page size for apps like KWord to use. No equivalent in QLocale but is in CLDR/ICU so will be added to Qt. In the interim the measure system could be an analog, i.e. Imperial = Letter, otherwise A4.

Need more checks on use of setPageSize, many false positives from QPrinter.

KLocale kdelibs Qt 5.0 Qt 5.1
enum MeasureSystem { Metric, Imperial } enum MeasurementSystem { MetricSystem, ImperialSystem }
MeasureSystem measureSystem() - MeasurementSystem measurementSystem()
int pageSize() - Not in Qt, use meaurementSystem() == ImperialSystem as analog Add to Qt
void setPageSize(int paperFormat) Not in Qt

Encoding

Not too sure if this belongs in Locale or Translation? Is this something for QString?


KLocale kdelibs Qt 5.0 Qt 5.1
QTextCodec *codecForEncoding()
const QByteArray encoding()
bool setEncoding(int mibEnum)
int encodingMib()
int fileEncodingMib()

Translations

Replace with QTranslator / tr() at lowest level, or new KTranslator at higher level.

KLocale kdelibs Qt 5.0 Qt 5.1
QStringList installedLanguages()
bool isApplicationTranslatedInto(QString language)
bool useTranscript()
QString localizedFilePath(QString filePath)
static QString langLookup(QString fname, const char *rtype)
bool nounDeclension()
QString removeAcceleratorMarker(QString label)
void copyCatalogsTo(KLocale *locale)
void insertCatalog(QString catalog)
void removeCatalog(QString catalog)
void setActiveCatalog(QString catalog)
static void setMainCatalog(const char *catalog)
QString translateQt(const char *context, const char *sourceText, const char *comment)
void translateRaw(const char *singular, const char *plural, unsigned long n, QString *lang, QString *trans)
void translateRaw(const char *ctxt, const char *singular, const char *plural, unsigned long n, QString *lang, QString *trans)
void translateRaw(const char *msg, QString *lang, QString *trans)
void translateRaw(const char *ctxt, const char *msg, QString *lang, QString *trans)
void translateRawFrom(const char *catname, const char *msg, QString *lang, QString *trans)
void translateRawFrom(const char *catname, const char *singular, const char *plural, unsigned long n, QString *lang, QString *trans)
void translateRawFrom(const char *catname, const char *ctxt, const char *msg, QString *lang, QString *trans)
void translateRawFrom(const char *catname, const char *ctxt, const char *singular, const char *plural, unsigned long n, QString *lang, QString *trans)

Setters Usage

Search using lxr of all places using KLocale setters.

Many hits, need more checks:

setCountry()
setEncoding()
setLanguage()
setLanguage()
setPageSize()

Some uses, most are very simple to port away in KDE 4.9:

setCalendar() - Date widgets, etc
setCalendarSystem() - Date widgets, etc
setDateFormat() - Only kdm, kcm
setDateFormatShort() - Clock Applet
setTimeFormat() - Some uses
setWeekStartDay() - KMyMoney

No real code, only kcm and tests

setBinaryUnitDialect()
setCountryDivisionCode()
setCurrencyCode()
setCurrencySymbol()
setDateMonthNamePossessive()
setDateTimeDigitSet()
setDecimalPlaces()
setDecimalSymbol()
setDigitSet()
setFracDigits()
setMeasureSystem()
setMonetaryDecimalPlaces()
setMonetaryDecimalSymbol()
setMonetaryDigitSet()
setMonetaryThousandsSeparator()
setNegativeMonetarySignPosition()
setNegativePrefixCurrencySymbol()
setNegativeSign()
setPositiveMonetarySignPosition()
setPositivePrefixCurrencySymbol()
setPositiveSign()
setThousandsSeparator()
setWeekDayOfPray()
setWeekNumberSystem()
setWorkingWeekEndDay()
setWorkingWeekStartDay()