1 /* 2 * Copyright 2003-2010, Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _B_LOCALE_H_ 6 #define _B_LOCALE_H_ 7 8 9 #include <Collator.h> 10 #include <Country.h> 11 #include <Language.h> 12 13 14 class BCatalog; 15 class BString; 16 class BTimeZone; 17 18 19 typedef enum { 20 B_DATE_ELEMENT_INVALID = B_BAD_DATA, 21 B_DATE_ELEMENT_YEAR = 0, 22 B_DATE_ELEMENT_MONTH, 23 B_DATE_ELEMENT_DAY, 24 B_DATE_ELEMENT_AM_PM, 25 B_DATE_ELEMENT_HOUR, 26 B_DATE_ELEMENT_MINUTE, 27 B_DATE_ELEMENT_SECOND 28 } BDateElement; 29 30 31 class BLocale { 32 public: 33 BLocale(const char* languageAndCountryCode 34 = "en_US"); 35 BLocale(const BLocale& other); 36 BLocale& operator=(const BLocale& other); 37 ~BLocale(); 38 39 const BCollator* Collator() const { return &fCollator; } 40 const BCountry* Country() const { return &fCountry; } 41 const BLanguage* Language() const { return &fLanguage; } 42 const char* Code() const; 43 bool GetName(BString& name) const; 44 45 void SetCountry(const BCountry& newCountry); 46 void SetCollator(const BCollator& newCollator); 47 void SetLanguage(const char* languageCode); 48 49 // see definitions in LocaleStrings.h 50 const char* GetString(uint32 id); 51 52 void FormatString(char* target, size_t maxSize, 53 char* fmt, ...); 54 void FormatString(BString* buffer, char* fmt, ...); 55 status_t FormatDateTime(char* target, size_t maxSize, 56 time_t time, bool longFormat); 57 status_t FormatDateTime(BString* buffer, time_t time, 58 bool longFormat); 59 60 // Date 61 62 // TODO: drop some of these once BDateFormat 63 // has been implemented! 64 status_t FormatDate(char* string, size_t maxSize, 65 time_t time, bool longFormat); 66 status_t FormatDate(BString* string, time_t time, 67 bool longFormat); 68 status_t FormatDate(BString* string, 69 int*& fieldPositions, int& fieldCount, 70 time_t time, bool longFormat); 71 status_t GetDateFields(BDateElement*& fields, 72 int& fieldCount, bool longFormat) const; 73 status_t GetDateFormat(BString&, bool longFormat) const; 74 status_t SetDateFormat(const char* formatString, 75 bool longFormat = true); 76 77 int StartOfWeek() const; 78 79 // Time 80 81 // TODO: drop some of these once BTimeFormat 82 // has been implemented! 83 status_t FormatTime(char* string, size_t maxSize, 84 time_t time, bool longFormat); 85 status_t FormatTime(BString* string, time_t time, 86 bool longFormat, 87 const BTimeZone* timeZone = NULL); 88 status_t FormatTime(BString* string, 89 int*& fieldPositions, int& fieldCount, 90 time_t time, bool longFormat); 91 status_t GetTimeFields(BDateElement*& fields, 92 int& fieldCount, bool longFormat) const; 93 94 status_t SetTimeFormat(const char* formatString, 95 bool longFormat = true); 96 status_t GetTimeFormat(BString& out, 97 bool longFormat) const; 98 99 // numbers 100 101 status_t FormatNumber(char* string, size_t maxSize, 102 double value); 103 status_t FormatNumber(BString* string, double value); 104 status_t FormatNumber(char* string, size_t maxSize, 105 int32 value); 106 status_t FormatNumber(BString* string, int32 value); 107 108 // monetary 109 110 ssize_t FormatMonetary(char* string, size_t maxSize, 111 double value); 112 ssize_t FormatMonetary(BString* string, double value); 113 114 // Collator short-hands 115 int StringCompare(const char* s1, 116 const char* s2) const; 117 int StringCompare(const BString* s1, 118 const BString* s2) const; 119 120 void GetSortKey(const char* string, 121 BString* key) const; 122 123 protected: 124 BCollator fCollator; 125 BCountry fCountry; 126 BLanguage fLanguage; 127 128 icu_44::Locale* fICULocale; 129 BString fLongDateFormat; 130 BString fShortDateFormat; 131 BString fLongTimeFormat; 132 BString fShortTimeFormat; 133 }; 134 135 136 //--- collator short-hands inlines --- 137 // #pragma mark - 138 139 inline int 140 BLocale::StringCompare(const char* s1, const char* s2) const 141 { 142 return fCollator.Compare(s1, s2); 143 } 144 145 146 inline int 147 BLocale::StringCompare(const BString* s1, const BString* s2) const 148 { 149 return fCollator.Compare(s1->String(), s2->String()); 150 } 151 152 153 inline void 154 BLocale::GetSortKey(const char* string, BString* key) const 155 { 156 fCollator.GetSortKey(string, key); 157 } 158 159 160 #endif /* _B_LOCALE_H_ */ 161