diff --git a/headers/os/locale/Country.h b/headers/os/locale/Country.h index cb8c7825fd..ecaea64f89 100644 --- a/headers/os/locale/Country.h +++ b/headers/os/locale/Country.h @@ -45,6 +45,8 @@ class BCountry { bool DateFormat(BString&, bool longFormat) const; void SetDateFormat(const char* formatString, bool longFormat = true); + void SetTimeFormat(const char* formatString, + bool longFormat = true); bool TimeFormat(BString&, bool longFormat) const; const char* DateSeparator() const; const char* TimeSeparator() const; @@ -85,6 +87,8 @@ class BCountry { private: icu_4_2::DateFormat* fICULongDateFormatter; icu_4_2::DateFormat* fICUShortDateFormatter; + icu_4_2::DateFormat* fICULongTimeFormatter; + icu_4_2::DateFormat* fICUShortTimeFormatter; const char** fStrings; icu_4_2::Locale* fICULocale; }; diff --git a/src/kits/locale/Country.cpp b/src/kits/locale/Country.cpp index 6eaf94194e..ffb02e046d 100644 --- a/src/kits/locale/Country.cpp +++ b/src/kits/locale/Country.cpp @@ -54,6 +54,14 @@ BCountry::BCountry(const char* languageCode, const char* countryCode) fStrings(gStrings) { fICULocale = new icu_4_2::Locale(languageCode, countryCode); + fICULongDateFormatter = DateFormat::createDateInstance( + DateFormat::FULL, *fICULocale); + fICUShortDateFormatter = DateFormat::createDateInstance( + DateFormat::SHORT, *fICULocale); + fICULongTimeFormatter = DateFormat::createTimeInstance( + DateFormat::MEDIUM, *fICULocale); + fICUShortTimeFormatter = DateFormat::createTimeInstance( + DateFormat::SHORT, *fICULocale); } @@ -66,11 +74,19 @@ BCountry::BCountry(const char* languageAndCountryCode) DateFormat::FULL, *fICULocale); fICUShortDateFormatter = DateFormat::createDateInstance( DateFormat::SHORT, *fICULocale); + fICULongTimeFormatter = DateFormat::createTimeInstance( + DateFormat::MEDIUM, *fICULocale); + fICUShortTimeFormatter = DateFormat::createTimeInstance( + DateFormat::SHORT, *fICULocale); } BCountry::~BCountry() { + delete fICULongTimeFormatter; + delete fICUShortTimeFormatter; + delete fICULongDateFormatter; + delete fICUShortDateFormatter; delete fICULocale; } @@ -168,9 +184,7 @@ BCountry::FormatTime(BString* string, time_t time, bool longFormat) // TODO: ICU allows for 4 different levels of expansion : // short, medium, long, and full. Our bool parameter is not enough... icu_4_2::DateFormat* timeFormatter; - timeFormatter = DateFormat::createTimeInstance( - longFormat ? DateFormat::MEDIUM : DateFormat::SHORT, - *fICULocale); + timeFormatter = longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter; UnicodeString ICUString; ICUString = timeFormatter->format((UDate)time * 1000, ICUString); @@ -178,8 +192,6 @@ BCountry::FormatTime(BString* string, time_t time, bool longFormat) BStringByteSink stringConverter(string); ICUString.toUTF8(stringConverter); - - delete timeFormatter; } @@ -215,13 +227,24 @@ BCountry::SetDateFormat(const char* formatString, bool longFormat) } +void +BCountry::SetTimeFormat(const char* formatString, bool longFormat) +{ + icu_4_2::DateFormat* dateFormatter + = longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter; + SimpleDateFormat* dateFormatterImpl + = static_cast(dateFormatter); + + UnicodeString pattern(formatString); + dateFormatterImpl->applyPattern(pattern); +} + + bool BCountry::TimeFormat(BString& format, bool longFormat) const { icu_4_2::DateFormat* dateFormatter; - dateFormatter = DateFormat::createTimeInstance( - longFormat ? DateFormat::FULL : DateFormat::SHORT, - *fICULocale); + dateFormatter = longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter; SimpleDateFormat* dateFormatterImpl = static_cast(dateFormatter); diff --git a/src/kits/locale/Locale.cpp b/src/kits/locale/Locale.cpp index 614af3bc4a..9ccf91f873 100644 --- a/src/kits/locale/Locale.cpp +++ b/src/kits/locale/Locale.cpp @@ -38,8 +38,5 @@ BLocale::GetString(uint32 id) return ""; } - if (id >= B_LANGUAGE_STRINGS_BASE) - return fLanguage->GetString(id); - - return fCountry->GetString(id); + return fLanguage->GetString(id); } diff --git a/src/preferences/locale/TimeFormatSettingsView.cpp b/src/preferences/locale/TimeFormatSettingsView.cpp index bff8580726..2d741b5738 100644 --- a/src/preferences/locale/TimeFormatSettingsView.cpp +++ b/src/preferences/locale/TimeFormatSettingsView.cpp @@ -6,6 +6,8 @@ #include "TimeFormatSettingsView.h" +#include + #include #include #include @@ -139,7 +141,6 @@ IsSpecialDateChar(char charToTest) return false; } - // #pragma mark - @@ -182,10 +183,17 @@ FormatView::FormatView(BCountry* country) clockBox->SetLabel(B_TRANSLATE("Clock")); f24HrRadioButton = new BRadioButton("", B_TRANSLATE("24 hour"), - new BMessage(kSettingsContentsModified)); + new BMessage(kClockFormatChange)); f12HrRadioButton = new BRadioButton("", B_TRANSLATE("12 hour"), - new BMessage(kSettingsContentsModified)); + new BMessage(kClockFormatChange)); + + BString timeFormat; + fCountry->TimeFormat(timeFormat, false); + if (timeFormat.FindFirst(" a")) + f12HrRadioButton->SetValue(1); + else + f24HrRadioButton->SetValue(1); float spacing = be_control_look->DefaultItemSpacing(); @@ -414,7 +422,8 @@ FormatView::MessageReceived(BMessage* message) if (item) { separator = fSeparatorMenuField->Menu()->IndexOf(item); if (separator >= 0) - //settings.SetTimeFormatSeparator((FormatSeparator)separator); + //settings.SetTimeFormatSeparator( + // (FormatSeparator)separator); ; } @@ -427,7 +436,8 @@ FormatView::MessageReceived(BMessage* message) notificationMessage.AddInt32("TimeFormatSeparator", separator); notificationMessage.AddBool("24HrClock", f24HrRadioButton->Value() == 1); - // tracker->SendNotices(kDateFormatChanged, ¬ificationMessage); + // tracker->SendNotices(kDateFormatChanged, + // ¬ificationMessage); _UpdateExamples(); @@ -435,6 +445,41 @@ FormatView::MessageReceived(BMessage* message) break; } + case kClockFormatChange: + { + BString timeFormat; + fCountry->TimeFormat(timeFormat, false); + if (f24HrRadioButton->Value() == 1) { + timeFormat.ReplaceAll("k","h"); + timeFormat.ReplaceAll("K","H"); + timeFormat.RemoveAll(" a"); + } else { + timeFormat.ReplaceAll("h","k"); + timeFormat.ReplaceAll("H","K"); + timeFormat.Append(" a"); + f12HrRadioButton->SetValue(true); + } + std::cout << timeFormat.String() << std::endl; + fCountry->SetTimeFormat(timeFormat.String(), false); + + timeFormat.Truncate(0); + + fCountry->TimeFormat(timeFormat, true); + if (f24HrRadioButton->Value() == 1) { + timeFormat.ReplaceAll("k","h"); + timeFormat.ReplaceAll("K","H"); + timeFormat.RemoveAll(" a"); + } else { + timeFormat.ReplaceAll("h","k"); + timeFormat.ReplaceAll("H","K"); + timeFormat.Append(" a"); + } + fCountry->SetTimeFormat(timeFormat.String(), true); + _UpdateExamples(); + Window()->PostMessage(kSettingsContentsModified); + break; + } + default: BView::MessageReceived(message); } diff --git a/src/preferences/locale/TimeFormatSettingsView.h b/src/preferences/locale/TimeFormatSettingsView.h index 0b42482342..36385cbd4f 100644 --- a/src/preferences/locale/TimeFormatSettingsView.h +++ b/src/preferences/locale/TimeFormatSettingsView.h @@ -30,6 +30,7 @@ enum FormatSeparator { }; const uint32 kSettingsContentsModified = 'Scmo'; +const uint32 kClockFormatChange = 'cfmc'; const uint32 kMenuMessage = 'FRMT';