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