27 #include <tqtextcodec.h>
29 #include <tqprinter.h>
30 #include <tqdatetime.h>
31 #include <tqfileinfo.h>
34 #include "kcatalogue.h"
35 #include "tdeglobal.h"
36 #include "kstandarddirs.h"
37 #include "ksimpleconfig.h"
38 #include "kinstance.h"
39 #include "tdeconfig.h"
41 #include "kcalendarsystem.h"
42 #include "kcalendarsystemfactory.h"
49 static const char *
const SYSTEM_MESSAGES =
"tdelibs";
51 static const char *maincatalogue = 0;
53 class TDELocalePrivate
58 bool dateMonthNamePossessive;
59 TQStringList languageList;
60 TQStringList catalogNames;
61 TQValueList<KCatalogue> catalogues;
63 TQTextCodec * codecForEncoding;
68 TQStringList langTwoAlpha;
71 TQString calendarType;
73 bool utf8FileEncoding;
76 char win32SystemEncoding[3+7];
78 bool useMainCatalogue;
85 d =
new TDELocalePrivate;
89 d->formatInited =
false;
92 initFileNameEncoding(0);
100 d->appName = catalog;
101 initLanguageList( cfg, config == 0);
102 initMainCatalogues(catalog);
110 this_klocale->initLanguageList((
TDEConfig *) config,
true);
114 return TQString::null;
117 void TDELocale::initMainCatalogues(
const TQString & catalog)
120 TQString mainCatalogue = catalog;
123 if (mainCatalogue.contains(
"desktop") == 0 || mainCatalogue.contains(
"kdesktop") == 1) {
125 mainCatalogue = TQString::fromLatin1(maincatalogue);
129 if (mainCatalogue.isEmpty()) {
130 kdDebug(173) <<
"TDELocale instance created called without valid "
131 <<
"catalog! Give an argument or call setMainCatalogue "
132 <<
"before init" <<
endl;
136 d->catalogNames.append( mainCatalogue );
137 if (mainCatalogue.contains(
"desktop") == 0 || mainCatalogue.contains(
"kdesktop") == 1) {
138 d->catalogNames.append( SYSTEM_MESSAGES );
139 d->catalogNames.append(
"tdeio" );
140 d->catalogNames.append(
"xdg-user-dirs" );
146 void TDELocale::initLanguageList(
TDEConfig * config,
bool useEnv)
150 m_country = config->
readEntry(
"Country" );
151 if ( m_country.isEmpty() )
158 (
':', TQFile::decodeName( ::getenv(
"TDE_LANG") ));
168 langs << TQFile::decodeName( ::getenv(
"LC_ALL") );
169 langs << TQFile::decodeName( ::getenv(
"LC_MESSAGES") );
170 langs << TQFile::decodeName( ::getenv(
"LANG") );
172 for ( TQStringList::Iterator it = langs.begin();
176 TQString ln, ct, chrset;
180 langs.insert(it, ln +
'_' + ct);
181 if (!chrset.isEmpty())
182 langs.insert(it, ln +
'_' + ct +
'.' + chrset);
185 langs.insert(it, ln);
195 void TDELocale::initPluralTypes()
197 for ( TQValueList<KCatalogue>::Iterator it = d->catalogues.begin();
198 it != d->catalogues.end();
201 TQString
language = (*it).language();
203 (*it).setPluralType( pt );
208 int TDELocale::pluralType(
const TQString & language )
210 for ( TQValueList<KCatalogue>::ConstIterator it = d->catalogues.begin();
211 it != d->catalogues.end();
214 if ( ((*it).name() == SYSTEM_MESSAGES ) && ((*it).language() ==
language )) {
215 return pluralType( *it );
222 int TDELocale::pluralType(
const KCatalogue& catalog )
224 const char* pluralFormString =
225 I18N_NOOP(
"_: Dear translator, please do not translate this string "
226 "in any form, but pick the _right_ value out of "
227 "NoPlural/TwoForms/French... If not sure what to do mail "
228 "thd@kde.org and coolo@kde.org, they will tell you. "
229 "Better leave that out if unsure, the programs will "
230 "crash!!\nDefinition of PluralForm - to be set by the "
231 "translator of tdelibs.po");
232 TQString pf (catalog.
translate( pluralFormString));
233 if ( pf.isEmpty() ) {
236 else if ( pf ==
"NoPlural" )
238 else if ( pf ==
"TwoForms" )
240 else if ( pf ==
"French" )
242 else if ( pf ==
"OneTwoRest" )
244 else if ( pf ==
"Russian" )
246 else if ( pf ==
"Polish" )
248 else if ( pf ==
"Slovenian" )
250 else if ( pf ==
"Lithuanian" )
252 else if ( pf ==
"Czech" )
254 else if ( pf ==
"Slovak" )
256 else if ( pf ==
"Maltese" )
258 else if ( pf ==
"Arabic" )
260 else if ( pf ==
"Balcan" )
262 else if ( pf ==
"Macedonian" )
264 else if ( pf ==
"Gaeilge" )
267 kdWarning(173) <<
"Definition of PluralForm is none of "
282 <<
"Maltese: " << pf <<
endl;
287 void TDELocale::doFormatInit()
const
289 if ( d->formatInited )
return;
294 d->formatInited =
true;
297 void TDELocale::initFormat()
303 kdDebug(173) <<
"TDELocale::initFormat" <<
endl;
309 TDEGlobal::_locale =
this;
314 TQString::fromLatin1(
"l10n/%1/entry.desktop")
315 .arg(m_country)),
true);
316 entry.setGroup(
"KCM Locale");
319 #define readConfigEntry(key, default, save) \
320 save = entry.readEntry(key, TQString::fromLatin1(default)); \
321 save = config->readEntry(key, save);
323 #define readConfigNumEntry(key, default, save, type) \
324 save = (type)entry.readNumEntry(key, default); \
325 save = (type)config->readNumEntry(key, save);
327 #define readConfigBoolEntry(key, default, save) \
328 save = entry.readBoolEntry(key, default); \
329 save = config->readBoolEntry(key, save);
331 readConfigEntry(
"DecimalSymbol",
".", m_decimalSymbol);
332 readConfigEntry(
"ThousandsSeparator",
",", m_thousandsSeparator);
333 m_thousandsSeparator.replace( TQString::fromLatin1(
"$0"), TQString() );
336 readConfigEntry(
"PositiveSign",
"", m_positiveSign);
337 readConfigEntry(
"NegativeSign",
"-", m_negativeSign);
340 readConfigEntry(
"CurrencySymbol",
"$", m_currencySymbol);
341 readConfigEntry(
"MonetaryDecimalSymbol",
".", m_monetaryDecimalSymbol);
342 readConfigEntry(
"MonetaryThousandsSeparator",
",",
343 m_monetaryThousandsSeparator);
344 m_monetaryThousandsSeparator.replace(TQString::fromLatin1(
"$0"), TQString());
346 readConfigNumEntry(
"FracDigits", 2, m_fracDigits,
int);
347 readConfigBoolEntry(
"PositivePrefixCurrencySymbol",
true,
348 m_positivePrefixCurrencySymbol);
349 readConfigBoolEntry(
"NegativePrefixCurrencySymbol",
true,
350 m_negativePrefixCurrencySymbol);
351 readConfigNumEntry(
"PositiveMonetarySignPosition", (
int)BeforeQuantityMoney,
353 readConfigNumEntry(
"NegativeMonetarySignPosition", (
int)ParensAround,
358 readConfigEntry(
"TimeFormat",
"%H:%M:%S", m_timeFormat);
359 readConfigEntry(
"DateFormat",
"%A %d %B %Y", m_dateFormat);
360 readConfigEntry(
"DateFormatShort",
"%Y-%m-%d", m_dateFormatShort);
361 readConfigNumEntry(
"WeekStartDay", 1, d->weekStartDay,
int);
364 readConfigNumEntry(
"PageSize", (
int)TQPrinter::A4, d->pageSize,
int);
365 readConfigNumEntry(
"MeasureSystem", (
int)Metric, d->measureSystem,
367 readConfigEntry(
"CalendarSystem",
"gregorian", d->calendarType);
374 TQString::fromLatin1(
"%1/entry.desktop")
375 .arg(m_language)),
true);
377 #define read3ConfigBoolEntry(key, default, save) \
378 save = entry.readBoolEntry(key, default); \
379 save = language.readBoolEntry(key, save); \
380 save = config->readBoolEntry(key, save);
382 read3ConfigBoolEntry(
"NounDeclension",
false, d->nounDeclension);
383 read3ConfigBoolEntry(
"DateMonthNamePossessive",
false,
384 d->dateMonthNamePossessive);
387 TDEGlobal::_locale = lsave;
398 d->formatInited =
false;
403 TQString TDELocale::catalogueFileName(
const TQString & language,
406 TQString path = TQString::fromLatin1(
"%1/LC_MESSAGES/%2.mo")
408 .arg( catalog.
name() );
410 TQString fileName = locate(
"locale", path );
411 if (fileName.isEmpty())
412 fileName = locate(
"locale-bundle", path );
419 if ( d->languageList.contains(
language ) ) {
422 d->languageList.prepend(
language );
430 d->formatInited =
false;
448 for( TQStringList::Iterator it =
languageList.fromLast();
452 bool bIsTranslated = isApplicationTranslatedInto( *it );
453 if (
languageList.contains(*it) > 1 || (*it).isEmpty() || (!bIsTranslated) ) {
464 if( (*it).isEmpty() || !(isApplicationTranslatedInto( *it )) ) {
477 d->langTwoAlpha.clear();
486 bool TDELocale::isApplicationTranslatedInto(
const TQString & language)
497 TQString appName = d->appName;
499 appName = TQString::fromLatin1(maincatalogue);
507 TQString sFileName = TQString::fromLatin1(
"%1/LC_MESSAGES/%2.mo")
512 TQString sAbsFileName = locate(
"locale", sFileName );
513 if (sAbsFileName.isEmpty())
514 sAbsFileName = locate(
"locale-bundle", sFileName );
517 return ! sAbsFileName.isEmpty();
528 int f = str.find(
':');
533 chrset = TQString::null;
539 chrset = str.mid(f + 1);
568 case 1:
return translate(
"January",
"Jan");
569 case 2:
return translate(
"February",
"Feb");
570 case 3:
return translate(
"March",
"Mar");
571 case 4:
return translate(
"April",
"Apr");
572 case 5:
return translate(
"May short",
"May");
575 case 8:
return translate(
"August",
"Aug");
576 case 9:
return translate(
"September",
"Sep");
577 case 10:
return translate(
"October",
"Oct");
578 case 11:
return translate(
"November",
"Nov");
579 case 12:
return translate(
"December",
"Dec");
588 case 5:
return translate(
"May long",
"May");
598 return TQString::null;
606 case 1:
return translate(
"of January",
"of Jan");
607 case 2:
return translate(
"of February",
"of Feb");
608 case 3:
return translate(
"of March",
"of Mar");
609 case 4:
return translate(
"of April",
"of Apr");
610 case 5:
return translate(
"of May short",
"of May");
611 case 6:
return translate(
"of June",
"of Jun");
612 case 7:
return translate(
"of July",
"of Jul");
613 case 8:
return translate(
"of August",
"of Aug");
614 case 9:
return translate(
"of September",
"of Sep");
615 case 10:
return translate(
"of October",
"of Oct");
616 case 11:
return translate(
"of November",
"of Nov");
617 case 12:
return translate(
"of December",
"of Dec");
626 case 5:
return translate(
"of May long",
"of May");
630 case 9:
return translate(
"of September");
632 case 11:
return translate(
"of November");
633 case 12:
return translate(
"of December");
636 return TQString::null;
646 if ( !d->catalogNames.contains( catalog) ) {
647 d->catalogNames.append( catalog );
652 void TDELocale::updateCatalogues( )
667 for ( TQValueList<KCatalogue>::Iterator it = d->catalogues.begin();
668 it != d->catalogues.end(); )
670 it = d->catalogues.remove(it);
677 for ( TQStringList::ConstIterator itLangs = d->languageList.begin();
678 itLangs != d->languageList.end(); ++itLangs)
680 for ( TQStringList::ConstIterator itNames = d->catalogNames.begin();
681 itNames != d->catalogNames.end(); ++itNames)
684 d->catalogues.append( cat );
695 if ( d->catalogNames.contains( catalog )) {
696 d->catalogNames.remove( catalog );
697 if (TDEGlobal::_instance)
704 if ( d->catalogNames.contains( catalog ) ) {
705 d->catalogNames.remove( catalog );
706 d->catalogNames.prepend( catalog );
719 TQString TDELocale::translate_priv(
const char *msgid,
720 const char *fallback,
721 const char **translated,
722 int* pluralType )
const
727 if (!msgid || !msgid[0])
729 kdWarning() <<
"TDELocale: trying to look up \"\" in catalog. "
730 <<
"Fix the program" <<
endl;
731 return TQString::null;
734 if ( useDefaultLanguage() ) {
735 return TQString::fromUtf8( fallback );
738 for ( TQValueList<KCatalogue>::ConstIterator it = d->catalogues.begin();
739 it != d->catalogues.end();
746 return TQString::fromUtf8( fallback );
749 const char * text = (*it).translate( msgid );
758 *pluralType = (*it).pluralType();
760 return TQString::fromUtf8( text );
765 return TQString::fromUtf8( fallback );
770 return translate_priv(msgid, msgid);
775 if (!index || !index[0] || !fallback || !fallback[0])
777 kdDebug(173) <<
"TDELocale: trying to look up \"\" in catalog. "
778 <<
"Fix the program" <<
endl;
779 return TQString::null;
782 if ( useDefaultLanguage() )
783 return TQString::fromUtf8( fallback );
785 char *newstring =
new char[strlen(index) + strlen(fallback) + 5];
786 sprintf(newstring,
"_: %s\n%s", index, fallback);
788 TQString r = translate_priv(newstring, fallback);
794 static TQString put_n_in(
const TQString &orig,
unsigned long n)
797 int index = ret.find(
"%n");
800 ret.replace(index, 2, TQString::number(n));
804 #define EXPECT_LENGTH(x) \
805 if (forms.count() != x) { \
806 kdError() << "translation of \"" << singular << "\" doesn't contain " << x << " different plural forms as expected\n"; \
807 return TQString( "BROKEN TRANSLATION %1" ).arg( singular ); }
810 unsigned long n )
const
812 if (!singular || !singular[0] || !plural || !plural[0])
814 kdWarning() <<
"TDELocale: trying to look up \"\" in catalog. "
815 <<
"Fix the program" <<
endl;
816 return TQString::null;
819 char *newstring =
new char[strlen(singular) + strlen(plural) + 6];
820 sprintf(newstring,
"_n: %s\n%s", singular, plural);
823 TQString r = translate_priv(newstring, 0, 0, &pluralType);
826 if ( r.isEmpty() || useDefaultLanguage() || pluralType == -1) {
828 return put_n_in( TQString::fromUtf8( singular ), n );
830 TQString tmp = TQString::fromUtf8( plural );
832 if (tmp.find(
"%n") == -1) {
833 kdDebug() <<
"the message for i18n should contain a '%n'! " << plural <<
endl;
836 return put_n_in( tmp, n );
840 TQStringList forms = TQStringList::split(
"\n", r,
false );
841 switch ( pluralType ) {
844 return put_n_in( forms[0], n);
848 return put_n_in( forms[0], n);
850 return put_n_in( forms[1], n);
853 if ( n == 1 || n == 0 )
854 return put_n_in( forms[0], n);
856 return put_n_in( forms[1], n);
860 return put_n_in( forms[0], n);
862 return put_n_in( forms[1], n);
864 return put_n_in( forms[2], n);
867 if ( n%10 == 1 && n%100 != 11)
868 return put_n_in( forms[0], n);
869 else if (( n%10 >= 2 && n%10 <=4 ) && (n%100<10 || n%100>20))
870 return put_n_in( forms[1], n);
872 return put_n_in( forms[2], n);
876 return put_n_in( forms[0], n);
877 else if ( n%10 >= 2 && n%10 <=4 && (n%100<10 || n%100>=20) )
878 return put_n_in( forms[1], n);
880 return put_n_in( forms[2], n);
884 return put_n_in( forms[1], n);
885 else if ( n%100 == 2 )
886 return put_n_in( forms[2], n);
887 else if ( n%100 == 3 || n%100 == 4 )
888 return put_n_in( forms[3], n);
890 return put_n_in( forms[0], n);
893 if ( n%10 == 0 || (n%100>=11 && n%100<=19) )
894 return put_n_in( forms[2], n);
895 else if ( n%10 == 1 )
896 return put_n_in( forms[0], n);
898 return put_n_in( forms[1], n);
903 return put_n_in( forms[0], n);
904 else if (( n >= 2 ) && ( n <= 4 ))
905 return put_n_in( forms[1], n);
907 return put_n_in( forms[2], n);
911 return put_n_in( forms[0], n );
912 else if ( ( n == 0 ) || ( n%100 > 0 && n%100 <= 10 ) )
913 return put_n_in( forms[1], n );
914 else if ( n%100 > 10 && n%100 < 20 )
915 return put_n_in( forms[2], n );
917 return put_n_in( forms[3], n );
921 return put_n_in(forms[0], n);
923 return put_n_in(forms[1], n);
925 return put_n_in(forms[2], n);
927 return put_n_in(forms[3], n);
930 if (n != 11 && n % 10 == 1)
931 return put_n_in(forms[0], n);
932 else if (n / 10 != 1 && n % 10 >= 2 && n % 10 <= 4)
933 return put_n_in(forms[1], n);
935 return put_n_in(forms[2], n);
939 return put_n_in(forms[0], n);
940 else if (n % 10 == 2)
941 return put_n_in(forms[1], n);
943 return put_n_in(forms[2], n);
947 return put_n_in(forms[0], n);
949 return put_n_in(forms[1], n);
951 return put_n_in(forms[2], n);
953 return put_n_in(forms[3], n);
955 return put_n_in(forms[4], n);
957 kdFatal() <<
"The function should have been returned in another way\n";
959 return TQString::null;
963 const char *message)
const
965 if (!source || !source[0]) {
966 kdWarning() <<
"TDELocale: trying to look up \"\" in catalog. "
967 <<
"Fix the program" <<
endl;
968 return TQString::null;
971 if ( useDefaultLanguage() ) {
972 return TQString::null;
976 const char *translation = 0;
979 if ( message && message[0]) {
980 char *newstring =
new char[strlen(source) + strlen(message) + 5];
981 sprintf(newstring,
"_: %s\n%s", source, message);
982 const char *translation = 0;
984 r = translate_priv(newstring, source, &translation);
990 if ( context && context[0] && message && message[0]) {
991 newstring =
new char[strlen(context) + strlen(message) + 5];
992 sprintf(newstring,
"_: %s\n%s", context, message);
994 r = translate_priv(newstring, source, &translation);
1000 r = translate_priv(source, source, &translation);
1003 return TQString::null;
1009 return d->nounDeclension;
1015 return d->dateMonthNamePossessive;
1021 return d->weekStartDay;
1027 return (d->weekStartDay==1);
1033 return m_decimalSymbol;
1039 return m_thousandsSeparator;
1045 return m_currencySymbol;
1051 return m_monetaryDecimalSymbol;
1057 return m_monetaryThousandsSeparator;
1063 return m_positiveSign;
1069 return m_negativeSign;
1075 return m_fracDigits;
1081 return m_positivePrefixCurrencySymbol;
1087 return m_negativePrefixCurrencySymbol;
1093 return m_positiveMonetarySignPosition;
1099 return m_negativeMonetarySignPosition;
1102 static inline void put_it_in( TQChar *buffer, uint& index,
const TQString &s )
1104 for ( uint l = 0; l < s.length(); l++ )
1105 buffer[index++] = s.at( l );
1108 static inline void put_it_in( TQChar *buffer, uint& index,
int number )
1110 buffer[index++] = number / 10 +
'0';
1111 buffer[index++] = number % 10 +
'0';
1115 static void _insertSeparator(TQString &str,
const TQString &separator,
1116 const TQString &decimalSymbol)
1119 TQString mainPart = str.section(decimalSymbol, 0, 0);
1120 TQString fracPart = str.section(decimalSymbol, 1, 1,
1121 TQString::SectionIncludeLeadingSep);
1123 for (
int pos = mainPart.length() - 3; pos > 0; pos -= 3)
1124 mainPart.insert(pos, separator);
1126 str = mainPart + fracPart;
1130 const TQString & symbol,
1131 int precision)
const
1134 TQString currency = symbol.isNull()
1141 TQString res = TQString::number(neg?-num:num,
'f', precision);
1163 case BeforeQuantityMoney:
1166 case AfterQuantityMoney:
1170 currency.prepend(sign);
1173 currency.append(sign);
1181 res.prepend(currency);
1184 res.append (currency);
1197 if (precision == -1) precision = 2;
1199 return formatNumber(TQString::number(num,
'f', precision),
false, 0);
1213 static void _inc_by_one(TQString &str,
int position)
1215 for (
int i = position; i >= 0; i--)
1217 char last_char = str[i].latin1();
1221 str[i] = (TQChar)
'1';
1224 str[i] = (TQChar)
'2';
1227 str[i] = (TQChar)
'3';
1230 str[i] = (TQChar)
'4';
1233 str[i] = (TQChar)
'5';
1236 str[i] = (TQChar)
'6';
1239 str[i] = (TQChar)
'7';
1242 str[i] = (TQChar)
'8';
1245 str[i] = (TQChar)
'9';
1248 str[i] = (TQChar)
'0';
1249 if (i == 0) str.prepend(
'1');
1259 static void _round(TQString &str,
int precision)
1261 int decimalSymbolPos = str.find(
'.');
1263 if (decimalSymbolPos == -1)
1264 if (precision == 0)
return;
1265 else if (precision > 0)
1268 decimalSymbolPos = str.length() - 1;
1272 str.append(TQString().fill(
'0', precision));
1275 char last_char = str[decimalSymbolPos + precision + 1].latin1();
1290 _inc_by_one(str, decimalSymbolPos + precision);
1296 decimalSymbolPos = str.find(
'.');
1297 str.truncate(decimalSymbolPos + precision + 1);
1300 if (precision == 0) str = str.section(
'.', 0, 0);
1304 int precision)
const
1306 TQString tmpString = numStr;
1307 if ((round && precision < 0) ||
1308 ! TQRegExp(
"^[+-]?\\d+(\\.\\d+)*(e[+-]?\\d+)?$").exactMatch(tmpString))
1313 bool neg = (tmpString[0] == (TQChar)
'-');
1314 if (neg || tmpString[0] == (TQChar)
'+') tmpString.remove(0, 1);
1317 TQString mantString = tmpString.section(
'e', 0, 0,
1318 TQString::SectionCaseInsensitiveSeps);
1319 TQString expString = tmpString.section(
'e', 1, 1,
1320 TQString::SectionCaseInsensitiveSeps |
1321 TQString::SectionIncludeLeadingSep);
1323 if (round) _round(mantString, precision);
1334 return mantString + expString;
1343 if ( ! pDate.isValid() )
return buffer;
1345 bool escape =
false;
1350 for ( uint format_index = 0; format_index < rst.length(); ++format_index )
1354 if ( (TQChar(rst.at( format_index )).unicode()) ==
'%' )
1357 buffer.append(rst.at(format_index));
1361 switch ( TQChar(rst.at( format_index )).unicode() )
1367 buffer.append(
calendar()->yearString(pDate,
false));
1370 buffer.append(
calendar()->yearString(pDate,
true));
1373 buffer.append(
calendar()->monthString(pDate,
true));
1376 buffer.append(
calendar()->dayString(pDate,
true));
1379 buffer.append(
calendar()->monthString(pDate,
false));
1382 if (d->nounDeclension && d->dateMonthNamePossessive)
1388 if (d->nounDeclension && d->dateMonthNamePossessive)
1394 buffer.append(
calendar()->dayString(pDate,
false));
1403 buffer.append(rst.at(format_index));
1414 maincatalogue = catalog;
1419 TQString str = _str.stripWhiteSpace();
1427 TQString exponentialPart;
1430 EPos = str.find(
'E', 0,
false);
1434 exponentialPart = str.mid(EPos);
1435 str = str.left(EPos);
1445 major = str.left(pos);
1455 int fromEnd = major.length() - pos;
1456 if ( fromEnd % (3+thlen) != 0
1457 || pos - lastpos > 3
1459 || (lastpos>0 && pos-lastpos!=3))
1461 if (ok) *ok =
false;
1466 major.remove( pos, thlen );
1468 if (lastpos>0 && major.length()-lastpos!=3)
1470 if (ok) *ok =
false;
1475 if (neg) tot = (TQChar)
'-';
1477 tot += major +
'.' + minor + exponentialPart;
1479 return tot.toDouble(ok);
1484 TQString str = _str.stripWhiteSpace();
1486 bool currencyFound =
false;
1489 int pos = str.find(symbol);
1490 if ( pos == 0 || pos == (
int) str.length()-symbol.length() )
1492 str.remove(pos,symbol.length());
1493 str = str.stripWhiteSpace();
1494 currencyFound =
true;
1498 if (ok) *ok =
false;
1505 if (str[0] == (TQChar)
'(' && str[str.length()-1] == (TQChar)
')')
1508 str.remove(str.length()-1,1);
1515 if ( i1 == 0 || i1 == (
int) str.length()-1 )
1521 if (neg) str = str.stripWhiteSpace();
1525 if ( !currencyFound )
1527 pos = str.find(symbol);
1528 if ( pos == 0 || pos == (
int) str.length()-symbol.length() )
1530 str.remove(pos,symbol.length());
1531 str = str.stripWhiteSpace();
1543 major = str.left(pos);
1553 int fromEnd = major.length() - pos;
1554 if ( fromEnd % (3+thlen) != 0
1555 || pos - lastpos > 3
1557 || (lastpos>0 && pos-lastpos!=3))
1559 if (ok) *ok =
false;
1563 major.remove( pos, thlen );
1565 if (lastpos>0 && major.length()-lastpos!=3)
1567 if (ok) *ok =
false;
1572 if (neg) tot = (TQChar)
'-';
1573 tot += major +
'.' + minior;
1574 return tot.toDouble(ok);
1583 static int readInt(
const TQString &str, uint &pos)
1585 if (!str.at(pos).isDigit())
return -1;
1587 for (; str.length() > pos && str.at(pos).isDigit(); pos++)
1590 result += str.at(pos).digitValue();
1599 date =
readDate(intstr, ShortFormat, ok);
1600 if (date.isValid())
return date;
1601 return readDate(intstr, NormalFormat, ok);
1607 return readDate( intstr, fmt, ok );
1613 TQString str = intstr.simplifyWhiteSpace().lower();
1614 int day = -1, month = -1;
1624 while (fmt.length() > fmtpos && str.length() > strpos && !error)
1627 TQChar c = fmt.at(fmtpos++);
1629 if (c != (TQChar)
'%') {
1630 if (c.isSpace() && str.at(strpos).isSpace())
1632 else if (c != str.at(strpos++))
1639 if (str.length() > strpos && str.at(strpos).isSpace())
1642 c = fmt.at(fmtpos++);
1650 while (error && (j < 8)) {
1652 int len = s.length();
1653 if (str.mid(strpos, len) == s)
1665 if (d->nounDeclension && d->dateMonthNamePossessive) {
1667 while (error && (j < 13)) {
1669 int len = s.length();
1670 if (str.mid(strpos, len) == s) {
1679 while (error && (j < 13)) {
1681 int len = s.length();
1682 if (str.mid(strpos, len) == s) {
1695 error = iLength <= 0;
1703 error = iLength <= 0;
1711 error = iLength <= 0;
1719 if ( fmt.length() > fmtpos || str.length() > strpos )
1725 if ( year != -1 && month != -1 && day != -1 && !error)
1736 if (ok) *ok =
false;
1744 _time =
readTime(intstr, WithSeconds, ok);
1745 if (_time.isValid())
return _time;
1746 return readTime(intstr, WithoutSeconds, ok);
1751 TQString str = intstr.simplifyWhiteSpace().lower();
1752 TQString Format =
timeFormat().simplifyWhiteSpace();
1753 if (flags & WithoutSeconds)
1754 Format.remove(TQRegExp(
".%S"));
1756 int hour = -1, minute = -1;
1757 int second = ( (flags & WithoutSeconds) == 0 ) ? -1 : 0;
1763 while (Format.length() > Formatpos || str.length() > strpos)
1765 if ( !(Format.length() > Formatpos && str.length() > strpos) )
goto error;
1767 TQChar c = Format.at(Formatpos++);
1769 if (c != (TQChar)
'%')
1773 else if (c != str.at(strpos++))
1779 if (str.length() > strpos && str.at(strpos).isSpace())
1782 c = Format.at(Formatpos++);
1789 int len = s.length();
1790 if (str.mid(strpos, len) == s)
1799 if (str.mid(strpos, len) == s) {
1812 hour = readInt(str, strpos);
1813 if (hour < 0 || hour > 23)
1821 hour = readInt(str, strpos);
1822 if (hour < 1 || hour > 12)
1828 minute = readInt(str, strpos);
1829 if (minute < 0 || minute > 59)
1835 second = readInt(str, strpos);
1836 if (second < 0 || second > 59)
1848 return TQTime(hour, minute, second);
1851 if (ok) *ok =
false;
1853 return TQTime(-1, -1, -1);
1859 return formatTime( pTime, includeSecs,
false );
1868 TQChar *buffer =
new TQChar[rst.length() * 3 / 2 + 30];
1871 bool escape =
false;
1874 for ( uint format_index = 0; format_index < rst.length(); format_index++ )
1878 if ( (TQChar(rst.at( format_index )).unicode()) ==
'%' )
1881 buffer[index++] = rst.at( format_index );
1885 switch ( TQChar(rst.at( format_index )).unicode() )
1888 buffer[index++] = (TQChar)
'%';
1891 put_it_in( buffer, index, pTime.hour() );
1895 put_it_in( buffer, index, pTime.hour() );
1897 put_it_in( buffer, index, ( pTime.hour() + 11) % 12 + 1 );
1900 put_it_in( buffer, index, pTime.minute() );
1904 put_it_in( buffer, index, pTime.second() );
1905 else if ( index > 0 )
1914 number = pTime.hour();
1917 if ( (TQChar(rst.at( format_index )).unicode()) ==
'l' )
1918 number = isDuration ? pTime.hour() : (pTime.hour() + 11) % 12 + 1;
1920 buffer[index++] = number / 10 +
'0';
1921 buffer[index++] = number % 10 +
'0';
1927 if ( pTime.hour() >= 12 )
1928 put_it_in( buffer, index,
translate(
"pm") );
1930 put_it_in( buffer, index,
translate(
"am") );
1934 buffer[index++] = rst.at( format_index );
1940 TQString ret( buffer, index );
1943 return ret.stripWhiteSpace();
1950 if ((
timeFormat().contains(TQString::fromLatin1(
"%I")) > 0) ||
1951 (
timeFormat().contains(TQString::fromLatin1(
"%l")) > 0))
1959 return d->languageList.join( TQString::fromLatin1(
":") );
1964 return d->languageList;
1969 bool includeSeconds)
const
1971 return translate(
"concatenation of dates and time",
"%1 %2")
1972 .arg(
formatDate( pDateTime.date(), shortFormat ) )
1973 .arg(
formatTime( pDateTime.time(), includeSeconds ) );
1980 return instance->translate(text);
1981 return TQString::fromUtf8(text);
1984 TQString
i18n(
const char* index,
const char *text)
1988 return instance->translate(index, text);
1989 return TQString::fromUtf8(text);
1992 TQString
i18n(
const char* singular,
const char* plural,
unsigned long n)
1996 return instance->translate(singular, plural, n);
1998 return put_n_in(TQString::fromUtf8(singular), n);
2000 return put_n_in(TQString::fromUtf8(plural), n);
2003 void TDELocale::initInstance()
2005 if (TDEGlobal::_locale)
2016 kdDebug(173) <<
"no app name available using TDELocale - nothing to do\n";
2021 TQStringList search;
2027 for (
int id=localDoc.count()-1;
id >= 0; --
id)
2030 langs.append(
"en" );
2032 TQStringList::ConstIterator lang;
2033 for (lang = langs.begin(); lang != langs.end(); ++lang)
2034 search.append(TQString(
"%1%2/%3").arg(localDoc[
id]).arg(*lang).arg(fname));
2038 TQStringList::Iterator it;
2039 for (it = search.begin(); it != search.end(); ++it)
2041 kdDebug(173) <<
"Looking for help in: " << *it <<
endl;
2043 TQFileInfo info(*it);
2044 if (info.exists() && info.isFile() && info.isReadable())
2048 return TQString::null;
2051 bool TDELocale::useDefaultLanguage()
const
2056 void TDELocale::initEncoding(
TDEConfig *)
2058 const int mibDefault = 4;
2061 setEncoding( TQTextCodec::codecForLocale()->mibEnum() );
2063 if ( !d->codecForEncoding )
2065 kdWarning(173) <<
" Defaulting to ISO 8859-1 encoding." <<
endl;
2069 Q_ASSERT( d->codecForEncoding );
2072 void TDELocale::initFileNameEncoding(
TDEConfig *)
2076 d->utf8FileEncoding = getenv(
"TDE_UTF8_FILENAMES") != 0;
2077 if (d->utf8FileEncoding)
2079 TQFile::setEncodingFunction(TDELocale::encodeFileNameUTF8);
2080 TQFile::setDecodingFunction(TDELocale::decodeFileNameUTF8);
2086 TQCString TDELocale::encodeFileNameUTF8(
const TQString & fileName )
2088 return TQString(fileName).utf8();
2091 TQString TDELocale::decodeFileNameUTF8(
const TQCString & localFileName )
2093 return TQString::fromUtf8(localFileName);
2099 m_dateFormat = format.stripWhiteSpace();
2105 m_dateFormatShort = format.stripWhiteSpace();
2111 d->dateMonthNamePossessive = possessive;
2117 m_timeFormat = format.stripWhiteSpace();
2124 d->weekStartDay = 1;
2126 d->weekStartDay = 7;
2133 d->weekStartDay = 1;
2135 d->weekStartDay = day;
2141 return m_dateFormat;
2147 return m_dateFormatShort;
2153 return m_timeFormat;
2159 m_decimalSymbol = symbol.stripWhiteSpace();
2166 m_thousandsSeparator = separator;
2172 m_positiveSign = sign.stripWhiteSpace();
2178 m_negativeSign = sign.stripWhiteSpace();
2184 m_positiveMonetarySignPosition = signpos;
2190 m_negativeMonetarySignPosition = signpos;
2196 m_positivePrefixCurrencySymbol = prefix;
2202 m_negativePrefixCurrencySymbol = prefix;
2208 m_fracDigits = digits;
2215 m_monetaryThousandsSeparator = separator;
2221 m_monetaryDecimalSymbol = symbol.stripWhiteSpace();
2227 m_currencySymbol = symbol.stripWhiteSpace();
2246 return d->measureSystem;
2252 d->measureSystem = value;
2257 return TQString::fromLatin1(
"en_US");
2262 return TQString::fromLatin1(
"C");
2271 strcpy(d->win32SystemEncoding,
"cp ");
2272 if (GetLocaleInfoA( MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), SORT_DEFAULT),
2273 LOCALE_IDEFAULTANSICODEPAGE, d->win32SystemEncoding+3,
sizeof(d->win32SystemEncoding)-3-1 ))
2275 return d->win32SystemEncoding;
2289 if (d->utf8FileEncoding)
2296 return d->codecForEncoding;
2301 TQTextCodec * codec = TQTextCodec::codecForMib(mibEnum);
2303 d->codecForEncoding = codec;
2310 if (d->langTwoAlpha.count())
2311 return d->langTwoAlpha;
2315 TQStringList result;
2317 TDEConfig config(TQString::fromLatin1(
"language.codes"),
true,
false);
2320 for ( TQStringList::ConstIterator it = origList.begin();
2321 it != origList.end();
2324 TQString lang = *it;
2325 TQStringList langLst;
2326 if (config.
hasKey( lang ))
2330 int i = lang.find(
'_');
2336 for ( TQStringList::ConstIterator langIt = langLst.begin();
2337 langIt != langLst.end();
2340 if ( !(*langIt).isEmpty() && !result.contains( *langIt ) )
2344 d->langTwoAlpha = result;
2351 d->languages =
new TDEConfig(
"all_languages",
true,
false,
"locale");
2353 return d->languages->groupList();
2359 d->languages =
new TDEConfig(
"all_languages",
true,
false,
"locale");
2361 TQString groupName = code;
2362 const int i = groupName.find(
'_');
2363 groupName.replace(0, i, groupName.left(i).lower());
2365 d->languages->setGroup(groupName);
2366 return d->languages->readEntry(
"Name");
2371 TQStringList countries;
2373 for(TQStringList::ConstIterator it = paths.begin();
2374 it != paths.end(); ++it)
2376 TQString code = (*it).mid((*it).length()-16, 2);
2378 countries.append(code);
2385 TDEConfig cfg(
"l10n/"+code.lower()+
"/entry.desktop",
true,
false,
"locale");
2394 d->calendarType = calType;
2404 return d->calendarType;
2420 d =
new TDELocalePrivate;
2428 m_decimalSymbol = rhs.m_decimalSymbol;
2429 m_thousandsSeparator = rhs.m_thousandsSeparator;
2430 m_currencySymbol = rhs.m_currencySymbol;
2431 m_monetaryDecimalSymbol = rhs.m_monetaryDecimalSymbol;
2432 m_monetaryThousandsSeparator = rhs.m_monetaryThousandsSeparator;
2433 m_positiveSign = rhs.m_positiveSign;
2434 m_negativeSign = rhs.m_negativeSign;
2435 m_fracDigits = rhs.m_fracDigits;
2436 m_positivePrefixCurrencySymbol = rhs.m_positivePrefixCurrencySymbol;
2437 m_negativePrefixCurrencySymbol = rhs.m_negativePrefixCurrencySymbol;
2438 m_positiveMonetarySignPosition = rhs.m_positiveMonetarySignPosition;
2439 m_negativeMonetarySignPosition = rhs.m_negativeMonetarySignPosition;
2442 m_timeFormat = rhs.m_timeFormat;
2443 m_dateFormat = rhs.m_dateFormat;
2444 m_dateFormatShort = rhs.m_dateFormatShort;
2446 m_language = rhs.m_language;
2447 m_country = rhs.m_country;
2462 void nothing() {
i18n(
"&Next"); }
static KCalendarSystem * create(const TQString &calType=TQString::fromLatin1("gregorian"), const TDELocale *locale=0)
Gets specific calendar type number of days in previous month for a given date.
CalendarSystem abstract class, default derived kde gregorian class and factory class.
virtual int monthStringToInteger(const TQString &sNum, int &iLength) const
Converts a month literal of a part of a string into a integer starting at the beginning of the string...
virtual TQString weekDayName(int weekDay, bool shortName=false) const =0
Gets specific calendar type week day name If an invalid week day is specified, TQString::null is retu...
virtual TQString monthNamePossessive(int month, int year, bool shortName=false) const =0
Returns a string containing the possessive form of the month name.
virtual int yearStringToInteger(const TQString &sNum, int &iLength) const
Converts a year literal of a part of a string into a integer starting at the beginning of the string.
virtual int month(const TQDate &date) const =0
Gets specific calendar type month for a given gregorian date.
virtual TQString monthName(int month, int year, bool shortName=false) const =0
Gets specific calendar type month name for a given month number If an invalid month is specified,...
virtual bool setYMD(TQDate &date, int y, int m, int d) const =0
Changes the date's year, month and day.
virtual int year(const TQDate &date) const =0
Gets specific calendar type year for a given gregorian date.
virtual int dayStringToInteger(const TQString &sNum, int &iLength) const
Converts a day literal of a part of a string into a integer starting at the beginning of the string.
This class abstracts a gettext message catalog.
const char * translate(const char *msgid) const
Retrieves a translation of the specified message id.
TQString name() const
Returns the name of the catalog.
KDE Configuration entries.
KDE Configuration Management abstract base class.
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
Reads the value of an entry specified by pKey in the current group.
int readListEntry(const TQString &pKey, TQStrList &list, char sep=',') const
Reads a list of strings.
bool hasKey(const TQString &key) const
Checks whether the key has an entry in the currently active group.
void setGroup(const TQString &group)
Specifies the group in which keys will be read and written.
Helper class to facilitate working with TDEConfig / KSimpleConfig groups.
Access KDE Configuration entries.
static TDEStandardDirs * dirs()
Returns the application standard dirs object.
static TDEInstance * instance()
Returns the global instance.
static TDELocale * locale()
Returns the global locale object.
Access to KDE global objects for use in shared libraries.
TQCString instanceName() const
Returns the name of the instance.
TDEConfig * config() const
Returns the general config object ("appnamerc").
TDELocale provides support for country specific stuff like the national language.
TQString formatDateTime(const TQDateTime &pDateTime, bool shortFormat=true, bool includeSecs=false) const
Returns a string formated to the current locale's conventions regarding both date and time.
TQString language() const
Returns the language used by this object.
TQString monthName(int i, bool shortName=false) const TDE_DEPRECATED
TQString formatMoney(double num, const TQString ¤cy=TQString::null, int digits=-1) const
Given a double, converts that to a numeric string containing the localized monetary equivalent.
void setPositivePrefixCurrencySymbol(bool prefix)
Changes the position where the currency symbol should be printed for positive monetary values.
void setTimeFormat(const TQString &format)
Changes the current time format.
TQString weekDayName(int i, bool shortName=false) const TDE_DEPRECATED
void setThousandsSeparator(const TQString &separator)
Changes the separator used to group digits when formating numbers.
void setDateFormatShort(const TQString &format)
Changes the current short date format.
TQString formatLong(long num) const
Given an integer, converts that to a numeric string containing the localized numeric equivalent.
void setNegativeMonetarySignPosition(SignPosition signpos)
Changes the sign position used for negative monetary values.
TQString positiveSign() const
Returns what a positive sign should look like ("+", " ", etc.) according to the current locale or use...
TDELocale(const TQString &catalog, TDEConfig *config=0)
Constructs a TDELocale with the given catalog name.
void setMonetaryDecimalSymbol(const TQString &symbol)
Changes the symbol used to identify the decimal pointer for monetary values.
double readMoney(const TQString &numStr, bool *ok=0) const
Converts a localized monetary string to a double.
SignPosition
Various positions for where to place the positive or negative sign when they are related to a monetar...
TQString i18n(const char *text)
i18n is the function that does everything you need to translate a string.
TQString calendarType() const
Returns the name of the calendar system that is currently being used by the system.
TQString thousandsSeparator() const
Returns what the thousands separator should look like ("," or "." etc.) according to the current loca...
TQString translateQt(const char *context, const char *sourceText, const char *message) const
Translates a message as a QTranslator is supposed to.
TQStringList languageList() const
Returns the languages selected by user.
TQString charset() const TDE_DEPRECATED
TQStringList allCountriesTwoAlpha() const
Returns list of all known country codes.
void setWeekStartsMonday(bool start) TDE_DEPRECATED
TQString monetaryThousandsSeparator() const
Returns what a thousands separator for monetary values should look like ("," or " " etc....
MeasureSystem
The Metric system will give you information in mm, while the Imperial system will give you informatio...
bool positivePrefixCurrencySymbol() const
If and only if the currency symbol precedes a positive value, this will be true.
TQTextCodec * codecForEncoding() const
Returns the user's preferred encoding.
int encodingMib() const
Returns the user's preferred encoding.
void setDateFormat(const TQString &format)
Changes the current date format.
bool negativePrefixCurrencySymbol() const
If and only if the currency symbol precedes a negative value, this will be true.
void setWeekStartDay(int day)
Changes how TDELocale defines the first day in week.
static TQString langLookup(const TQString &fname, const char *rtype="html")
Finds localized resource in resourceDir( rtype ) + <lang> + fname.
MeasureSystem measureSystem() const
Returns which measuring system we use.
void setActiveCatalogue(const TQString &catalog)
Sets the active catalog for translation lookup.
int fileEncodingMib() const
Returns the file encoding.
void setNegativeSign(const TQString &sign)
Changes the sign used to identify a negative number.
void removeCatalogue(const TQString &catalog)
Removes a catalog for translation lookup.
bool setCharset(const TQString &charset) TDE_DEPRECATED
void setMonetaryThousandsSeparator(const TQString &separator)
Changes the separator used to group digits when formating monetary values.
void setPageSize(int paperFormat)
Changes the preferred page size when printing.
const char * encoding() const
Returns the user's preferred encoding.
TQString currencySymbol() const
Returns what the symbol denoting currency in the current locale as as defined by user settings should...
static TQString defaultLanguage()
Returns the name of the internal language.
TQString timeFormat() const
Returns the currently selected time format.
const KCalendarSystem * calendar() const
Returns a pointer to the calendar system object.
TQTime readTime(const TQString &str, bool *ok=0) const
Converts a localized time string to a TQTime.
void setFracDigits(int digits)
Changes the number of digits used when formating numbers.
TDELocale & operator=(const TDELocale &rhs)
Assignment operator.
#define I18N_NOOP(x)
I18N_NOOP marks a string to be translated without translating it.
TQString monthNamePossessive(int i, bool shortName=false) const TDE_DEPRECATED
static TQString defaultCountry()
Returns the name of the default country.
TQStringList allLanguagesTwoAlpha() const
Returns list of all known ISO 639-1 codes.
bool nounDeclension() const
Use this to determine whether nouns are declined in locale's language.
void setDateMonthNamePossessive(bool possessive)
Changes the form of month name used in dates.
TQString country() const
Returns the country code of the country where the user lives.
void setDecimalSymbol(const TQString &symbol)
Changes the symbol used to identify the decimal pointer.
TQString decimalSymbol() const
Returns what a decimal point should look like ("." or "," etc.) according to the current locale or us...
TQString twoAlphaToLanguageName(const TQString &code) const
Convert a ISO 639-1 code to a human readable form.
int fracDigits() const
The number of fractional digits to include in numeric/monetary values (usually 2).
void setCalendar(const TQString &calendarType)
Changes the current calendar system to the calendar specified.
bool weekStartsMonday() const TDE_DEPRECATED
TQString formatNumber(double num, int precision=-1) const
Given a double, converts that to a numeric string containing the localized numeric equivalent.
int weekStartDay() const
Use this to determine which day is the first day of the week.
TQString formatDate(const TQDate &pDate, bool shortFormat=false) const
Returns a string formatted to the current locale's conventions regarding dates.
void setPositiveMonetarySignPosition(SignPosition signpos)
Changes the sign position used for positive monetary values.
bool setLanguage(const TQString &language)
Changes the current language.
TQString dateFormatShort() const
Returns the currently selected short date format.
TQString formatTime(const TQTime &pTime, bool includeSecs, bool isDuration) const
Returns a string formatted to the current locale's conventions regarding times.
TQStringList languagesTwoAlpha() const
Returns the preferred languages as ISO 639-1 codes.
bool setEncoding(int mibEnum)
Changes the current encoding.
SignPosition positiveMonetarySignPosition() const
Returns the position of a positive sign in relation to a monetary value.
double readNumber(const TQString &numStr, bool *ok=0) const
Converts a localized numeric string to a double.
TQString translate(const char *index) const
Translates the string into the corresponding string in the national language, if available.
void setPositiveSign(const TQString &sign)
Changes the sign used to identify a positive number.
TQString twoAlphaToCountryName(const TQString &code) const
Convert a country code to a human readable form.
TQString dateFormat() const
Returns the currently selected date format.
TQString negativeSign() const
Returns what a negative sign should look like ("-", etc.) according to the current locale or user set...
bool setCountry(const TQString &country)
Changes the current country.
SignPosition negativeMonetarySignPosition() const
Denotes where to place a negative sign in relation to a monetary value.
static void splitLocale(const TQString &str, TQString &language, TQString &country, TQString &charset)
Returns the parts of the parameter str understood as language setting the format is language_COUNTRY....
int pageSize() const
Returns the preferred page size for printing.
void setNegativePrefixCurrencySymbol(bool prefix)
Changes the position where the currency symbol should be printed for negative monetary values.
TQString monetaryDecimalSymbol() const
Returns what a decimal point should look like ("." or "," etc.) for monetary values,...
static void setMainCatalogue(const char *catalog)
Use this as main catalog for all TDELocales, if not the appname will be used.
void setMeasureSystem(MeasureSystem value)
Changes the preferred measuring system.
void insertCatalogue(const TQString &catalog)
Adds another catalog to search for translation lookup.
TQDate readDate(const TQString &str, bool *ok=0) const
Converts a localized date string to a TQDate.
bool dateMonthNamePossessive() const
Use this to determine whether in dates a possessive form of month name is preferred ("of January" rat...
void setCurrencySymbol(const TQString &symbol)
Changes the current currency symbol.
bool use12Clock() const
Use this to determine if the user wants a 12 hour clock.
TQString languages() const TDE_DEPRECATED
TQStringList resourceDirs(const char *type) const
This function is used internally by almost all other function as it serves and fills the directories ...
TQStringList findAllResources(const char *type, const TQString &filter=TQString::null, bool recursive=false, bool unique=false) const
Tries to find all resources with the specified type.
kndbgstream & endl(kndbgstream &s)
Does nothing.