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