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 17 18 class BLocale { 19 public: 20 BLocale(const char* languageAndCountryCode 21 = "en_US"); 22 BLocale(const BLocale& other); 23 BLocale& operator=(const BLocale& other); 24 ~BLocale(); 25 26 const BCollator* Collator() const { return &fCollator; } 27 const BCountry* Country() const { return &fCountry; } 28 const BLanguage* Language() const { return &fLanguage; } 29 const char* Code() const; 30 31 void SetCountry(const BCountry& newCountry); 32 void SetCollator(const BCollator& newCollator); 33 void SetLanguage(const char* languageCode); 34 35 // see definitions in LocaleStrings.h 36 const char* GetString(uint32 id); 37 38 void FormatString(char* target, size_t maxSize, 39 char* fmt, ...); 40 void FormatString(BString* buffer, char* fmt, ...); 41 void FormatDateTime(char* target, size_t maxSize, 42 const char* fmt, time_t value); 43 void FormatDateTime(BString* buffer, const char* fmt, 44 time_t value); 45 46 // Date 47 48 status_t FormatDate(char* string, size_t maxSize, 49 time_t time, bool longFormat); 50 status_t FormatDate(BString* string, time_t time, 51 bool longFormat); 52 status_t FormatDate(BString* string, 53 int*& fieldPositions, int& fieldCount, 54 time_t time, bool longFormat); 55 status_t GetDateFields(BDateElement*& fields, 56 int& fieldCount, bool longFormat) const; 57 status_t GetDateFormat(BString&, bool longFormat) const; 58 status_t SetDateFormat(const char* formatString, 59 bool longFormat = true); 60 61 int StartOfWeek() const; 62 63 // Time 64 65 status_t FormatTime(char* string, size_t maxSize, 66 time_t time, bool longFormat); 67 status_t FormatTime(BString* string, time_t time, 68 bool longFormat); 69 status_t FormatTime(BString* string, 70 int*& fieldPositions, int& fieldCount, 71 time_t time, bool longFormat); 72 status_t GetTimeFields(BDateElement*& fields, 73 int& fieldCount, bool longFormat) const; 74 75 status_t SetTimeFormat(const char* formatString, 76 bool longFormat = true); 77 status_t GetTimeFormat(BString& out, 78 bool longFormat) const; 79 80 // numbers 81 82 status_t FormatNumber(char* string, size_t maxSize, 83 double value); 84 status_t FormatNumber(BString* string, double value); 85 status_t FormatNumber(char* string, size_t maxSize, 86 int32 value); 87 status_t FormatNumber(BString* string, int32 value); 88 89 // monetary 90 91 ssize_t FormatMonetary(char* string, size_t maxSize, 92 double value); 93 ssize_t FormatMonetary(BString* string, double value); 94 95 // Collator short-hands 96 int StringCompare(const char* s1, 97 const char* s2) const; 98 int StringCompare(const BString* s1, 99 const BString* s2) const; 100 101 void GetSortKey(const char* string, 102 BString* key) const; 103 104 protected: 105 BCollator fCollator; 106 BCountry fCountry; 107 BLanguage fLanguage; 108 109 icu_44::Locale* fICULocale; 110 BString fLongDateFormat; 111 BString fShortDateFormat; 112 BString fLongTimeFormat; 113 BString fShortTimeFormat; 114 }; 115 116 117 //--- collator short-hands inlines --- 118 // #pragma mark - 119 120 inline int 121 BLocale::StringCompare(const char* s1, const char* s2) const 122 { 123 return fCollator.Compare(s1, s2); 124 } 125 126 127 inline int 128 BLocale::StringCompare(const BString* s1, const BString* s2) const 129 { 130 return fCollator.Compare(s1->String(), s2->String()); 131 } 132 133 134 inline void 135 BLocale::GetSortKey(const char* string, BString* key) const 136 { 137 fCollator.GetSortKey(string, key); 138 } 139 140 141 #endif /* _B_LOCALE_H_ */ 142