You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2475 lines
65 KiB
2475 lines
65 KiB
15 years ago
|
// -*- c-basic-offset: 2 -*-
|
||
|
/* This file is part of the KDE libraries
|
||
|
Copyright (c) 1997,2001 Stephan Kulow <coolo@kde.org>
|
||
|
Copyright (c) 1999 Preston Brown <pbrown@kde.org>
|
||
|
Copyright (c) 1999-2002 Hans Petter Bieker <bieker@kde.org>
|
||
|
Copyright (c) 2002 Lukas Tinkl <lukas@kde.org>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
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.
|
||
|
*/
|
||
|
|
||
|
#include <config.h>
|
||
|
|
||
|
#include <stdlib.h> // getenv
|
||
|
|
||
13 years ago
|
#include <tqtextcodec.h>
|
||
15 years ago
|
#include <tqfile.h>
|
||
|
#include <tqprinter.h>
|
||
|
#include <tqdatetime.h>
|
||
|
#include <tqfileinfo.h>
|
||
|
#include <tqregexp.h>
|
||
15 years ago
|
|
||
|
#include "kcatalogue.h"
|
||
12 years ago
|
#include "tdeglobal.h"
|
||
15 years ago
|
#include "kstandarddirs.h"
|
||
|
#include "ksimpleconfig.h"
|
||
|
#include "kinstance.h"
|
||
12 years ago
|
#include "tdeconfig.h"
|
||
15 years ago
|
#include "kdebug.h"
|
||
|
#include "kcalendarsystem.h"
|
||
|
#include "kcalendarsystemfactory.h"
|
||
12 years ago
|
#include "tdelocale.h"
|
||
15 years ago
|
|
||
|
#ifdef Q_WS_WIN
|
||
|
#include <windows.h>
|
||
|
#endif
|
||
|
|
||
13 years ago
|
static const char * const SYSTEM_MESSAGES = "tdelibs";
|
||
15 years ago
|
|
||
|
static const char *maincatalogue = 0;
|
||
|
|
||
12 years ago
|
class TDELocalePrivate
|
||
15 years ago
|
{
|
||
|
public:
|
||
|
int weekStartDay;
|
||
|
bool nounDeclension;
|
||
|
bool dateMonthNamePossessive;
|
||
15 years ago
|
TQStringList languageList;
|
||
|
TQStringList catalogNames; // list of all catalogs (regardless of language)
|
||
14 years ago
|
TQValueList<KCatalogue> catalogues; // list of all loaded catalogs, contains one instance per catalog name and language
|
||
15 years ago
|
TQString encoding;
|
||
|
TQTextCodec * codecForEncoding;
|
||
12 years ago
|
TDEConfig * config;
|
||
15 years ago
|
bool formatInited;
|
||
15 years ago
|
int /*TQPrinter::PageSize*/ pageSize;
|
||
12 years ago
|
TDELocale::MeasureSystem measureSystem;
|
||
15 years ago
|
TQStringList langTwoAlpha;
|
||
12 years ago
|
TDEConfig *languages;
|
||
15 years ago
|
|
||
15 years ago
|
TQString calendarType;
|
||
15 years ago
|
KCalendarSystem * calendar;
|
||
|
bool utf8FileEncoding;
|
||
15 years ago
|
TQString appName;
|
||
15 years ago
|
#ifdef Q_WS_WIN
|
||
|
char win32SystemEncoding[3+7]; //"cp " + lang ID
|
||
|
#endif
|
||
15 years ago
|
bool useMainCatalogue;
|
||
15 years ago
|
};
|
||
|
|
||
12 years ago
|
static TDELocale *this_klocale = 0;
|
||
15 years ago
|
|
||
12 years ago
|
TDELocale::TDELocale( const TQString & catalog, TDEConfig * config )
|
||
15 years ago
|
{
|
||
12 years ago
|
d = new TDELocalePrivate;
|
||
15 years ago
|
d->config = config;
|
||
|
d->languages = 0;
|
||
|
d->calendar = 0;
|
||
|
d->formatInited = false;
|
||
|
|
||
|
initEncoding(0);
|
||
|
initFileNameEncoding(0);
|
||
|
|
||
12 years ago
|
TDEConfig *cfg = d->config;
|
||
15 years ago
|
this_klocale = this;
|
||
12 years ago
|
if (!cfg) cfg = TDEGlobal::instance()->config();
|
||
15 years ago
|
this_klocale = 0;
|
||
|
Q_ASSERT( cfg );
|
||
|
|
||
|
d->appName = catalog;
|
||
|
initLanguageList( cfg, config == 0);
|
||
|
initMainCatalogues(catalog);
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::_initLanguage(TDEConfigBase *config)
|
||
15 years ago
|
{
|
||
|
if (this_klocale)
|
||
|
{
|
||
|
// ### HPB Why this cast??
|
||
12 years ago
|
this_klocale->initLanguageList((TDEConfig *) config, true);
|
||
15 years ago
|
// todo: adapt current catalog list: remove unused languages, insert main catalogs, if not already found
|
||
|
return this_klocale->language();
|
||
|
}
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::initMainCatalogues(const TQString & catalog)
|
||
15 years ago
|
{
|
||
|
// Use the first non-null string.
|
||
15 years ago
|
TQString mainCatalogue = catalog;
|
||
15 years ago
|
|
||
|
// don't use main catalogue if we're looking up .desktop translations
|
||
14 years ago
|
if (mainCatalogue.contains("desktop") == 0 || mainCatalogue.contains("kdesktop") == 1) {
|
||
15 years ago
|
if (maincatalogue) {
|
||
13 years ago
|
mainCatalogue = TQString::fromLatin1(maincatalogue);
|
||
15 years ago
|
}
|
||
|
}
|
||
15 years ago
|
|
||
|
if (mainCatalogue.isEmpty()) {
|
||
12 years ago
|
kdDebug(173) << "TDELocale instance created called without valid "
|
||
15 years ago
|
<< "catalog! Give an argument or call setMainCatalogue "
|
||
|
<< "before init" << endl;
|
||
|
}
|
||
|
else {
|
||
|
// do not use insertCatalogue here, that would already trigger updateCatalogs
|
||
|
d->catalogNames.append( mainCatalogue ); // application catalog
|
||
14 years ago
|
if (mainCatalogue.contains("desktop") == 0 || mainCatalogue.contains("kdesktop") == 1) { //don't bother if we're looking up desktop translations
|
||
13 years ago
|
d->catalogNames.append( SYSTEM_MESSAGES ); // always include tdelibs.mo
|
||
15 years ago
|
d->catalogNames.append( "kio" ); // always include kio.mo
|
||
|
d->catalogNames.append( "xdg-user-dirs" );
|
||
|
}
|
||
15 years ago
|
updateCatalogues(); // evaluate this for all languages
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::initLanguageList(TDEConfig * config, bool useEnv)
|
||
15 years ago
|
{
|
||
12 years ago
|
TDEConfigGroupSaver saver(config, "Locale");
|
||
15 years ago
|
|
||
|
m_country = config->readEntry( "Country" );
|
||
|
if ( m_country.isEmpty() )
|
||
|
m_country = defaultCountry();
|
||
|
|
||
|
// Reset the list and add the new languages
|
||
15 years ago
|
TQStringList languageList;
|
||
15 years ago
|
if ( useEnv )
|
||
15 years ago
|
languageList += TQStringList::split
|
||
13 years ago
|
(':', TQFile::decodeName( ::getenv("TDE_LANG") ));
|
||
15 years ago
|
|
||
|
languageList += config->readListEntry("Language", ':');
|
||
|
|
||
|
// same order as setlocale use
|
||
|
if ( useEnv )
|
||
|
{
|
||
|
// HPB: Only run splitLocale on the environment variables..
|
||
15 years ago
|
TQStringList langs;
|
||
15 years ago
|
|
||
15 years ago
|
langs << TQFile::decodeName( ::getenv("LC_ALL") );
|
||
|
langs << TQFile::decodeName( ::getenv("LC_MESSAGES") );
|
||
|
langs << TQFile::decodeName( ::getenv("LANG") );
|
||
15 years ago
|
|
||
15 years ago
|
for ( TQStringList::Iterator it = langs.begin();
|
||
15 years ago
|
it != langs.end();
|
||
|
++it )
|
||
|
{
|
||
15 years ago
|
TQString ln, ct, chrset;
|
||
15 years ago
|
splitLocale(*it, ln, ct, chrset);
|
||
|
|
||
|
if (!ct.isEmpty()) {
|
||
|
langs.insert(it, ln + '_' + ct);
|
||
|
if (!chrset.isEmpty())
|
||
|
langs.insert(it, ln + '_' + ct + '.' + chrset);
|
||
|
}
|
||
|
|
||
|
langs.insert(it, ln);
|
||
|
}
|
||
|
|
||
|
languageList += langs;
|
||
|
}
|
||
|
|
||
|
// now we have a language list -- let's use the first OK language
|
||
|
setLanguage( languageList );
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::initPluralTypes()
|
||
15 years ago
|
{
|
||
15 years ago
|
for ( TQValueList<KCatalogue>::Iterator it = d->catalogues.begin();
|
||
15 years ago
|
it != d->catalogues.end();
|
||
|
++it )
|
||
|
{
|
||
15 years ago
|
TQString language = (*it).language();
|
||
15 years ago
|
int pt = pluralType( language );
|
||
|
(*it).setPluralType( pt );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
12 years ago
|
int TDELocale::pluralType( const TQString & language )
|
||
15 years ago
|
{
|
||
15 years ago
|
for ( TQValueList<KCatalogue>::ConstIterator it = d->catalogues.begin();
|
||
15 years ago
|
it != d->catalogues.end();
|
||
|
++it )
|
||
|
{
|
||
|
if ( ((*it).name() == SYSTEM_MESSAGES ) && ((*it).language() == language )) {
|
||
|
return pluralType( *it );
|
||
|
}
|
||
|
}
|
||
13 years ago
|
// tdelibs.mo does not seem to exist for this language
|
||
15 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
12 years ago
|
int TDELocale::pluralType( const KCatalogue& catalog )
|
||
15 years ago
|
{
|
||
|
const char* pluralFormString =
|
||
|
I18N_NOOP("_: Dear translator, please do not translate this string "
|
||
|
"in any form, but pick the _right_ value out of "
|
||
|
"NoPlural/TwoForms/French... If not sure what to do mail "
|
||
|
"thd@kde.org and coolo@kde.org, they will tell you. "
|
||
|
"Better leave that out if unsure, the programs will "
|
||
|
"crash!!\nDefinition of PluralForm - to be set by the "
|
||
13 years ago
|
"translator of tdelibs.po");
|
||
15 years ago
|
TQString pf (catalog.translate( pluralFormString));
|
||
15 years ago
|
if ( pf.isEmpty() ) {
|
||
|
return -1;
|
||
|
}
|
||
|
else if ( pf == "NoPlural" )
|
||
|
return 0;
|
||
|
else if ( pf == "TwoForms" )
|
||
|
return 1;
|
||
|
else if ( pf == "French" )
|
||
|
return 2;
|
||
|
else if ( pf == "OneTwoRest" )
|
||
|
return 3;
|
||
|
else if ( pf == "Russian" )
|
||
|
return 4;
|
||
|
else if ( pf == "Polish" )
|
||
|
return 5;
|
||
|
else if ( pf == "Slovenian" )
|
||
|
return 6;
|
||
|
else if ( pf == "Lithuanian" )
|
||
|
return 7;
|
||
|
else if ( pf == "Czech" )
|
||
|
return 8;
|
||
|
else if ( pf == "Slovak" )
|
||
|
return 9;
|
||
|
else if ( pf == "Maltese" )
|
||
|
return 10;
|
||
|
else if ( pf == "Arabic" )
|
||
|
return 11;
|
||
|
else if ( pf == "Balcan" )
|
||
|
return 12;
|
||
|
else if ( pf == "Macedonian" )
|
||
|
return 13;
|
||
|
else if ( pf == "Gaeilge" )
|
||
|
return 14;
|
||
|
else {
|
||
|
kdWarning(173) << "Definition of PluralForm is none of "
|
||
|
<< "NoPlural/"
|
||
|
<< "TwoForms/"
|
||
|
<< "French/"
|
||
|
<< "OneTwoRest/"
|
||
|
<< "Russian/"
|
||
|
<< "Polish/"
|
||
|
<< "Slovenian/"
|
||
|
<< "Lithuanian/"
|
||
|
<< "Czech/"
|
||
|
<< "Slovak/"
|
||
|
<< "Arabic/"
|
||
|
<< "Balcan/"
|
||
|
<< "Macedonian/"
|
||
|
<< "Gaeilge/"
|
||
|
<< "Maltese: " << pf << endl;
|
||
|
exit(1);
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::doFormatInit() const
|
||
15 years ago
|
{
|
||
|
if ( d->formatInited ) return;
|
||
|
|
||
12 years ago
|
TDELocale * that = const_cast<TDELocale *>(this);
|
||
15 years ago
|
that->initFormat();
|
||
|
|
||
|
d->formatInited = true;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::initFormat()
|
||
15 years ago
|
{
|
||
12 years ago
|
TDEConfig *config = d->config;
|
||
12 years ago
|
if (!config) config = TDEGlobal::instance()->config();
|
||
15 years ago
|
Q_ASSERT( config );
|
||
|
|
||
12 years ago
|
kdDebug(173) << "TDELocale::initFormat" << endl;
|
||
15 years ago
|
|
||
|
// make sure the config files are read using the correct locale
|
||
12 years ago
|
// ### Why not add a TDEConfigBase::setLocale( const TDELocale * )?
|
||
15 years ago
|
// ### Then we could remove this hack
|
||
12 years ago
|
TDELocale *lsave = TDEGlobal::_locale;
|
||
12 years ago
|
TDEGlobal::_locale = this;
|
||
15 years ago
|
|
||
12 years ago
|
TDEConfigGroupSaver saver(config, "Locale");
|
||
15 years ago
|
|
||
|
KSimpleConfig entry(locate("locale",
|
||
13 years ago
|
TQString::fromLatin1("l10n/%1/entry.desktop")
|
||
15 years ago
|
.arg(m_country)), true);
|
||
|
entry.setGroup("KCM Locale");
|
||
|
|
||
|
// Numeric
|
||
|
#define readConfigEntry(key, default, save) \
|
||
13 years ago
|
save = entry.readEntry(key, TQString::fromLatin1(default)); \
|
||
15 years ago
|
save = config->readEntry(key, save);
|
||
|
|
||
|
#define readConfigNumEntry(key, default, save, type) \
|
||
|
save = (type)entry.readNumEntry(key, default); \
|
||
|
save = (type)config->readNumEntry(key, save);
|
||
|
|
||
|
#define readConfigBoolEntry(key, default, save) \
|
||
|
save = entry.readBoolEntry(key, default); \
|
||
|
save = config->readBoolEntry(key, save);
|
||
|
|
||
|
readConfigEntry("DecimalSymbol", ".", m_decimalSymbol);
|
||
|
readConfigEntry("ThousandsSeparator", ",", m_thousandsSeparator);
|
||
13 years ago
|
m_thousandsSeparator.replace( TQString::fromLatin1("$0"), TQString() );
|
||
15 years ago
|
//kdDebug(173) << "m_thousandsSeparator=" << m_thousandsSeparator << endl;
|
||
|
|
||
|
readConfigEntry("PositiveSign", "", m_positiveSign);
|
||
|
readConfigEntry("NegativeSign", "-", m_negativeSign);
|
||
|
|
||
|
// Monetary
|
||
|
readConfigEntry("CurrencySymbol", "$", m_currencySymbol);
|
||
|
readConfigEntry("MonetaryDecimalSymbol", ".", m_monetaryDecimalSymbol);
|
||
|
readConfigEntry("MonetaryThousandsSeparator", ",",
|
||
|
m_monetaryThousandsSeparator);
|
||
13 years ago
|
m_monetaryThousandsSeparator.replace(TQString::fromLatin1("$0"), TQString());
|
||
15 years ago
|
|
||
|
readConfigNumEntry("FracDigits", 2, m_fracDigits, int);
|
||
|
readConfigBoolEntry("PositivePrefixCurrencySymbol", true,
|
||
|
m_positivePrefixCurrencySymbol);
|
||
|
readConfigBoolEntry("NegativePrefixCurrencySymbol", true,
|
||
|
m_negativePrefixCurrencySymbol);
|
||
|
readConfigNumEntry("PositiveMonetarySignPosition", (int)BeforeQuantityMoney,
|
||
|
m_positiveMonetarySignPosition, SignPosition);
|
||
|
readConfigNumEntry("NegativeMonetarySignPosition", (int)ParensAround,
|
||
|
m_negativeMonetarySignPosition, SignPosition);
|
||
|
|
||
|
|
||
|
// Date and time
|
||
|
readConfigEntry("TimeFormat", "%H:%M:%S", m_timeFormat);
|
||
|
readConfigEntry("DateFormat", "%A %d %B %Y", m_dateFormat);
|
||
|
readConfigEntry("DateFormatShort", "%Y-%m-%d", m_dateFormatShort);
|
||
|
readConfigNumEntry("WeekStartDay", 1, d->weekStartDay, int);
|
||
|
|
||
|
// other
|
||
15 years ago
|
readConfigNumEntry("PageSize", (int)TQPrinter::A4, d->pageSize, int);
|
||
15 years ago
|
readConfigNumEntry("MeasureSystem", (int)Metric, d->measureSystem,
|
||
|
MeasureSystem);
|
||
|
readConfigEntry("CalendarSystem", "gregorian", d->calendarType);
|
||
|
delete d->calendar;
|
||
|
d->calendar = 0; // ### HPB Is this the correct place?
|
||
|
|
||
|
//Grammatical
|
||
|
//Precedence here is l10n / i18n / config file
|
||
|
KSimpleConfig language(locate("locale",
|
||
13 years ago
|
TQString::fromLatin1("%1/entry.desktop")
|
||
15 years ago
|
.arg(m_language)), true);
|
||
|
language.setGroup("KCM Locale");
|
||
|
#define read3ConfigBoolEntry(key, default, save) \
|
||
|
save = entry.readBoolEntry(key, default); \
|
||
|
save = language.readBoolEntry(key, save); \
|
||
|
save = config->readBoolEntry(key, save);
|
||
|
|
||
|
read3ConfigBoolEntry("NounDeclension", false, d->nounDeclension);
|
||
|
read3ConfigBoolEntry("DateMonthNamePossessive", false,
|
||
|
d->dateMonthNamePossessive);
|
||
|
|
||
|
// end of hack
|
||
12 years ago
|
TDEGlobal::_locale = lsave;
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
bool TDELocale::setCountry(const TQString & country)
|
||
15 years ago
|
{
|
||
|
// Check if the file exists too??
|
||
|
if ( country.isEmpty() )
|
||
|
return false;
|
||
|
|
||
|
m_country = country;
|
||
|
|
||
|
d->formatInited = false;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::catalogueFileName(const TQString & language,
|
||
15 years ago
|
const KCatalogue & catalog)
|
||
|
{
|
||
13 years ago
|
TQString path = TQString::fromLatin1("%1/LC_MESSAGES/%2.mo")
|
||
15 years ago
|
.arg( language )
|
||
|
.arg( catalog.name() );
|
||
|
|
||
14 years ago
|
TQString fileName = locate( "locale", path );
|
||
|
if (fileName.isEmpty())
|
||
|
fileName = locate( "locale-bundle", path );
|
||
|
|
||
|
return fileName;
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
bool TDELocale::setLanguage(const TQString & language)
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( d->languageList.contains( language ) ) {
|
||
15 years ago
|
d->languageList.remove( language );
|
||
|
}
|
||
|
d->languageList.prepend( language ); // let us consider this language to be the most important one
|
||
|
|
||
|
m_language = language; // remember main language for shortcut evaluation
|
||
|
|
||
|
// important when called from the outside and harmless when called before populating the
|
||
|
// catalog name list
|
||
|
updateCatalogues();
|
||
|
|
||
|
d->formatInited = false;
|
||
|
|
||
|
return true; // Maybe the mo-files for this language are empty, but in principle we can speak all languages
|
||
|
}
|
||
|
|
||
12 years ago
|
bool TDELocale::setLanguage(const TQStringList & languages)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQStringList languageList( languages );
|
||
15 years ago
|
// This list might contain
|
||
|
// 1) some empty strings that we have to eliminate
|
||
|
// 2) duplicate entries like in de:fr:de, where we have to keep the first occurrance of a language in order
|
||
|
// to preserve the order of precenence of the user => iterate backwards
|
||
13 years ago
|
// 3) languages into which the application is not translated. For those languages we should not even load tdelibs.mo or kio.po.
|
||
15 years ago
|
// these langugage have to be dropped. Otherwise we get strange side effects, e.g. with Hebrew:
|
||
|
// the right/left switch for languages that write from
|
||
13 years ago
|
// right to left (like Hebrew or Arabic) is set in tdelibs.mo. If you only have tdelibs.mo
|
||
14 years ago
|
// but nothing from appname.mo, you get a mostly English app with layout from right to left.
|
||
15 years ago
|
// That was considered to be a bug by the Hebrew translators.
|
||
15 years ago
|
for( TQStringList::Iterator it = languageList.fromLast();
|
||
15 years ago
|
it != languageList.begin(); --it )
|
||
|
{
|
||
|
// kdDebug() << "checking " << (*it) << endl;
|
||
|
bool bIsTranslated = isApplicationTranslatedInto( *it );
|
||
14 years ago
|
if ( languageList.contains(*it) > 1 || (*it).isEmpty() || (!bIsTranslated) ) {
|
||
15 years ago
|
// kdDebug() << "removing " << (*it) << endl;
|
||
|
it = languageList.remove( it );
|
||
|
}
|
||
|
}
|
||
|
// now this has left the first element of the list unchecked.
|
||
|
// The question why this is the case is left as an exercise for the reader...
|
||
|
// Besides the list might have been empty all the way, so check that too.
|
||
|
if ( languageList.begin() != languageList.end() ) {
|
||
15 years ago
|
TQStringList::Iterator it = languageList.begin(); // now pointing to the first element
|
||
15 years ago
|
// kdDebug() << "checking " << (*it) << endl;
|
||
|
if( (*it).isEmpty() || !(isApplicationTranslatedInto( *it )) ) {
|
||
|
// kdDebug() << "removing " << (*it) << endl;
|
||
|
languageList.remove( it ); // that's what the iterator was for...
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ( languageList.isEmpty() ) {
|
||
|
// user picked no language, so we assume he/she speaks English.
|
||
|
languageList.append( defaultLanguage() );
|
||
|
}
|
||
|
m_language = languageList.first(); // keep this for shortcut evaluations
|
||
|
|
||
|
d->languageList = languageList; // keep this new list of languages to use
|
||
|
d->langTwoAlpha.clear(); // Flush cache
|
||
|
|
||
|
// important when called from the outside and harmless when called before populating the
|
||
|
// catalog name list
|
||
|
updateCatalogues();
|
||
|
|
||
|
return true; // we found something. Maybe it's only English, but we found something
|
||
|
}
|
||
|
|
||
12 years ago
|
bool TDELocale::isApplicationTranslatedInto( const TQString & language)
|
||
15 years ago
|
{
|
||
|
if ( language.isEmpty() ) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if ( language == defaultLanguage() ) {
|
||
|
// en_us is always "installed"
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString appName = d->appName;
|
||
15 years ago
|
if (maincatalogue) {
|
||
13 years ago
|
appName = TQString::fromLatin1(maincatalogue);
|
||
15 years ago
|
}
|
||
|
// sorry, catalogueFileName requires catalog object,k which we do not have here
|
||
14 years ago
|
// path finding was supposed to be moved completely to KCatalogue. The interface cannot
|
||
15 years ago
|
// be changed that far during deep freeze. So in order to fix the bug now, we have
|
||
|
// duplicated code for file path evaluation. Cleanup will follow later. We could have e.g.
|
||
|
// a static method in KCataloge that can translate between these file names.
|
||
|
// a stat
|
||
13 years ago
|
TQString sFileName = TQString::fromLatin1("%1/LC_MESSAGES/%2.mo")
|
||
15 years ago
|
.arg( language )
|
||
|
.arg( appName );
|
||
|
// kdDebug() << "isApplicationTranslatedInto: filename " << sFileName << endl;
|
||
|
|
||
15 years ago
|
TQString sAbsFileName = locate( "locale", sFileName );
|
||
14 years ago
|
if (sAbsFileName.isEmpty())
|
||
|
sAbsFileName = locate( "locale-bundle", sFileName );
|
||
|
|
||
15 years ago
|
// kdDebug() << "isApplicationTranslatedInto: absname " << sAbsFileName << endl;
|
||
|
return ! sAbsFileName.isEmpty();
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::splitLocale(const TQString & aStr,
|
||
15 years ago
|
TQString & language,
|
||
|
TQString & country,
|
||
|
TQString & chrset)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString str = aStr;
|
||
15 years ago
|
|
||
|
// just in case, there is another language appended
|
||
14 years ago
|
int f = str.find(':');
|
||
15 years ago
|
if (f >= 0)
|
||
|
str.truncate(f);
|
||
|
|
||
15 years ago
|
country = TQString::null;
|
||
|
chrset = TQString::null;
|
||
|
language = TQString::null;
|
||
15 years ago
|
|
||
14 years ago
|
f = str.find('.');
|
||
15 years ago
|
if (f >= 0)
|
||
|
{
|
||
|
chrset = str.mid(f + 1);
|
||
|
str.truncate(f);
|
||
|
}
|
||
|
|
||
14 years ago
|
f = str.find('_');
|
||
15 years ago
|
if (f >= 0)
|
||
|
{
|
||
|
country = str.mid(f + 1);
|
||
|
str.truncate(f);
|
||
|
}
|
||
|
|
||
|
language = str;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::language() const
|
||
15 years ago
|
{
|
||
|
return m_language;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::country() const
|
||
15 years ago
|
{
|
||
|
return m_country;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::monthName(int i, bool shortName) const
|
||
15 years ago
|
{
|
||
|
if ( shortName )
|
||
|
switch ( i )
|
||
|
{
|
||
|
case 1: return translate("January", "Jan");
|
||
|
case 2: return translate("February", "Feb");
|
||
|
case 3: return translate("March", "Mar");
|
||
|
case 4: return translate("April", "Apr");
|
||
|
case 5: return translate("May short", "May");
|
||
|
case 6: return translate("June", "Jun");
|
||
|
case 7: return translate("July", "Jul");
|
||
|
case 8: return translate("August", "Aug");
|
||
|
case 9: return translate("September", "Sep");
|
||
|
case 10: return translate("October", "Oct");
|
||
|
case 11: return translate("November", "Nov");
|
||
|
case 12: return translate("December", "Dec");
|
||
|
}
|
||
|
else
|
||
|
switch (i)
|
||
|
{
|
||
|
case 1: return translate("January");
|
||
|
case 2: return translate("February");
|
||
|
case 3: return translate("March");
|
||
|
case 4: return translate("April");
|
||
|
case 5: return translate("May long", "May");
|
||
|
case 6: return translate("June");
|
||
|
case 7: return translate("July");
|
||
|
case 8: return translate("August");
|
||
|
case 9: return translate("September");
|
||
|
case 10: return translate("October");
|
||
|
case 11: return translate("November");
|
||
|
case 12: return translate("December");
|
||
|
}
|
||
|
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::monthNamePossessive(int i, bool shortName) const
|
||
15 years ago
|
{
|
||
|
if ( shortName )
|
||
|
switch ( i )
|
||
|
{
|
||
|
case 1: return translate("of January", "of Jan");
|
||
|
case 2: return translate("of February", "of Feb");
|
||
|
case 3: return translate("of March", "of Mar");
|
||
|
case 4: return translate("of April", "of Apr");
|
||
|
case 5: return translate("of May short", "of May");
|
||
|
case 6: return translate("of June", "of Jun");
|
||
|
case 7: return translate("of July", "of Jul");
|
||
|
case 8: return translate("of August", "of Aug");
|
||
|
case 9: return translate("of September", "of Sep");
|
||
|
case 10: return translate("of October", "of Oct");
|
||
|
case 11: return translate("of November", "of Nov");
|
||
|
case 12: return translate("of December", "of Dec");
|
||
|
}
|
||
|
else
|
||
|
switch (i)
|
||
|
{
|
||
|
case 1: return translate("of January");
|
||
|
case 2: return translate("of February");
|
||
|
case 3: return translate("of March");
|
||
|
case 4: return translate("of April");
|
||
|
case 5: return translate("of May long", "of May");
|
||
|
case 6: return translate("of June");
|
||
|
case 7: return translate("of July");
|
||
|
case 8: return translate("of August");
|
||
|
case 9: return translate("of September");
|
||
|
case 10: return translate("of October");
|
||
|
case 11: return translate("of November");
|
||
|
case 12: return translate("of December");
|
||
|
}
|
||
|
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::weekDayName (int i, bool shortName) const
|
||
15 years ago
|
{
|
||
|
return calendar()->weekDayName(i, shortName);
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::insertCatalogue( const TQString & catalog )
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( !d->catalogNames.contains( catalog) ) {
|
||
15 years ago
|
d->catalogNames.append( catalog );
|
||
|
}
|
||
|
updateCatalogues( ); // evaluate the changed list and generate the neccessary KCatalog objects
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::updateCatalogues( )
|
||
15 years ago
|
{
|
||
|
// some changes have occured. Maybe we have learned or forgotten some languages.
|
||
|
// Maybe the language precedence has changed.
|
||
|
// Maybe we have learned or forgotten some catalog names.
|
||
|
// Now examine the list of KCatalogue objects and change it according to the new circumstances.
|
||
|
|
||
|
// this could be optimized: try to reuse old KCatalog objects, but remember that the order of
|
||
|
// catalogs might have changed: e.g. in this fashion
|
||
|
// 1) move all catalogs into a temporary list
|
||
|
// 2) iterate over all languages and catalog names
|
||
|
// 3.1) pick the catalog from the saved list, if it already exists
|
||
|
// 3.2) else create a new catalog.
|
||
|
// but we will do this later.
|
||
|
|
||
15 years ago
|
for ( TQValueList<KCatalogue>::Iterator it = d->catalogues.begin();
|
||
15 years ago
|
it != d->catalogues.end(); )
|
||
|
{
|
||
|
it = d->catalogues.remove(it);
|
||
|
}
|
||
|
|
||
|
// now iterate over all languages and all wanted catalog names and append or create them in the right order
|
||
12 years ago
|
// the sequence must be e.g. nds/appname nds/tdelibs nds/tdeio de/appname de/tdelibs de/tdeio etc.
|
||
13 years ago
|
// and not nds/appname de/appname nds/tdelibs de/tdelibs etc. Otherwise we would be in trouble with a language
|
||
15 years ago
|
// sequende nds,en_US, de. In this case en_US must hide everything below in the language list.
|
||
15 years ago
|
for ( TQStringList::ConstIterator itLangs = d->languageList.begin();
|
||
15 years ago
|
itLangs != d->languageList.end(); ++itLangs)
|
||
|
{
|
||
15 years ago
|
for ( TQStringList::ConstIterator itNames = d->catalogNames.begin();
|
||
15 years ago
|
itNames != d->catalogNames.end(); ++itNames)
|
||
|
{
|
||
|
KCatalogue cat( *itNames, *itLangs ); // create Catalog for this name and this language
|
||
|
d->catalogues.append( cat );
|
||
|
}
|
||
|
}
|
||
|
initPluralTypes(); // evaluate the plural type for all languages and remember this in each KCatalogue
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
12 years ago
|
void TDELocale::removeCatalogue(const TQString &catalog)
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( d->catalogNames.contains( catalog )) {
|
||
15 years ago
|
d->catalogNames.remove( catalog );
|
||
12 years ago
|
if (TDEGlobal::_instance)
|
||
15 years ago
|
updateCatalogues(); // walk through the KCatalogue instances and weed out everything we no longer need
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setActiveCatalogue(const TQString &catalog)
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( d->catalogNames.contains( catalog ) ) {
|
||
15 years ago
|
d->catalogNames.remove( catalog );
|
||
|
d->catalogNames.prepend( catalog );
|
||
|
updateCatalogues(); // walk through the KCatalogue instances and adapt to the new order
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
TDELocale::~TDELocale()
|
||
15 years ago
|
{
|
||
|
delete d->calendar;
|
||
|
delete d->languages;
|
||
|
delete d;
|
||
|
d = 0L;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::translate_priv(const char *msgid,
|
||
15 years ago
|
const char *fallback,
|
||
|
const char **translated,
|
||
|
int* pluralType ) const
|
||
|
{
|
||
|
if ( pluralType) {
|
||
14 years ago
|
*pluralType = -1; // unless we find something more precise
|
||
15 years ago
|
}
|
||
|
if (!msgid || !msgid[0])
|
||
|
{
|
||
12 years ago
|
kdWarning() << "TDELocale: trying to look up \"\" in catalog. "
|
||
15 years ago
|
<< "Fix the program" << endl;
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
|
if ( useDefaultLanguage() ) { // shortcut evaluation if en_US is main language: do not consult the catalogs
|
||
15 years ago
|
return TQString::fromUtf8( fallback );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
for ( TQValueList<KCatalogue>::ConstIterator it = d->catalogues.begin();
|
||
15 years ago
|
it != d->catalogues.end();
|
||
|
++it )
|
||
|
{
|
||
|
// shortcut evaluation: once we have arrived at en_US (default language) we cannot consult
|
||
|
// the catalog as it will not have an assiciated mo-file. For this default language we can
|
||
|
// immediately pick the fallback string.
|
||
|
if ( (*it).language() == defaultLanguage() ) {
|
||
15 years ago
|
return TQString::fromUtf8( fallback );
|
||
15 years ago
|
}
|
||
|
|
||
|
const char * text = (*it).translate( msgid );
|
||
|
|
||
|
if ( text )
|
||
|
{
|
||
|
// we found it
|
||
|
if (translated) {
|
||
|
*translated = text;
|
||
|
}
|
||
|
if ( pluralType) {
|
||
|
*pluralType = (*it).pluralType(); // remember the plural type information from the catalog that was used
|
||
|
}
|
||
15 years ago
|
return TQString::fromUtf8( text );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Always use UTF-8 if the string was not found
|
||
15 years ago
|
return TQString::fromUtf8( fallback );
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::translate(const char* msgid) const
|
||
15 years ago
|
{
|
||
|
return translate_priv(msgid, msgid);
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::translate( const char *index, const char *fallback) const
|
||
15 years ago
|
{
|
||
|
if (!index || !index[0] || !fallback || !fallback[0])
|
||
|
{
|
||
12 years ago
|
kdDebug(173) << "TDELocale: trying to look up \"\" in catalog. "
|
||
15 years ago
|
<< "Fix the program" << endl;
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
|
if ( useDefaultLanguage() )
|
||
15 years ago
|
return TQString::fromUtf8( fallback );
|
||
15 years ago
|
|
||
|
char *newstring = new char[strlen(index) + strlen(fallback) + 5];
|
||
|
sprintf(newstring, "_: %s\n%s", index, fallback);
|
||
15 years ago
|
// as copying TQString is very fast, it looks slower as it is ;/
|
||
|
TQString r = translate_priv(newstring, fallback);
|
||
15 years ago
|
delete [] newstring;
|
||
|
|
||
|
return r;
|
||
|
}
|
||
|
|
||
15 years ago
|
static TQString put_n_in(const TQString &orig, unsigned long n)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString ret = orig;
|
||
14 years ago
|
int index = ret.find("%n");
|
||
15 years ago
|
if (index == -1)
|
||
|
return ret;
|
||
14 years ago
|
ret.replace(index, 2, TQString::number(n));
|
||
15 years ago
|
return ret;
|
||
|
}
|
||
|
|
||
|
#define EXPECT_LENGTH(x) \
|
||
|
if (forms.count() != x) { \
|
||
|
kdError() << "translation of \"" << singular << "\" doesn't contain " << x << " different plural forms as expected\n"; \
|
||
15 years ago
|
return TQString( "BROKEN TRANSLATION %1" ).arg( singular ); }
|
||
15 years ago
|
|
||
12 years ago
|
TQString TDELocale::translate( const char *singular, const char *plural,
|
||
15 years ago
|
unsigned long n ) const
|
||
|
{
|
||
|
if (!singular || !singular[0] || !plural || !plural[0])
|
||
|
{
|
||
12 years ago
|
kdWarning() << "TDELocale: trying to look up \"\" in catalog. "
|
||
15 years ago
|
<< "Fix the program" << endl;
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
|
char *newstring = new char[strlen(singular) + strlen(plural) + 6];
|
||
|
sprintf(newstring, "_n: %s\n%s", singular, plural);
|
||
15 years ago
|
// as copying TQString is very fast, it looks slower as it is ;/
|
||
15 years ago
|
int pluralType = -1;
|
||
15 years ago
|
TQString r = translate_priv(newstring, 0, 0, &pluralType);
|
||
15 years ago
|
delete [] newstring;
|
||
|
|
||
|
if ( r.isEmpty() || useDefaultLanguage() || pluralType == -1) {
|
||
|
if ( n == 1 ) {
|
||
15 years ago
|
return put_n_in( TQString::fromUtf8( singular ), n );
|
||
15 years ago
|
} else {
|
||
15 years ago
|
TQString tmp = TQString::fromUtf8( plural );
|
||
15 years ago
|
#ifndef NDEBUG
|
||
14 years ago
|
if (tmp.find("%n") == -1) {
|
||
15 years ago
|
kdDebug() << "the message for i18n should contain a '%n'! " << plural << endl;
|
||
|
}
|
||
|
#endif
|
||
|
return put_n_in( tmp, n );
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList forms = TQStringList::split( "\n", r, false );
|
||
15 years ago
|
switch ( pluralType ) {
|
||
|
case 0: // NoPlural
|
||
|
EXPECT_LENGTH( 1 );
|
||
|
return put_n_in( forms[0], n);
|
||
|
case 1: // TwoForms
|
||
|
EXPECT_LENGTH( 2 );
|
||
|
if ( n == 1 )
|
||
|
return put_n_in( forms[0], n);
|
||
|
else
|
||
|
return put_n_in( forms[1], n);
|
||
|
case 2: // French
|
||
|
EXPECT_LENGTH( 2 );
|
||
|
if ( n == 1 || n == 0 )
|
||
|
return put_n_in( forms[0], n);
|
||
|
else
|
||
|
return put_n_in( forms[1], n);
|
||
|
case 3: // OneTwoRest
|
||
|
EXPECT_LENGTH( 3 );
|
||
|
if ( n == 1 )
|
||
|
return put_n_in( forms[0], n);
|
||
|
else if ( n == 2 )
|
||
|
return put_n_in( forms[1], n);
|
||
|
else
|
||
|
return put_n_in( forms[2], n);
|
||
|
case 4: // Russian, corrected by mok
|
||
|
EXPECT_LENGTH( 3 );
|
||
|
if ( n%10 == 1 && n%100 != 11)
|
||
|
return put_n_in( forms[0], n); // odin fail
|
||
|
else if (( n%10 >= 2 && n%10 <=4 ) && (n%100<10 || n%100>20))
|
||
|
return put_n_in( forms[1], n); // dva faila
|
||
|
else
|
||
|
return put_n_in( forms[2], n); // desyat' failov
|
||
|
case 5: // Polish
|
||
|
EXPECT_LENGTH( 3 );
|
||
|
if ( n == 1 )
|
||
|
return put_n_in( forms[0], n);
|
||
|
else if ( n%10 >= 2 && n%10 <=4 && (n%100<10 || n%100>=20) )
|
||
|
return put_n_in( forms[1], n);
|
||
|
else
|
||
|
return put_n_in( forms[2], n);
|
||
|
case 6: // Slovenian
|
||
|
EXPECT_LENGTH( 4 );
|
||
|
if ( n%100 == 1 )
|
||
|
return put_n_in( forms[1], n); // ena datoteka
|
||
|
else if ( n%100 == 2 )
|
||
|
return put_n_in( forms[2], n); // dve datoteki
|
||
|
else if ( n%100 == 3 || n%100 == 4 )
|
||
|
return put_n_in( forms[3], n); // tri datoteke
|
||
|
else
|
||
|
return put_n_in( forms[0], n); // sto datotek
|
||
|
case 7: // Lithuanian
|
||
|
EXPECT_LENGTH( 3 );
|
||
|
if ( n%10 == 0 || (n%100>=11 && n%100<=19) )
|
||
|
return put_n_in( forms[2], n);
|
||
|
else if ( n%10 == 1 )
|
||
|
return put_n_in( forms[0], n);
|
||
|
else
|
||
|
return put_n_in( forms[1], n);
|
||
|
case 8: // Czech - use modern form which is equivalent to Slovak
|
||
|
case 9: // Slovak
|
||
|
EXPECT_LENGTH( 3 );
|
||
|
if ( n == 1 )
|
||
|
return put_n_in( forms[0], n);
|
||
|
else if (( n >= 2 ) && ( n <= 4 ))
|
||
|
return put_n_in( forms[1], n);
|
||
|
else
|
||
|
return put_n_in( forms[2], n);
|
||
|
case 10: // Maltese
|
||
|
EXPECT_LENGTH( 4 );
|
||
|
if ( n == 1 )
|
||
|
return put_n_in( forms[0], n );
|
||
|
else if ( ( n == 0 ) || ( n%100 > 0 && n%100 <= 10 ) )
|
||
|
return put_n_in( forms[1], n );
|
||
|
else if ( n%100 > 10 && n%100 < 20 )
|
||
|
return put_n_in( forms[2], n );
|
||
|
else
|
||
|
return put_n_in( forms[3], n );
|
||
|
case 11: // Arabic
|
||
|
EXPECT_LENGTH( 4 );
|
||
|
if (n == 1)
|
||
|
return put_n_in(forms[0], n);
|
||
|
else if (n == 2)
|
||
|
return put_n_in(forms[1], n);
|
||
|
else if ( n < 11)
|
||
|
return put_n_in(forms[2], n);
|
||
|
else
|
||
|
return put_n_in(forms[3], n);
|
||
|
case 12: // Balcan
|
||
|
EXPECT_LENGTH( 3 );
|
||
|
if (n != 11 && n % 10 == 1)
|
||
|
return put_n_in(forms[0], n);
|
||
|
else if (n / 10 != 1 && n % 10 >= 2 && n % 10 <= 4)
|
||
|
return put_n_in(forms[1], n);
|
||
|
else
|
||
|
return put_n_in(forms[2], n);
|
||
|
case 13: // Macedonian
|
||
|
EXPECT_LENGTH(3);
|
||
|
if (n % 10 == 1)
|
||
|
return put_n_in(forms[0], n);
|
||
|
else if (n % 10 == 2)
|
||
|
return put_n_in(forms[1], n);
|
||
|
else
|
||
|
return put_n_in(forms[2], n);
|
||
|
case 14: // Gaeilge
|
||
|
EXPECT_LENGTH(5);
|
||
|
if (n == 1) // "ceann amhain"
|
||
|
return put_n_in(forms[0], n);
|
||
|
else if (n == 2) // "dha cheann"
|
||
|
return put_n_in(forms[1], n);
|
||
|
else if (n < 7) // "%n cinn"
|
||
|
return put_n_in(forms[2], n);
|
||
|
else if (n < 11) // "%n gcinn"
|
||
|
return put_n_in(forms[3], n);
|
||
|
else // "%n ceann"
|
||
|
return put_n_in(forms[4], n);
|
||
|
}
|
||
|
kdFatal() << "The function should have been returned in another way\n";
|
||
|
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::translateQt( const char *context, const char *source,
|
||
15 years ago
|
const char *message) const
|
||
|
{
|
||
|
if (!source || !source[0]) {
|
||
12 years ago
|
kdWarning() << "TDELocale: trying to look up \"\" in catalog. "
|
||
15 years ago
|
<< "Fix the program" << endl;
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
|
if ( useDefaultLanguage() ) {
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
|
char *newstring = 0;
|
||
|
const char *translation = 0;
|
||
15 years ago
|
TQString r;
|
||
15 years ago
|
|
||
|
if ( message && message[0]) {
|
||
|
char *newstring = new char[strlen(source) + strlen(message) + 5];
|
||
|
sprintf(newstring, "_: %s\n%s", source, message);
|
||
|
const char *translation = 0;
|
||
15 years ago
|
// as copying TQString is very fast, it looks slower as it is ;/
|
||
15 years ago
|
r = translate_priv(newstring, source, &translation);
|
||
|
delete [] newstring;
|
||
|
if (translation)
|
||
|
return r;
|
||
|
}
|
||
|
|
||
|
if ( context && context[0] && message && message[0]) {
|
||
|
newstring = new char[strlen(context) + strlen(message) + 5];
|
||
|
sprintf(newstring, "_: %s\n%s", context, message);
|
||
15 years ago
|
// as copying TQString is very fast, it looks slower as it is ;/
|
||
15 years ago
|
r = translate_priv(newstring, source, &translation);
|
||
|
delete [] newstring;
|
||
|
if (translation)
|
||
|
return r;
|
||
|
}
|
||
|
|
||
|
r = translate_priv(source, source, &translation);
|
||
|
if (translation)
|
||
|
return r;
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
bool TDELocale::nounDeclension() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return d->nounDeclension;
|
||
|
}
|
||
|
|
||
12 years ago
|
bool TDELocale::dateMonthNamePossessive() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return d->dateMonthNamePossessive;
|
||
|
}
|
||
|
|
||
12 years ago
|
int TDELocale::weekStartDay() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return d->weekStartDay;
|
||
|
}
|
||
|
|
||
12 years ago
|
bool TDELocale::weekStartsMonday() const //deprecated
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return (d->weekStartDay==1);
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::decimalSymbol() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_decimalSymbol;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::thousandsSeparator() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_thousandsSeparator;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::currencySymbol() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_currencySymbol;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::monetaryDecimalSymbol() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_monetaryDecimalSymbol;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::monetaryThousandsSeparator() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_monetaryThousandsSeparator;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::positiveSign() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_positiveSign;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::negativeSign() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_negativeSign;
|
||
|
}
|
||
|
|
||
12 years ago
|
int TDELocale::fracDigits() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_fracDigits;
|
||
|
}
|
||
|
|
||
12 years ago
|
bool TDELocale::positivePrefixCurrencySymbol() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_positivePrefixCurrencySymbol;
|
||
|
}
|
||
|
|
||
12 years ago
|
bool TDELocale::negativePrefixCurrencySymbol() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_negativePrefixCurrencySymbol;
|
||
|
}
|
||
|
|
||
12 years ago
|
TDELocale::SignPosition TDELocale::positiveMonetarySignPosition() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_positiveMonetarySignPosition;
|
||
|
}
|
||
|
|
||
12 years ago
|
TDELocale::SignPosition TDELocale::negativeMonetarySignPosition() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_negativeMonetarySignPosition;
|
||
|
}
|
||
|
|
||
15 years ago
|
static inline void put_it_in( TQChar *buffer, uint& index, const TQString &s )
|
||
15 years ago
|
{
|
||
|
for ( uint l = 0; l < s.length(); l++ )
|
||
13 years ago
|
buffer[index++] = s.at( l );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
static inline void put_it_in( TQChar *buffer, uint& index, int number )
|
||
15 years ago
|
{
|
||
|
buffer[index++] = number / 10 + '0';
|
||
|
buffer[index++] = number % 10 + '0';
|
||
|
}
|
||
|
|
||
|
// insert (thousands)-"separator"s into the non-fractional part of str
|
||
15 years ago
|
static void _insertSeparator(TQString &str, const TQString &separator,
|
||
|
const TQString &decimalSymbol)
|
||
15 years ago
|
{
|
||
|
// leave fractional part untouched
|
||
15 years ago
|
TQString mainPart = str.section(decimalSymbol, 0, 0);
|
||
|
TQString fracPart = str.section(decimalSymbol, 1, 1,
|
||
|
TQString::SectionIncludeLeadingSep);
|
||
15 years ago
|
|
||
|
for (int pos = mainPart.length() - 3; pos > 0; pos -= 3)
|
||
|
mainPart.insert(pos, separator);
|
||
|
|
||
|
str = mainPart + fracPart;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::formatMoney(double num,
|
||
15 years ago
|
const TQString & symbol,
|
||
15 years ago
|
int precision) const
|
||
|
{
|
||
|
// some defaults
|
||
15 years ago
|
TQString currency = symbol.isNull()
|
||
15 years ago
|
? currencySymbol()
|
||
|
: symbol;
|
||
|
if (precision < 0) precision = fracDigits();
|
||
|
|
||
|
// the number itself
|
||
|
bool neg = num < 0;
|
||
15 years ago
|
TQString res = TQString::number(neg?-num:num, 'f', precision);
|
||
15 years ago
|
|
||
|
// Replace dot with locale decimal separator
|
||
14 years ago
|
res.replace(TQChar('.'), monetaryDecimalSymbol());
|
||
15 years ago
|
|
||
|
// Insert the thousand separators
|
||
|
_insertSeparator(res, monetaryThousandsSeparator(), monetaryDecimalSymbol());
|
||
|
|
||
|
// set some variables we need later
|
||
|
int signpos = neg
|
||
|
? negativeMonetarySignPosition()
|
||
|
: positiveMonetarySignPosition();
|
||
15 years ago
|
TQString sign = neg
|
||
15 years ago
|
? negativeSign()
|
||
|
: positiveSign();
|
||
|
|
||
|
switch (signpos)
|
||
|
{
|
||
|
case ParensAround:
|
||
|
res.prepend('(');
|
||
|
res.append (')');
|
||
|
break;
|
||
|
case BeforeQuantityMoney:
|
||
|
res.prepend(sign);
|
||
|
break;
|
||
|
case AfterQuantityMoney:
|
||
|
res.append(sign);
|
||
|
break;
|
||
|
case BeforeMoney:
|
||
|
currency.prepend(sign);
|
||
|
break;
|
||
|
case AfterMoney:
|
||
|
currency.append(sign);
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (neg?negativePrefixCurrencySymbol():
|
||
|
positivePrefixCurrencySymbol())
|
||
|
{
|
||
|
res.prepend(' ');
|
||
|
res.prepend(currency);
|
||
|
} else {
|
||
|
res.append (' ');
|
||
|
res.append (currency);
|
||
|
}
|
||
|
|
||
|
return res;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::formatMoney(const TQString &numStr) const
|
||
15 years ago
|
{
|
||
|
return formatMoney(numStr.toDouble());
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::formatNumber(double num, int precision) const
|
||
15 years ago
|
{
|
||
|
if (precision == -1) precision = 2;
|
||
15 years ago
|
// no need to round since TQString::number does this for us
|
||
|
return formatNumber(TQString::number(num, 'f', precision), false, 0);
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::formatLong(long num) const
|
||
15 years ago
|
{
|
||
|
return formatNumber((double)num, 0);
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::formatNumber(const TQString &numStr) const
|
||
15 years ago
|
{
|
||
|
return formatNumber(numStr, true, 2);
|
||
|
}
|
||
|
|
||
|
// increase the digit at 'position' by one
|
||
15 years ago
|
static void _inc_by_one(TQString &str, int position)
|
||
15 years ago
|
{
|
||
|
for (int i = position; i >= 0; i--)
|
||
|
{
|
||
|
char last_char = str[i].latin1();
|
||
|
switch(last_char)
|
||
|
{
|
||
|
case '0':
|
||
15 years ago
|
str[i] = (QChar)'1';
|
||
15 years ago
|
break;
|
||
|
case '1':
|
||
15 years ago
|
str[i] = (QChar)'2';
|
||
15 years ago
|
break;
|
||
|
case '2':
|
||
15 years ago
|
str[i] = (QChar)'3';
|
||
15 years ago
|
break;
|
||
|
case '3':
|
||
15 years ago
|
str[i] = (QChar)'4';
|
||
15 years ago
|
break;
|
||
|
case '4':
|
||
15 years ago
|
str[i] = (QChar)'5';
|
||
15 years ago
|
break;
|
||
|
case '5':
|
||
15 years ago
|
str[i] = (QChar)'6';
|
||
15 years ago
|
break;
|
||
|
case '6':
|
||
15 years ago
|
str[i] = (QChar)'7';
|
||
15 years ago
|
break;
|
||
|
case '7':
|
||
15 years ago
|
str[i] = (QChar)'8';
|
||
15 years ago
|
break;
|
||
|
case '8':
|
||
15 years ago
|
str[i] = (QChar)'9';
|
||
15 years ago
|
break;
|
||
|
case '9':
|
||
15 years ago
|
str[i] = (QChar)'0';
|
||
15 years ago
|
if (i == 0) str.prepend('1');
|
||
|
continue;
|
||
|
case '.':
|
||
|
continue;
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Cut off if more digits in fractional part than 'precision'
|
||
15 years ago
|
static void _round(TQString &str, int precision)
|
||
15 years ago
|
{
|
||
14 years ago
|
int decimalSymbolPos = str.find('.');
|
||
15 years ago
|
|
||
|
if (decimalSymbolPos == -1)
|
||
|
if (precision == 0) return;
|
||
|
else if (precision > 0) // add dot if missing (and needed)
|
||
|
{
|
||
|
str.append('.');
|
||
|
decimalSymbolPos = str.length() - 1;
|
||
|
}
|
||
|
|
||
|
// fill up with more than enough zeroes (in case fractional part too short)
|
||
15 years ago
|
str.append(TQString().fill('0', precision));
|
||
15 years ago
|
|
||
|
// Now decide whether to round up or down
|
||
|
char last_char = str[decimalSymbolPos + precision + 1].latin1();
|
||
|
switch (last_char)
|
||
|
{
|
||
|
case '0':
|
||
|
case '1':
|
||
|
case '2':
|
||
|
case '3':
|
||
|
case '4':
|
||
|
// nothing to do, rounding down
|
||
|
break;
|
||
|
case '5':
|
||
|
case '6':
|
||
|
case '7':
|
||
|
case '8':
|
||
|
case '9':
|
||
|
_inc_by_one(str, decimalSymbolPos + precision);
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
|
||
14 years ago
|
decimalSymbolPos = str.find('.');
|
||
15 years ago
|
str.truncate(decimalSymbolPos + precision + 1);
|
||
|
|
||
|
// if precision == 0 delete also '.'
|
||
|
if (precision == 0) str = str.section('.', 0, 0);
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::formatNumber(const TQString &numStr, bool round,
|
||
15 years ago
|
int precision) const
|
||
|
{
|
||
15 years ago
|
TQString tmpString = numStr;
|
||
15 years ago
|
if ((round && precision < 0) ||
|
||
15 years ago
|
! TQRegExp("^[+-]?\\d+(\\.\\d+)*(e[+-]?\\d+)?$").exactMatch(tmpString))
|
||
15 years ago
|
return numStr;
|
||
|
|
||
|
|
||
|
// Skip the sign (for now)
|
||
15 years ago
|
bool neg = (tmpString[0] == (QChar)'-');
|
||
|
if (neg || tmpString[0] == (QChar)'+') tmpString.remove(0, 1);
|
||
15 years ago
|
|
||
|
// Split off exponential part (including 'e'-symbol)
|
||
15 years ago
|
TQString mantString = tmpString.section('e', 0, 0,
|
||
|
TQString::SectionCaseInsensitiveSeps);
|
||
|
TQString expString = tmpString.section('e', 1, 1,
|
||
|
TQString::SectionCaseInsensitiveSeps |
|
||
|
TQString::SectionIncludeLeadingSep);
|
||
15 years ago
|
|
||
|
if (round) _round(mantString, precision);
|
||
|
|
||
|
// Replace dot with locale decimal separator
|
||
14 years ago
|
mantString.replace(TQChar('.'), decimalSymbol());
|
||
15 years ago
|
|
||
|
// Insert the thousand separators
|
||
|
_insertSeparator(mantString, thousandsSeparator(), decimalSymbol());
|
||
|
|
||
|
// How can we know where we should put the sign?
|
||
|
mantString.prepend(neg?negativeSign():positiveSign());
|
||
|
|
||
|
return mantString + expString;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::formatDate(const TQDate &pDate, bool shortFormat) const
|
||
15 years ago
|
{
|
||
15 years ago
|
const TQString rst = shortFormat?dateFormatShort():dateFormat();
|
||
15 years ago
|
|
||
15 years ago
|
TQString buffer;
|
||
15 years ago
|
|
||
|
if ( ! pDate.isValid() ) return buffer;
|
||
|
|
||
|
bool escape = false;
|
||
|
|
||
|
int year = calendar()->year(pDate);
|
||
|
int month = calendar()->month(pDate);
|
||
|
|
||
|
for ( uint format_index = 0; format_index < rst.length(); ++format_index )
|
||
|
{
|
||
|
if ( !escape )
|
||
|
{
|
||
13 years ago
|
if ( (TQChar(rst.at( format_index )).unicode()) == '%' )
|
||
15 years ago
|
escape = true;
|
||
|
else
|
||
13 years ago
|
buffer.append(rst.at(format_index));
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
13 years ago
|
switch ( TQChar(rst.at( format_index )).unicode() )
|
||
15 years ago
|
{
|
||
|
case '%':
|
||
|
buffer.append('%');
|
||
|
break;
|
||
|
case 'Y':
|
||
|
buffer.append(calendar()->yearString(pDate, false));
|
||
|
break;
|
||
|
case 'y':
|
||
|
buffer.append(calendar()->yearString(pDate, true));
|
||
|
break;
|
||
|
case 'n':
|
||
|
buffer.append(calendar()->monthString(pDate, true));
|
||
|
break;
|
||
|
case 'e':
|
||
|
buffer.append(calendar()->dayString(pDate, true));
|
||
|
break;
|
||
|
case 'm':
|
||
|
buffer.append(calendar()->monthString(pDate, false));
|
||
|
break;
|
||
|
case 'b':
|
||
|
if (d->nounDeclension && d->dateMonthNamePossessive)
|
||
|
buffer.append(calendar()->monthNamePossessive(month, year, true));
|
||
|
else
|
||
|
buffer.append(calendar()->monthName(month, year, true));
|
||
|
break;
|
||
|
case 'B':
|
||
|
if (d->nounDeclension && d->dateMonthNamePossessive)
|
||
|
buffer.append(calendar()->monthNamePossessive(month, year, false));
|
||
|
else
|
||
|
buffer.append(calendar()->monthName(month, year, false));
|
||
|
break;
|
||
|
case 'd':
|
||
|
buffer.append(calendar()->dayString(pDate, false));
|
||
|
break;
|
||
|
case 'a':
|
||
|
buffer.append(calendar()->weekDayName(pDate, true));
|
||
|
break;
|
||
|
case 'A':
|
||
|
buffer.append(calendar()->weekDayName(pDate, false));
|
||
|
break;
|
||
|
default:
|
||
13 years ago
|
buffer.append(rst.at(format_index));
|
||
15 years ago
|
break;
|
||
|
}
|
||
|
escape = false;
|
||
|
}
|
||
|
}
|
||
|
return buffer;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setMainCatalogue(const char *catalog)
|
||
15 years ago
|
{
|
||
|
maincatalogue = catalog;
|
||
|
}
|
||
|
|
||
12 years ago
|
double TDELocale::readNumber(const TQString &_str, bool * ok) const
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString str = _str.stripWhiteSpace();
|
||
14 years ago
|
bool neg = str.find(negativeSign()) == 0;
|
||
15 years ago
|
if (neg)
|
||
|
str.remove( 0, negativeSign().length() );
|
||
|
|
||
|
/* will hold the scientific notation portion of the number.
|
||
|
Example, with 2.34E+23, exponentialPart == "E+23"
|
||
|
*/
|
||
15 years ago
|
TQString exponentialPart;
|
||
15 years ago
|
int EPos;
|
||
|
|
||
14 years ago
|
EPos = str.find('E', 0, false);
|
||
15 years ago
|
|
||
|
if (EPos != -1)
|
||
|
{
|
||
|
exponentialPart = str.mid(EPos);
|
||
|
str = str.left(EPos);
|
||
|
}
|
||
|
|
||
14 years ago
|
int pos = str.find(decimalSymbol());
|
||
15 years ago
|
TQString major;
|
||
|
TQString minor;
|
||
15 years ago
|
if ( pos == -1 )
|
||
|
major = str;
|
||
|
else
|
||
|
{
|
||
|
major = str.left(pos);
|
||
|
minor = str.mid(pos + decimalSymbol().length());
|
||
|
}
|
||
|
|
||
|
// Remove thousand separators
|
||
|
int thlen = thousandsSeparator().length();
|
||
|
int lastpos = 0;
|
||
14 years ago
|
while ( ( pos = major.find( thousandsSeparator() ) ) > 0 )
|
||
15 years ago
|
{
|
||
|
// e.g. 12,,345,,678,,922 Acceptable positions (from the end) are 5, 10, 15... i.e. (3+thlen)*N
|
||
|
int fromEnd = major.length() - pos;
|
||
|
if ( fromEnd % (3+thlen) != 0 // Needs to be a multiple, otherwise it's an error
|
||
|
|| pos - lastpos > 3 // More than 3 digits between two separators -> error
|
||
|
|| pos == 0 // Can't start with a separator
|
||
|
|| (lastpos>0 && pos-lastpos!=3)) // Must have exactly 3 digits between two separators
|
||
|
{
|
||
|
if (ok) *ok = false;
|
||
|
return 0.0;
|
||
|
}
|
||
|
|
||
|
lastpos = pos;
|
||
|
major.remove( pos, thlen );
|
||
|
}
|
||
|
if (lastpos>0 && major.length()-lastpos!=3) // Must have exactly 3 digits after the last separator
|
||
|
{
|
||
|
if (ok) *ok = false;
|
||
|
return 0.0;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString tot;
|
||
15 years ago
|
if (neg) tot = (QChar)'-';
|
||
15 years ago
|
|
||
|
tot += major + '.' + minor + exponentialPart;
|
||
|
|
||
|
return tot.toDouble(ok);
|
||
|
}
|
||
|
|
||
12 years ago
|
double TDELocale::readMoney(const TQString &_str, bool * ok) const
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString str = _str.stripWhiteSpace();
|
||
15 years ago
|
bool neg = false;
|
||
|
bool currencyFound = false;
|
||
15 years ago
|
TQString symbol = currencySymbol();
|
||
15 years ago
|
// First try removing currency symbol from either end
|
||
14 years ago
|
int pos = str.find(symbol);
|
||
15 years ago
|
if ( pos == 0 || pos == (int) str.length()-symbol.length() )
|
||
|
{
|
||
|
str.remove(pos,symbol.length());
|
||
|
str = str.stripWhiteSpace();
|
||
|
currencyFound = true;
|
||
|
}
|
||
|
if (str.isEmpty())
|
||
|
{
|
||
|
if (ok) *ok = false;
|
||
|
return 0;
|
||
|
}
|
||
|
// Then try removing negative sign from either end
|
||
|
// (with a special case for parenthesis)
|
||
|
if (negativeMonetarySignPosition() == ParensAround)
|
||
|
{
|
||
15 years ago
|
if (str[0] == (QChar)'(' && str[str.length()-1] == (QChar)')')
|
||
15 years ago
|
{
|
||
|
neg = true;
|
||
|
str.remove(str.length()-1,1);
|
||
|
str.remove(0,1);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
14 years ago
|
int i1 = str.find(negativeSign());
|
||
15 years ago
|
if ( i1 == 0 || i1 == (int) str.length()-1 )
|
||
|
{
|
||
|
neg = true;
|
||
|
str.remove(i1,negativeSign().length());
|
||
|
}
|
||
|
}
|
||
|
if (neg) str = str.stripWhiteSpace();
|
||
|
|
||
14 years ago
|
// Finally try again for the currency symbol, if we didn't find
|
||
15 years ago
|
// it already (because of the negative sign being in the way).
|
||
|
if ( !currencyFound )
|
||
|
{
|
||
14 years ago
|
pos = str.find(symbol);
|
||
15 years ago
|
if ( pos == 0 || pos == (int) str.length()-symbol.length() )
|
||
|
{
|
||
|
str.remove(pos,symbol.length());
|
||
|
str = str.stripWhiteSpace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// And parse the rest as a number
|
||
14 years ago
|
pos = str.find(monetaryDecimalSymbol());
|
||
15 years ago
|
TQString major;
|
||
|
TQString minior;
|
||
15 years ago
|
if (pos == -1)
|
||
|
major = str;
|
||
|
else
|
||
|
{
|
||
|
major = str.left(pos);
|
||
|
minior = str.mid(pos + monetaryDecimalSymbol().length());
|
||
|
}
|
||
|
|
||
|
// Remove thousand separators
|
||
|
int thlen = monetaryThousandsSeparator().length();
|
||
|
int lastpos = 0;
|
||
14 years ago
|
while ( ( pos = major.find( monetaryThousandsSeparator() ) ) > 0 )
|
||
15 years ago
|
{
|
||
|
// e.g. 12,,345,,678,,922 Acceptable positions (from the end) are 5, 10, 15... i.e. (3+thlen)*N
|
||
|
int fromEnd = major.length() - pos;
|
||
|
if ( fromEnd % (3+thlen) != 0 // Needs to be a multiple, otherwise it's an error
|
||
|
|| pos - lastpos > 3 // More than 3 digits between two separators -> error
|
||
|
|| pos == 0 // Can't start with a separator
|
||
|
|| (lastpos>0 && pos-lastpos!=3)) // Must have exactly 3 digits between two separators
|
||
|
{
|
||
|
if (ok) *ok = false;
|
||
|
return 0.0;
|
||
|
}
|
||
|
lastpos = pos;
|
||
|
major.remove( pos, thlen );
|
||
|
}
|
||
|
if (lastpos>0 && major.length()-lastpos!=3) // Must have exactly 3 digits after the last separator
|
||
|
{
|
||
|
if (ok) *ok = false;
|
||
|
return 0.0;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString tot;
|
||
15 years ago
|
if (neg) tot = (QChar)'-';
|
||
15 years ago
|
tot += major + '.' + minior;
|
||
|
return tot.toDouble(ok);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* helper function to read integers
|
||
|
* @param str
|
||
|
* @param pos the position to start at. It will be updated when we parse it.
|
||
|
* @return the integer read in the string, or -1 if no string
|
||
|
*/
|
||
15 years ago
|
static int readInt(const TQString &str, uint &pos)
|
||
15 years ago
|
{
|
||
13 years ago
|
if (!str.at(pos).isDigit()) return -1;
|
||
15 years ago
|
int result = 0;
|
||
13 years ago
|
for (; str.length() > pos && str.at(pos).isDigit(); pos++)
|
||
15 years ago
|
{
|
||
|
result *= 10;
|
||
13 years ago
|
result += str.at(pos).digitValue();
|
||
15 years ago
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQDate TDELocale::readDate(const TQString &intstr, bool* ok) const
|
||
15 years ago
|
{
|
||
15 years ago
|
TQDate date;
|
||
15 years ago
|
date = readDate(intstr, ShortFormat, ok);
|
||
|
if (date.isValid()) return date;
|
||
|
return readDate(intstr, NormalFormat, ok);
|
||
|
}
|
||
|
|
||
12 years ago
|
TQDate TDELocale::readDate(const TQString &intstr, ReadDateFlags flags, bool* ok) const
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString fmt = ((flags & ShortFormat) ? dateFormatShort() : dateFormat()).simplifyWhiteSpace();
|
||
15 years ago
|
return readDate( intstr, fmt, ok );
|
||
|
}
|
||
|
|
||
12 years ago
|
TQDate TDELocale::readDate(const TQString &intstr, const TQString &fmt, bool* ok) const
|
||
15 years ago
|
{
|
||
12 years ago
|
//kdDebug() << "TDELocale::readDate intstr=" << intstr << " fmt=" << fmt << endl;
|
||
15 years ago
|
TQString str = intstr.simplifyWhiteSpace().lower();
|
||
15 years ago
|
int day = -1, month = -1;
|
||
|
// allow the year to be omitted if not in the format
|
||
14 years ago
|
int year = calendar()->year(TQDate::currentDate());
|
||
15 years ago
|
uint strpos = 0;
|
||
|
uint fmtpos = 0;
|
||
|
|
||
|
int iLength; // Temporary variable used when reading input
|
||
|
|
||
|
bool error = false;
|
||
|
|
||
|
while (fmt.length() > fmtpos && str.length() > strpos && !error)
|
||
|
{
|
||
|
|
||
13 years ago
|
TQChar c = fmt.at(fmtpos++);
|
||
15 years ago
|
|
||
15 years ago
|
if (c != (QChar)'%') {
|
||
13 years ago
|
if (c.isSpace() && str.at(strpos).isSpace())
|
||
15 years ago
|
strpos++;
|
||
13 years ago
|
else if (c != str.at(strpos++))
|
||
15 years ago
|
error = true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
int j;
|
||
|
// remove space at the beginning
|
||
13 years ago
|
if (str.length() > strpos && str.at(strpos).isSpace())
|
||
15 years ago
|
strpos++;
|
||
|
|
||
13 years ago
|
c = fmt.at(fmtpos++);
|
||
15 years ago
|
switch (c)
|
||
|
{
|
||
|
case 'a':
|
||
|
case 'A':
|
||
|
|
||
|
error = true;
|
||
|
j = 1;
|
||
|
while (error && (j < 8)) {
|
||
15 years ago
|
TQString s = calendar()->weekDayName(j, c == (QChar)'a').lower();
|
||
15 years ago
|
int len = s.length();
|
||
|
if (str.mid(strpos, len) == s)
|
||
|
{
|
||
|
strpos += len;
|
||
|
error = false;
|
||
|
}
|
||
|
j++;
|
||
|
}
|
||
|
break;
|
||
|
case 'b':
|
||
|
case 'B':
|
||
|
|
||
|
error = true;
|
||
|
if (d->nounDeclension && d->dateMonthNamePossessive) {
|
||
|
j = 1;
|
||
|
while (error && (j < 13)) {
|
||
15 years ago
|
TQString s = calendar()->monthNamePossessive(j, year, c == (QChar)'b').lower();
|
||
15 years ago
|
int len = s.length();
|
||
|
if (str.mid(strpos, len) == s) {
|
||
|
month = j;
|
||
|
strpos += len;
|
||
|
error = false;
|
||
|
}
|
||
|
j++;
|
||
|
}
|
||
|
}
|
||
|
j = 1;
|
||
|
while (error && (j < 13)) {
|
||
15 years ago
|
TQString s = calendar()->monthName(j, year, c == (QChar)'b').lower();
|
||
15 years ago
|
int len = s.length();
|
||
|
if (str.mid(strpos, len) == s) {
|
||
|
month = j;
|
||
|
strpos += len;
|
||
|
error = false;
|
||
|
}
|
||
|
j++;
|
||
|
}
|
||
|
break;
|
||
|
case 'd':
|
||
|
case 'e':
|
||
|
day = calendar()->dayStringToInteger(str.mid(strpos), iLength);
|
||
|
strpos += iLength;
|
||
|
|
||
|
error = iLength <= 0;
|
||
|
break;
|
||
|
|
||
|
case 'n':
|
||
|
case 'm':
|
||
|
month = calendar()->monthStringToInteger(str.mid(strpos), iLength);
|
||
|
strpos += iLength;
|
||
|
|
||
|
error = iLength <= 0;
|
||
|
break;
|
||
|
|
||
|
case 'Y':
|
||
|
case 'y':
|
||
|
year = calendar()->yearStringToInteger(str.mid(strpos), iLength);
|
||
|
strpos += iLength;
|
||
|
|
||
|
error = iLength <= 0;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* for a match, we should reach the end of both strings, not just one of
|
||
|
them */
|
||
|
if ( fmt.length() > fmtpos || str.length() > strpos )
|
||
|
{
|
||
|
error = true;
|
||
|
}
|
||
|
|
||
12 years ago
|
//kdDebug(173) << "TDELocale::readDate day=" << day << " month=" << month << " year=" << year << endl;
|
||
15 years ago
|
if ( year != -1 && month != -1 && day != -1 && !error)
|
||
|
{
|
||
|
if (ok) *ok = true;
|
||
|
|
||
15 years ago
|
TQDate result;
|
||
15 years ago
|
calendar()->setYMD(result, year, month, day);
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (ok) *ok = false;
|
||
15 years ago
|
return TQDate(); // invalid date
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
TQTime TDELocale::readTime(const TQString &intstr, bool *ok) const
|
||
15 years ago
|
{
|
||
15 years ago
|
TQTime _time;
|
||
15 years ago
|
_time = readTime(intstr, WithSeconds, ok);
|
||
|
if (_time.isValid()) return _time;
|
||
|
return readTime(intstr, WithoutSeconds, ok);
|
||
|
}
|
||
|
|
||
12 years ago
|
TQTime TDELocale::readTime(const TQString &intstr, ReadTimeFlags flags, bool *ok) const
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString str = intstr.simplifyWhiteSpace().lower();
|
||
|
TQString Format = timeFormat().simplifyWhiteSpace();
|
||
15 years ago
|
if (flags & WithoutSeconds)
|
||
15 years ago
|
Format.remove(TQRegExp(".%S"));
|
||
15 years ago
|
|
||
|
int hour = -1, minute = -1;
|
||
|
int second = ( (flags & WithoutSeconds) == 0 ) ? -1 : 0; // don't require seconds
|
||
|
bool g_12h = false;
|
||
|
bool pm = false;
|
||
|
uint strpos = 0;
|
||
|
uint Formatpos = 0;
|
||
|
|
||
|
while (Format.length() > Formatpos || str.length() > strpos)
|
||
|
{
|
||
|
if ( !(Format.length() > Formatpos && str.length() > strpos) ) goto error;
|
||
|
|
||
13 years ago
|
TQChar c = Format.at(Formatpos++);
|
||
15 years ago
|
|
||
15 years ago
|
if (c != (QChar)'%')
|
||
15 years ago
|
{
|
||
|
if (c.isSpace())
|
||
|
strpos++;
|
||
13 years ago
|
else if (c != str.at(strpos++))
|
||
15 years ago
|
goto error;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// remove space at the beginning
|
||
13 years ago
|
if (str.length() > strpos && str.at(strpos).isSpace())
|
||
15 years ago
|
strpos++;
|
||
|
|
||
13 years ago
|
c = Format.at(Formatpos++);
|
||
15 years ago
|
switch (c)
|
||
|
{
|
||
|
case 'p':
|
||
|
{
|
||
15 years ago
|
TQString s;
|
||
15 years ago
|
s = translate("pm").lower();
|
||
|
int len = s.length();
|
||
|
if (str.mid(strpos, len) == s)
|
||
|
{
|
||
|
pm = true;
|
||
|
strpos += len;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
s = translate("am").lower();
|
||
|
len = s.length();
|
||
|
if (str.mid(strpos, len) == s) {
|
||
|
pm = false;
|
||
|
strpos += len;
|
||
|
}
|
||
|
else
|
||
|
goto error;
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'k':
|
||
|
case 'H':
|
||
|
g_12h = false;
|
||
|
hour = readInt(str, strpos);
|
||
|
if (hour < 0 || hour > 23)
|
||
|
goto error;
|
||
|
|
||
|
break;
|
||
|
|
||
|
case 'l':
|
||
|
case 'I':
|
||
|
g_12h = true;
|
||
|
hour = readInt(str, strpos);
|
||
|
if (hour < 1 || hour > 12)
|
||
|
goto error;
|
||
|
|
||
|
break;
|
||
|
|
||
|
case 'M':
|
||
|
minute = readInt(str, strpos);
|
||
|
if (minute < 0 || minute > 59)
|
||
|
goto error;
|
||
|
|
||
|
break;
|
||
|
|
||
|
case 'S':
|
||
|
second = readInt(str, strpos);
|
||
|
if (second < 0 || second > 59)
|
||
|
goto error;
|
||
|
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if (g_12h) {
|
||
|
hour %= 12;
|
||
|
if (pm) hour += 12;
|
||
|
}
|
||
|
|
||
|
if (ok) *ok = true;
|
||
15 years ago
|
return TQTime(hour, minute, second);
|
||
15 years ago
|
|
||
|
error:
|
||
|
if (ok) *ok = false;
|
||
|
// ######## KDE4: remove this
|
||
15 years ago
|
return TQTime(-1, -1, -1); // return invalid date if it didn't work
|
||
15 years ago
|
}
|
||
|
|
||
|
//BIC: merge with below
|
||
12 years ago
|
TQString TDELocale::formatTime(const TQTime &pTime, bool includeSecs) const
|
||
15 years ago
|
{
|
||
|
return formatTime( pTime, includeSecs, false );
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::formatTime(const TQTime &pTime, bool includeSecs, bool isDuration) const
|
||
15 years ago
|
{
|
||
15 years ago
|
const TQString rst = timeFormat();
|
||
15 years ago
|
|
||
|
// only "pm/am" here can grow, the rest shrinks, but
|
||
|
// I'm rather safe than sorry
|
||
14 years ago
|
TQChar *buffer = new TQChar[rst.length() * 3 / 2 + 30];
|
||
15 years ago
|
|
||
|
uint index = 0;
|
||
|
bool escape = false;
|
||
|
int number = 0;
|
||
|
|
||
|
for ( uint format_index = 0; format_index < rst.length(); format_index++ )
|
||
|
{
|
||
|
if ( !escape )
|
||
|
{
|
||
13 years ago
|
if ( (TQChar(rst.at( format_index )).unicode()) == '%' )
|
||
15 years ago
|
escape = true;
|
||
|
else
|
||
13 years ago
|
buffer[index++] = rst.at( format_index );
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
13 years ago
|
switch ( TQChar(rst.at( format_index )).unicode() )
|
||
15 years ago
|
{
|
||
|
case '%':
|
||
15 years ago
|
buffer[index++] = (QChar)'%';
|
||
15 years ago
|
break;
|
||
|
case 'H':
|
||
|
put_it_in( buffer, index, pTime.hour() );
|
||
|
break;
|
||
|
case 'I':
|
||
|
if ( isDuration )
|
||
|
put_it_in( buffer, index, pTime.hour() );
|
||
|
else
|
||
|
put_it_in( buffer, index, ( pTime.hour() + 11) % 12 + 1 );
|
||
|
break;
|
||
|
case 'M':
|
||
|
put_it_in( buffer, index, pTime.minute() );
|
||
|
break;
|
||
|
case 'S':
|
||
|
if (includeSecs)
|
||
|
put_it_in( buffer, index, pTime.second() );
|
||
|
else if ( index > 0 )
|
||
|
{
|
||
|
// we remove the separator sign before the seconds and
|
||
|
// assume that works everywhere
|
||
|
--index;
|
||
|
break;
|
||
|
}
|
||
|
break;
|
||
|
case 'k':
|
||
|
number = pTime.hour();
|
||
|
case 'l':
|
||
|
// to share the code
|
||
13 years ago
|
if ( (TQChar(rst.at( format_index )).unicode()) == 'l' )
|
||
15 years ago
|
number = isDuration ? pTime.hour() : (pTime.hour() + 11) % 12 + 1;
|
||
|
if ( number / 10 )
|
||
|
buffer[index++] = number / 10 + '0';
|
||
|
buffer[index++] = number % 10 + '0';
|
||
|
break;
|
||
|
case 'p':
|
||
|
if ( !isDuration )
|
||
|
{
|
||
15 years ago
|
TQString s;
|
||
15 years ago
|
if ( pTime.hour() >= 12 )
|
||
|
put_it_in( buffer, index, translate("pm") );
|
||
|
else
|
||
|
put_it_in( buffer, index, translate("am") );
|
||
|
}
|
||
|
break;
|
||
|
default:
|
||
13 years ago
|
buffer[index++] = rst.at( format_index );
|
||
15 years ago
|
break;
|
||
|
}
|
||
|
escape = false;
|
||
|
}
|
||
|
}
|
||
15 years ago
|
TQString ret( buffer, index );
|
||
15 years ago
|
delete [] buffer;
|
||
|
if ( isDuration ) // eliminate trailing-space due to " %p"
|
||
|
return ret.stripWhiteSpace();
|
||
|
else
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
12 years ago
|
bool TDELocale::use12Clock() const
|
||
15 years ago
|
{
|
||
13 years ago
|
if ((timeFormat().contains(TQString::fromLatin1("%I")) > 0) ||
|
||
|
(timeFormat().contains(TQString::fromLatin1("%l")) > 0))
|
||
15 years ago
|
return true;
|
||
|
else
|
||
|
return false;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::languages() const
|
||
15 years ago
|
{
|
||
13 years ago
|
return d->languageList.join( TQString::fromLatin1(":") );
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
TQStringList TDELocale::languageList() const
|
||
15 years ago
|
{
|
||
|
return d->languageList;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::formatDateTime(const TQDateTime &pDateTime,
|
||
15 years ago
|
bool shortFormat,
|
||
|
bool includeSeconds) const
|
||
|
{
|
||
|
return translate("concatenation of dates and time", "%1 %2")
|
||
14 years ago
|
.arg( formatDate( TQT_TQDATE_OBJECT(pDateTime.date()), shortFormat ) )
|
||
|
.arg( formatTime( TQT_TQTIME_OBJECT(pDateTime.time()), includeSeconds ) );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQString i18n(const char* text)
|
||
15 years ago
|
{
|
||
12 years ago
|
register TDELocale *instance = TDEGlobal::locale();
|
||
15 years ago
|
if (instance)
|
||
|
return instance->translate(text);
|
||
15 years ago
|
return TQString::fromUtf8(text);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQString i18n(const char* index, const char *text)
|
||
15 years ago
|
{
|
||
12 years ago
|
register TDELocale *instance = TDEGlobal::locale();
|
||
15 years ago
|
if (instance)
|
||
|
return instance->translate(index, text);
|
||
15 years ago
|
return TQString::fromUtf8(text);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQString i18n(const char* singular, const char* plural, unsigned long n)
|
||
15 years ago
|
{
|
||
12 years ago
|
register TDELocale *instance = TDEGlobal::locale();
|
||
15 years ago
|
if (instance)
|
||
|
return instance->translate(singular, plural, n);
|
||
|
if (n == 1)
|
||
15 years ago
|
return put_n_in(TQString::fromUtf8(singular), n);
|
||
15 years ago
|
else
|
||
15 years ago
|
return put_n_in(TQString::fromUtf8(plural), n);
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::initInstance()
|
||
15 years ago
|
{
|
||
12 years ago
|
if (TDEGlobal::_locale)
|
||
15 years ago
|
return;
|
||
|
|
||
12 years ago
|
TDEInstance *app = TDEGlobal::instance();
|
||
15 years ago
|
if (app) {
|
||
12 years ago
|
TDEGlobal::_locale = new TDELocale(TQString::fromLatin1(app->instanceName()));
|
||
15 years ago
|
|
||
|
// only do this for the global instance
|
||
12 years ago
|
TQTextCodec::setCodecForLocale(TDEGlobal::_locale->codecForEncoding());
|
||
15 years ago
|
}
|
||
|
else
|
||
12 years ago
|
kdDebug(173) << "no app name available using TDELocale - nothing to do\n";
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::langLookup(const TQString &fname, const char *rtype)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQStringList search;
|
||
15 years ago
|
|
||
|
// assemble the local search paths
|
||
12 years ago
|
const TQStringList localDoc = TDEGlobal::dirs()->resourceDirs(rtype);
|
||
15 years ago
|
|
||
|
// look up the different languages
|
||
|
for (int id=localDoc.count()-1; id >= 0; --id)
|
||
|
{
|
||
12 years ago
|
TQStringList langs = TDEGlobal::locale()->languageList();
|
||
15 years ago
|
langs.append( "en" );
|
||
|
langs.remove( defaultLanguage() );
|
||
15 years ago
|
TQStringList::ConstIterator lang;
|
||
15 years ago
|
for (lang = langs.begin(); lang != langs.end(); ++lang)
|
||
15 years ago
|
search.append(TQString("%1%2/%3").arg(localDoc[id]).arg(*lang).arg(fname));
|
||
15 years ago
|
}
|
||
|
|
||
|
// try to locate the file
|
||
15 years ago
|
TQStringList::Iterator it;
|
||
15 years ago
|
for (it = search.begin(); it != search.end(); ++it)
|
||
|
{
|
||
|
kdDebug(173) << "Looking for help in: " << *it << endl;
|
||
|
|
||
15 years ago
|
TQFileInfo info(*it);
|
||
15 years ago
|
if (info.exists() && info.isFile() && info.isReadable())
|
||
|
return *it;
|
||
|
}
|
||
|
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
bool TDELocale::useDefaultLanguage() const
|
||
15 years ago
|
{
|
||
|
return language() == defaultLanguage();
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::initEncoding(TDEConfig *)
|
||
15 years ago
|
{
|
||
|
const int mibDefault = 4; // ISO 8859-1
|
||
|
|
||
|
// This all made more sense when we still had the EncodingEnum config key.
|
||
15 years ago
|
setEncoding( TQTextCodec::codecForLocale()->mibEnum() );
|
||
15 years ago
|
|
||
|
if ( !d->codecForEncoding )
|
||
|
{
|
||
|
kdWarning(173) << " Defaulting to ISO 8859-1 encoding." << endl;
|
||
|
setEncoding(mibDefault);
|
||
|
}
|
||
|
|
||
|
Q_ASSERT( d->codecForEncoding );
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::initFileNameEncoding(TDEConfig *)
|
||
15 years ago
|
{
|
||
|
// If the following environment variable is set, assume all filenames
|
||
|
// are in UTF-8 regardless of the current C locale.
|
||
13 years ago
|
d->utf8FileEncoding = getenv("TDE_UTF8_FILENAMES") != 0;
|
||
15 years ago
|
if (d->utf8FileEncoding)
|
||
|
{
|
||
12 years ago
|
TQFile::setEncodingFunction(TDELocale::encodeFileNameUTF8);
|
||
|
TQFile::setDecodingFunction(TDELocale::decodeFileNameUTF8);
|
||
15 years ago
|
}
|
||
|
// Otherwise, stay with QFile's default filename encoding functions
|
||
|
// which, on Unix platforms, use the locale's codec.
|
||
|
}
|
||
|
|
||
15 years ago
|
#ifdef USE_QT3
|
||
12 years ago
|
TQCString TDELocale::encodeFileNameUTF8( const TQString & fileName )
|
||
15 years ago
|
#endif // USE_QT3
|
||
|
#ifdef USE_QT4
|
||
12 years ago
|
QByteArray TDELocale::encodeFileNameUTF8( const QString & fileName )
|
||
15 years ago
|
#endif // USE_QT4
|
||
15 years ago
|
{
|
||
14 years ago
|
return TQString(fileName).utf8();
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
#ifdef USE_QT3
|
||
12 years ago
|
TQString TDELocale::decodeFileNameUTF8( const TQCString & localFileName )
|
||
15 years ago
|
#endif // USE_QT3
|
||
|
#ifdef USE_QT4
|
||
12 years ago
|
QString TDELocale::decodeFileNameUTF8( const QByteArray & localFileName )
|
||
15 years ago
|
#endif // USE_QT4
|
||
15 years ago
|
{
|
||
15 years ago
|
return TQString::fromUtf8(localFileName);
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setDateFormat(const TQString & format)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
m_dateFormat = format.stripWhiteSpace();
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setDateFormatShort(const TQString & format)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
m_dateFormatShort = format.stripWhiteSpace();
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setDateMonthNamePossessive(bool possessive)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
d->dateMonthNamePossessive = possessive;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setTimeFormat(const TQString & format)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
m_timeFormat = format.stripWhiteSpace();
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setWeekStartsMonday(bool start) //deprecated
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
if (start)
|
||
|
d->weekStartDay = 1;
|
||
|
else
|
||
|
d->weekStartDay = 7;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setWeekStartDay(int day)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
if (day>7 || day<1)
|
||
|
d->weekStartDay = 1; //Monday is default
|
||
|
else
|
||
|
d->weekStartDay = day;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::dateFormat() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_dateFormat;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::dateFormatShort() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_dateFormatShort;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::timeFormat() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return m_timeFormat;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setDecimalSymbol(const TQString & symbol)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
m_decimalSymbol = symbol.stripWhiteSpace();
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setThousandsSeparator(const TQString & separator)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
// allow spaces here
|
||
|
m_thousandsSeparator = separator;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setPositiveSign(const TQString & sign)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
m_positiveSign = sign.stripWhiteSpace();
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setNegativeSign(const TQString & sign)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
m_negativeSign = sign.stripWhiteSpace();
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setPositiveMonetarySignPosition(SignPosition signpos)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
m_positiveMonetarySignPosition = signpos;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setNegativeMonetarySignPosition(SignPosition signpos)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
m_negativeMonetarySignPosition = signpos;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setPositivePrefixCurrencySymbol(bool prefix)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
m_positivePrefixCurrencySymbol = prefix;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setNegativePrefixCurrencySymbol(bool prefix)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
m_negativePrefixCurrencySymbol = prefix;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setFracDigits(int digits)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
m_fracDigits = digits;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setMonetaryThousandsSeparator(const TQString & separator)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
// allow spaces here
|
||
|
m_monetaryThousandsSeparator = separator;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setMonetaryDecimalSymbol(const TQString & symbol)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
m_monetaryDecimalSymbol = symbol.stripWhiteSpace();
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setCurrencySymbol(const TQString & symbol)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
m_currencySymbol = symbol.stripWhiteSpace();
|
||
|
}
|
||
|
|
||
12 years ago
|
int TDELocale::pageSize() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return d->pageSize;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setPageSize(int pageSize)
|
||
15 years ago
|
{
|
||
|
// #### check if it's in range??
|
||
|
doFormatInit();
|
||
|
d->pageSize = pageSize;
|
||
|
}
|
||
|
|
||
12 years ago
|
TDELocale::MeasureSystem TDELocale::measureSystem() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
return d->measureSystem;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setMeasureSystem(MeasureSystem value)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
d->measureSystem = value;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::defaultLanguage()
|
||
15 years ago
|
{
|
||
13 years ago
|
return TQString::fromLatin1("en_US");
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::defaultCountry()
|
||
15 years ago
|
{
|
||
13 years ago
|
return TQString::fromLatin1("C");
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
const char * TDELocale::encoding() const
|
||
15 years ago
|
{
|
||
|
#ifdef Q_WS_WIN
|
||
|
if (0==qstrcmp("System", codecForEncoding()->name()))
|
||
|
{
|
||
|
//win32 returns "System" codec name here but KDE apps expect a real name:
|
||
|
strcpy(d->win32SystemEncoding, "cp ");
|
||
|
if (GetLocaleInfoA( MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), SORT_DEFAULT),
|
||
|
LOCALE_IDEFAULTANSICODEPAGE, d->win32SystemEncoding+3, sizeof(d->win32SystemEncoding)-3-1 ))
|
||
|
{
|
||
|
return d->win32SystemEncoding;
|
||
|
}
|
||
|
}
|
||
|
#endif
|
||
|
return codecForEncoding()->name();
|
||
|
}
|
||
|
|
||
12 years ago
|
int TDELocale::encodingMib() const
|
||
15 years ago
|
{
|
||
|
return codecForEncoding()->mibEnum();
|
||
|
}
|
||
|
|
||
12 years ago
|
int TDELocale::fileEncodingMib() const
|
||
15 years ago
|
{
|
||
|
if (d->utf8FileEncoding)
|
||
|
return 106;
|
||
|
return codecForEncoding()->mibEnum();
|
||
|
}
|
||
|
|
||
12 years ago
|
TQTextCodec * TDELocale::codecForEncoding() const
|
||
15 years ago
|
{
|
||
|
return d->codecForEncoding;
|
||
|
}
|
||
|
|
||
12 years ago
|
bool TDELocale::setEncoding(int mibEnum)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQTextCodec * codec = TQTextCodec::codecForMib(mibEnum);
|
||
15 years ago
|
if (codec)
|
||
|
d->codecForEncoding = codec;
|
||
|
|
||
|
return codec != 0;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQStringList TDELocale::languagesTwoAlpha() const
|
||
15 years ago
|
{
|
||
|
if (d->langTwoAlpha.count())
|
||
|
return d->langTwoAlpha;
|
||
|
|
||
15 years ago
|
const TQStringList &origList = languageList();
|
||
15 years ago
|
|
||
15 years ago
|
TQStringList result;
|
||
15 years ago
|
|
||
12 years ago
|
TDEConfig config(TQString::fromLatin1("language.codes"), true, false);
|
||
15 years ago
|
config.setGroup("TwoLetterCodes");
|
||
|
|
||
15 years ago
|
for ( TQStringList::ConstIterator it = origList.begin();
|
||
15 years ago
|
it != origList.end();
|
||
|
++it )
|
||
|
{
|
||
15 years ago
|
TQString lang = *it;
|
||
|
TQStringList langLst;
|
||
15 years ago
|
if (config.hasKey( lang ))
|
||
|
langLst = config.readListEntry( lang );
|
||
|
else
|
||
|
{
|
||
14 years ago
|
int i = lang.find('_');
|
||
15 years ago
|
if (i >= 0)
|
||
|
lang.truncate(i);
|
||
|
langLst << lang;
|
||
|
}
|
||
|
|
||
15 years ago
|
for ( TQStringList::ConstIterator langIt = langLst.begin();
|
||
15 years ago
|
langIt != langLst.end();
|
||
|
++langIt )
|
||
|
{
|
||
14 years ago
|
if ( !(*langIt).isEmpty() && !result.contains( *langIt ) )
|
||
15 years ago
|
result += *langIt;
|
||
|
}
|
||
|
}
|
||
|
d->langTwoAlpha = result;
|
||
|
return result;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQStringList TDELocale::allLanguagesTwoAlpha() const
|
||
15 years ago
|
{
|
||
|
if (!d->languages)
|
||
12 years ago
|
d->languages = new TDEConfig("all_languages", true, false, "locale");
|
||
15 years ago
|
|
||
|
return d->languages->groupList();
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::twoAlphaToLanguageName(const TQString &code) const
|
||
15 years ago
|
{
|
||
|
if (!d->languages)
|
||
12 years ago
|
d->languages = new TDEConfig("all_languages", true, false, "locale");
|
||
15 years ago
|
|
||
15 years ago
|
TQString groupName = code;
|
||
14 years ago
|
const int i = groupName.find('_');
|
||
|
groupName.replace(0, i, groupName.left(i).lower());
|
||
15 years ago
|
|
||
|
d->languages->setGroup(groupName);
|
||
|
return d->languages->readEntry("Name");
|
||
|
}
|
||
|
|
||
12 years ago
|
TQStringList TDELocale::allCountriesTwoAlpha() const
|
||
15 years ago
|
{
|
||
15 years ago
|
TQStringList countries;
|
||
12 years ago
|
TQStringList paths = TDEGlobal::dirs()->findAllResources("locale", "l10n/*/entry.desktop");
|
||
15 years ago
|
for(TQStringList::ConstIterator it = paths.begin();
|
||
15 years ago
|
it != paths.end(); ++it)
|
||
|
{
|
||
15 years ago
|
TQString code = (*it).mid((*it).length()-16, 2);
|
||
15 years ago
|
if (code != "/C")
|
||
|
countries.append(code);
|
||
|
}
|
||
|
return countries;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::twoAlphaToCountryName(const TQString &code) const
|
||
15 years ago
|
{
|
||
12 years ago
|
TDEConfig cfg("l10n/"+code.lower()+"/entry.desktop", true, false, "locale");
|
||
15 years ago
|
cfg.setGroup("KCM Locale");
|
||
|
return cfg.readEntry("Name");
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDELocale::setCalendar(const TQString & calType)
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
|
||
|
d->calendarType = calType;
|
||
|
|
||
|
delete d->calendar;
|
||
|
d->calendar = 0;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDELocale::calendarType() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
|
||
|
return d->calendarType;
|
||
|
}
|
||
|
|
||
12 years ago
|
const KCalendarSystem * TDELocale::calendar() const
|
||
15 years ago
|
{
|
||
|
doFormatInit();
|
||
|
|
||
|
// Check if it's the correct calendar?!?
|
||
|
if ( !d->calendar )
|
||
|
d->calendar = KCalendarSystemFactory::create( d->calendarType, this );
|
||
|
|
||
|
return d->calendar;
|
||
|
}
|
||
|
|
||
12 years ago
|
TDELocale::TDELocale(const TDELocale & rhs)
|
||
15 years ago
|
{
|
||
12 years ago
|
d = new TDELocalePrivate;
|
||
15 years ago
|
|
||
|
*this = rhs;
|
||
|
}
|
||
|
|
||
12 years ago
|
TDELocale & TDELocale::operator=(const TDELocale & rhs)
|
||
15 years ago
|
{
|
||
|
// Numbers and money
|
||
|
m_decimalSymbol = rhs.m_decimalSymbol;
|
||
|
m_thousandsSeparator = rhs.m_thousandsSeparator;
|
||
|
m_currencySymbol = rhs.m_currencySymbol;
|
||
|
m_monetaryDecimalSymbol = rhs.m_monetaryDecimalSymbol;
|
||
|
m_monetaryThousandsSeparator = rhs.m_monetaryThousandsSeparator;
|
||
|
m_positiveSign = rhs.m_positiveSign;
|
||
|
m_negativeSign = rhs.m_negativeSign;
|
||
|
m_fracDigits = rhs.m_fracDigits;
|
||
|
m_positivePrefixCurrencySymbol = rhs.m_positivePrefixCurrencySymbol;
|
||
|
m_negativePrefixCurrencySymbol = rhs.m_negativePrefixCurrencySymbol;
|
||
|
m_positiveMonetarySignPosition = rhs.m_positiveMonetarySignPosition;
|
||
|
m_negativeMonetarySignPosition = rhs.m_negativeMonetarySignPosition;
|
||
|
|
||
|
// Date and time
|
||
|
m_timeFormat = rhs.m_timeFormat;
|
||
|
m_dateFormat = rhs.m_dateFormat;
|
||
|
m_dateFormatShort = rhs.m_dateFormatShort;
|
||
|
|
||
|
m_language = rhs.m_language;
|
||
|
m_country = rhs.m_country;
|
||
|
|
||
|
// the assignment operator works here
|
||
|
*d = *rhs.d;
|
||
|
d->languages = 0; // Don't copy languages
|
||
|
d->calendar = 0; // Don't copy the calendar
|
||
|
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
12 years ago
|
bool TDELocale::setCharset(const TQString & ) { return true; }
|
||
|
TQString TDELocale::charset() const { return TQString::fromLatin1("UTF-8"); }
|
||
15 years ago
|
|
||
|
// KDE4: remove
|
||
|
#if 0
|
||
|
void nothing() { i18n("&Next"); }
|
||
|
#endif
|