xref: /haiku/headers/os/locale/Locale.h (revision 7749d0bb0c358a3279b1b9cc76d8376e900130a5)
1 /*
2  * Copyright 2003-2011, 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 <FormattingConventions.h>
11 #include <Language.h>
12 #include <Locker.h>
13 
14 
15 namespace icu {
16 	class DateFormat;
17 }
18 
19 
20 class BCatalog;
21 class BString;
22 class BTimeZone;
23 
24 
25 enum BDateElement {
26 	B_DATE_ELEMENT_INVALID = B_BAD_DATA,
27 	B_DATE_ELEMENT_YEAR = 0,
28 	B_DATE_ELEMENT_MONTH,
29 	B_DATE_ELEMENT_DAY,
30 	B_DATE_ELEMENT_AM_PM,
31 	B_DATE_ELEMENT_HOUR,
32 	B_DATE_ELEMENT_MINUTE,
33 	B_DATE_ELEMENT_SECOND
34 };
35 
36 enum BNumberElement {
37 	B_NUMBER_ELEMENT_INVALID = B_BAD_DATA,
38 	B_NUMBER_ELEMENT_INTEGER = 0,
39 	B_NUMBER_ELEMENT_FRACTIONAL,
40 	B_NUMBER_ELEMENT_CURRENCY
41 };
42 
43 
44 // TODO: move this to BCalendar (should we ever have that) or BDate
45 enum BWeekday {
46 	B_WEEKDAY_MONDAY = 1,
47 	B_WEEKDAY_TUESDAY,
48 	B_WEEKDAY_WEDNESDAY,
49 	B_WEEKDAY_THURSDAY,
50 	B_WEEKDAY_FRIDAY,
51 	B_WEEKDAY_SATURDAY,
52 	B_WEEKDAY_SUNDAY,
53 };
54 
55 
56 class BLocale {
57 public:
58 								BLocale(const BLanguage* language = NULL,
59 									const BFormattingConventions* conventions
60 										= NULL);
61 								BLocale(const BLocale& other);
62 								~BLocale();
63 
64 	static	const BLocale*		Default();
65 
66 			BLocale&			operator=(const BLocale& other);
67 
68 			status_t			GetCollator(BCollator* collator) const;
69 			status_t			GetLanguage(BLanguage* language) const;
70 			status_t			GetFormattingConventions(
71 									BFormattingConventions* conventions) const;
72 
73 			void				SetFormattingConventions(
74 									const BFormattingConventions& conventions);
75 			void				SetCollator(const BCollator& newCollator);
76 			void				SetLanguage(const BLanguage& newLanguage);
77 
78 			// see definitions in LocaleStrings.h
79 			const char*			GetString(uint32 id) const;
80 
81 			void				FormatString(char* target, size_t maxSize,
82 									char* fmt, ...) const;
83 			void				FormatString(BString* buffer, char* fmt,
84 									...) const;
85 
86 								// DateTime
87 
88 								// TODO: drop some of these once BDateTimeFormat
89 								//       has been implemented!
90 			ssize_t				FormatDateTime(char* target, size_t maxSize,
91 									time_t time, BDateFormatStyle dateStyle,
92 									BTimeFormatStyle timeStyle) const;
93 			status_t			FormatDateTime(BString* buffer, time_t time,
94 									BDateFormatStyle dateStyle,
95 									BTimeFormatStyle timeStyle,
96 									const BTimeZone* timeZone = NULL) const;
97 
98 								// Date
99 
100 								// TODO: drop some of these once BDateFormat
101 								//       has been implemented!
102 			ssize_t				FormatDate(char* string, size_t maxSize,
103 									time_t time, BDateFormatStyle style) const;
104 			status_t			FormatDate(BString* string, time_t time,
105 									BDateFormatStyle style,
106 									const BTimeZone* timeZone = NULL) const;
107 			status_t			FormatDate(BString* string,
108 									int*& fieldPositions, int& fieldCount,
109 									time_t time, BDateFormatStyle style) const;
110 			status_t			GetDateFields(BDateElement*& fields,
111 									int& fieldCount, BDateFormatStyle style
112 									) const;
113 
114 			status_t			GetStartOfWeek(BWeekday* weekday) const;
115 
116 								// Time
117 
118 								// TODO: drop some of these once BTimeFormat
119 								//       has been implemented!
120 			ssize_t				FormatTime(char* string, size_t maxSize,
121 									time_t time, BTimeFormatStyle style) const;
122 			status_t			FormatTime(BString* string, time_t time,
123 									BTimeFormatStyle style,
124 									const BTimeZone* timeZone = NULL) const;
125 			status_t			FormatTime(BString* string,
126 									int*& fieldPositions, int& fieldCount,
127 									time_t time, BTimeFormatStyle style) const;
128 			status_t			GetTimeFields(BDateElement*& fields,
129 									int& fieldCount, BTimeFormatStyle style
130 									) const;
131 
132 								// numbers
133 
134 			ssize_t				FormatNumber(char* string, size_t maxSize,
135 									double value) const;
136 			status_t			FormatNumber(BString* string,
137 									double value) const;
138 			ssize_t				FormatNumber(char* string, size_t maxSize,
139 									int32 value) const;
140 			status_t			FormatNumber(BString* string,
141 									int32 value) const;
142 
143 								// monetary
144 
145 			ssize_t				FormatMonetary(char* string, size_t maxSize,
146 									double value) const;
147 			status_t			FormatMonetary(BString* string,
148 									double value) const;
149 
150 			// Collator short-hands
151 			int					StringCompare(const char* s1,
152 									const char* s2) const;
153 			int					StringCompare(const BString* s1,
154 									const BString* s2) const;
155 
156 			void				GetSortKey(const char* string,
157 									BString* sortKey) const;
158 
159 private:
160 			icu::DateFormat*	_CreateDateFormatter(
161 									const BString& format) const;
162 			icu::DateFormat*	_CreateTimeFormatter(
163 									const BString& format) const;
164 
165 	mutable	BLocker				fLock;
166 			BCollator			fCollator;
167 			BFormattingConventions	fConventions;
168 			BLanguage			fLanguage;
169 };
170 
171 
172 //--- collator short-hands inlines ---
173 //	#pragma mark -
174 
175 inline int
176 BLocale::StringCompare(const char* s1, const char* s2) const
177 {
178 	return fCollator.Compare(s1, s2);
179 }
180 
181 
182 inline int
183 BLocale::StringCompare(const BString* s1, const BString* s2) const
184 {
185 	return fCollator.Compare(s1->String(), s2->String());
186 }
187 
188 
189 inline void
190 BLocale::GetSortKey(const char* string, BString* sortKey) const
191 {
192 	fCollator.GetSortKey(string, sortKey);
193 }
194 
195 
196 #endif	/* _B_LOCALE_H_ */
197