xref: /haiku/headers/os/locale/Locale.h (revision 1026b0a1a76dc88927bb8175c470f638dc5464ee)
1 /*
2  * Copyright 2003-2012, 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 			ssize_t				FormatTime(char* string, size_t maxSize,
123 									time_t time, BString format) const;
124 			status_t			FormatTime(BString* string, time_t time,
125 									BTimeFormatStyle style,
126 									const BTimeZone* timeZone = NULL) const;
127 			status_t			FormatTime(BString* string, time_t time,
128 									BString format,
129 									const BTimeZone* timeZone) const;
130 			status_t			FormatTime(BString* string,
131 									int*& fieldPositions, int& fieldCount,
132 									time_t time, BTimeFormatStyle style) const;
133 			status_t			GetTimeFields(BDateElement*& fields,
134 									int& fieldCount, BTimeFormatStyle style
135 									) const;
136 
137 								// numbers
138 
139 			ssize_t				FormatNumber(char* string, size_t maxSize,
140 									double value) const;
141 			status_t			FormatNumber(BString* string,
142 									double value) const;
143 			ssize_t				FormatNumber(char* string, size_t maxSize,
144 									int32 value) const;
145 			status_t			FormatNumber(BString* string,
146 									int32 value) const;
147 
148 								// monetary
149 
150 			ssize_t				FormatMonetary(char* string, size_t maxSize,
151 									double value) const;
152 			status_t			FormatMonetary(BString* string,
153 									double value) const;
154 
155 			// Collator short-hands
156 			int					StringCompare(const char* s1,
157 									const char* s2) const;
158 			int					StringCompare(const BString* s1,
159 									const BString* s2) const;
160 
161 			void				GetSortKey(const char* string,
162 									BString* sortKey) const;
163 
164 private:
165 			icu::DateFormat*	_CreateDateFormatter(
166 									const BString& format) const;
167 			icu::DateFormat*	_CreateTimeFormatter(
168 									const BString& format) const;
169 
170 	mutable	BLocker				fLock;
171 			BCollator			fCollator;
172 			BFormattingConventions	fConventions;
173 			BLanguage			fLanguage;
174 };
175 
176 
177 //--- collator short-hands inlines ---
178 //	#pragma mark -
179 
180 inline int
181 BLocale::StringCompare(const char* s1, const char* s2) const
182 {
183 	return fCollator.Compare(s1, s2);
184 }
185 
186 
187 inline int
188 BLocale::StringCompare(const BString* s1, const BString* s2) const
189 {
190 	return fCollator.Compare(s1->String(), s2->String());
191 }
192 
193 
194 inline void
195 BLocale::GetSortKey(const char* string, BString* sortKey) const
196 {
197 	fCollator.GetSortKey(string, sortKey);
198 }
199 
200 
201 #endif	/* _B_LOCALE_H_ */
202