Locale.h revision 03b2550e
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 <DateTimeFormat.h>
11#include <FormattingConventions.h>
12#include <Language.h>
13#include <Locker.h>
14
15
16namespace icu {
17	class DateFormat;
18}
19
20
21class BCatalog;
22class BDateFormat;
23class BString;
24class BTimeZone;
25
26
27enum BNumberElement {
28	B_NUMBER_ELEMENT_INVALID = B_BAD_DATA,
29	B_NUMBER_ELEMENT_INTEGER = 0,
30	B_NUMBER_ELEMENT_FRACTIONAL,
31	B_NUMBER_ELEMENT_CURRENCY
32};
33
34
35class BLocale {
36public:
37								BLocale(const BLanguage* language = NULL,
38									const BFormattingConventions* conventions
39										= NULL);
40								BLocale(const BLocale& other);
41								~BLocale();
42
43	static	const BLocale*		Default();
44
45			BLocale&			operator=(const BLocale& other);
46
47			status_t			GetCollator(BCollator* collator) const;
48			status_t			GetLanguage(BLanguage* language) const;
49			status_t			GetFormattingConventions(
50									BFormattingConventions* conventions) const;
51
52			void				SetFormattingConventions(
53									const BFormattingConventions& conventions);
54			void				SetCollator(const BCollator& newCollator);
55			void				SetLanguage(const BLanguage& newLanguage);
56
57			// see definitions in LocaleStrings.h
58			const char*			GetString(uint32 id) const;
59
60								// DateTime
61
62								// TODO: drop some of these once BDateTimeFormat
63								//       has been implemented!
64			ssize_t				FormatDateTime(char* target, size_t maxSize,
65									time_t time, BDateFormatStyle dateStyle,
66									BTimeFormatStyle timeStyle) const;
67			status_t			FormatDateTime(BString* buffer, time_t time,
68									BDateFormatStyle dateStyle,
69									BTimeFormatStyle timeStyle,
70									const BTimeZone* timeZone = NULL) const;
71
72								// numbers
73
74			ssize_t				FormatNumber(char* string, size_t maxSize,
75									double value) const;
76			status_t			FormatNumber(BString* string,
77									double value) const;
78			ssize_t				FormatNumber(char* string, size_t maxSize,
79									int32 value) const;
80			status_t			FormatNumber(BString* string,
81									int32 value) const;
82
83								// monetary
84
85			ssize_t				FormatMonetary(char* string, size_t maxSize,
86									double value) const;
87			status_t			FormatMonetary(BString* string,
88									double value) const;
89
90			// Collator short-hands
91			int					StringCompare(const char* s1,
92									const char* s2) const;
93			int					StringCompare(const BString* s1,
94									const BString* s2) const;
95
96			void				GetSortKey(const char* string,
97									BString* sortKey) const;
98
99private:
100			icu::DateFormat*	_CreateDateFormatter(
101									const BString& format) const;
102			icu::DateFormat*	_CreateTimeFormatter(
103									const BString& format) const;
104
105	mutable	BLocker				fLock;
106			BCollator			fCollator;
107			BFormattingConventions	fConventions;
108			BLanguage			fLanguage;
109};
110
111
112//--- collator short-hands inlines ---
113//	#pragma mark -
114
115inline int
116BLocale::StringCompare(const char* s1, const char* s2) const
117{
118	return fCollator.Compare(s1, s2);
119}
120
121
122inline int
123BLocale::StringCompare(const BString* s1, const BString* s2) const
124{
125	return fCollator.Compare(s1->String(), s2->String());
126}
127
128
129inline void
130BLocale::GetSortKey(const char* string, BString* sortKey) const
131{
132	fCollator.GetSortKey(string, sortKey);
133}
134
135
136#endif	/* _B_LOCALE_H_ */
137