xref: /haiku/headers/os/locale/Catalog.h (revision bb83316a5811a550c4f850d07fa8e328e7ac0a94)
1 /*
2  * Copyright 2003-2012, Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _CATALOG_H_
6 #define _CATALOG_H_
7 
8 
9 #include <LocaleRoster.h>
10 #include <Locker.h>
11 #include <SupportDefs.h>
12 #include <String.h>
13 
14 
15 class BCatalogData;
16 class BLocale;
17 class BMessage;
18 struct entry_ref;
19 
20 
21 class BCatalog {
22 public:
23 								BCatalog();
24 								BCatalog(const entry_ref& catalogOwner,
25 									const char* language = NULL,
26 									uint32 fingerprint = 0);
27 								BCatalog(const char* signature,
28 									const char* language = NULL);
29 
30 	virtual						~BCatalog();
31 
32 			const char*			GetString(const char* string,
33 									const char* context = NULL,
34 									const char* comment = NULL);
35 			const char*			GetString(uint32 id);
36 
37 			status_t			GetData(const char* name, BMessage* msg);
38 			status_t			GetData(uint32 id, BMessage* msg);
39 
40 			status_t			GetSignature(BString* signature);
41 			status_t			GetLanguage(BString* language);
42 			status_t			GetFingerprint(uint32* fingerprint);
43 
44 			status_t			SetTo(const entry_ref& catalogOwner,
45 									const char* language = NULL,
46 									uint32 fingerprint = 0);
47 			status_t			SetTo(const char* signature,
48 									const char* language = NULL);
49 
50 			status_t			InitCheck() const;
51 			int32				CountItems() const;
52 
53 protected:
54 								BCatalog(const BCatalog&);
55 			const BCatalog&		operator= (const BCatalog&);
56 									// hide assignment and copy-constructor
57 
58 			BCatalogData*		fCatalogData;
59 	mutable	BLocker				fLock;
60 
61 private:
62 	friend	class BLocale;
63 	friend	status_t			get_add_on_catalog(BCatalog*, const char*);
64 };
65 
66 
67 #undef B_TRANSLATION_SYSTEM_NAME_CONTEXT
68 #define B_TRANSLATION_SYSTEM_NAME_CONTEXT "System name"
69 
70 
71 #ifndef B_AVOID_TRANSLATION_MACROS
72 // macros for easy catalog-access, define B_AVOID_TRANSLATION_MACROS if
73 // you don't want these (in which case you need to collect the catalog keys
74 // manually, as collectcatkeys won't do it for you):
75 
76 #undef B_TRANSLATION_CONTEXT
77 	// In a single application, several strings (e.g. 'Ok') will be used
78 	// more than once, in different contexts.
79 	// As the application programmer can not know if all translations of
80 	// this string will be the same for all languages, each occurrence of
81 	// the string must be translated on its own.
82 	// Specifying the context explicitly with each string allows the person
83 	// translating a catalog to separate these different occurrences of the
84 	// same string and tell which strings appears in what context of the
85 	// application.
86 	// In order to give the translator a useful hint, the application
87 	// programmer needs to define B_TRANSLATION_CONTEXT with the context he'd
88 	// like to be associated with the strings used in this specifc source file.
89 	// example:
90 	//		#define B_TRANSLATION_CONTEXT "Folder-Window"
91 	// Tip: Use a descriptive name of the class implemented in that
92 	//		source-file.
93 
94 #ifdef B_COLLECTING_CATKEYS
95 
96 // pull in all the macros used when collecting catalog keys.
97 #include <tools/CollectingCatalog.h>
98 
99 #else
100 
101 #ifndef B_CATALOG
102 #define B_CATALOG BLocaleRoster::Default()->GetCatalog()
103 #endif
104 
105 // Translation macros which may be used to shorten translation requests:
106 #undef B_TRANSLATE
107 #define B_TRANSLATE(string) \
108 	B_CATALOG->GetString((string), B_TRANSLATION_CONTEXT)
109 
110 #undef B_TRANSLATE_CONTEXT
111 #define B_TRANSLATE_CONTEXT(string, context) \
112 	B_CATALOG->GetString((string), (context))
113 
114 #undef B_TRANSLATE_COMMENT
115 #define B_TRANSLATE_COMMENT(string, comment) \
116 	B_CATALOG->GetString((string), \
117 		B_TRANSLATION_CONTEXT, (comment))
118 
119 #undef B_TRANSLATE_ALL
120 #define B_TRANSLATE_ALL(string, context, comment) \
121 	B_CATALOG->GetString((string), (context), \
122 		(comment))
123 
124 #undef B_TRANSLATE_ID
125 #define B_TRANSLATE_ID(id) \
126 	B_CATALOG->GetString((id))
127 
128 #undef B_TRANSLATE_SYSTEM_NAME
129 #define B_TRANSLATE_SYSTEM_NAME(string) \
130 	(BLocaleRoster::Default()->IsFilesystemTranslationPreferred() \
131 		? BLocaleRoster::Default()->GetCatalog()->GetString((string), \
132 			B_TRANSLATION_SYSTEM_NAME_CONTEXT) \
133 		: (string))
134 
135 // Translation markers which can be used to mark static strings/IDs which
136 // are used as key for translation requests (at other places in the code).
137 /* Example:
138 		#define B_TRANSLATION_CONTEXT "MyDecentApp-Menu"
139 
140 		static const char* choices[] = {
141 			B_TRANSLATE_MARK("left"),
142 			B_TRANSLATE_MARK("right"),
143 			B_TRANSLATE_MARK("up"),
144 			B_TRANSLATE_MARK("down")
145 		};
146 
147 		void MyClass::AddChoices(BMenu* menu)
148 		{
149 			for (char** ch = choices; *ch != '\0'; ++ch) {
150 				menu->AddItem(
151 					new BMenuItem(
152 						B_TRANSLATE(*ch),
153 						new BMessage(...)
154 					)
155 				);
156 			}
157 		}
158 */
159 #undef B_TRANSLATE_MARK
160 #define B_TRANSLATE_MARK(string) (string)
161 
162 #undef B_TRANSLATE_MARK_CONTEXT
163 #define B_TRANSLATE_MARK_CONTEXT(string, context) (string)
164 
165 #undef B_TRANSLATE_MARK_COMMENT
166 #define B_TRANSLATE_MARK_COMMENT(string, comment) (string)
167 
168 #undef B_TRANSLATE_MARK_ALL
169 #define B_TRANSLATE_MARK_ALL(string, context, comment) (string)
170 
171 #undef B_TRANSLATE_MARK_ID
172 #define B_TRANSLATE_MARK_ID(id) (id)
173 
174 #undef B_TRANSLATE_MARK_SYSTEM_NAME
175 #define B_TRANSLATE_MARK_SYSTEM_NAME(string) (string)
176 
177 // the same for void contexts:
178 #undef B_TRANSLATE_MARK_VOID
179 #define B_TRANSLATE_MARK_VOID(string)
180 
181 #undef B_TRANSLATE_MARK_CONTEXT_VOID
182 #define B_TRANSLATE_MARK_CONTEXT_VOID(string, context)
183 
184 #undef B_TRANSLATE_MARK_COMMENT_VOID
185 #define B_TRANSLATE_MARK_COMMENT_VOID(string, comment)
186 
187 #undef B_TRANSLATE_MARK_ALL_VOID
188 #define B_TRANSLATE_MARK_ALL_VOID(string, context, comment)
189 
190 #undef B_TRANSLATE_MARK_ID_VOID
191 #define B_TRANSLATE_MARK_ID_VOID(id)
192 
193 #undef B_TRANSLATE_MARK_SYSTEM_NAME_VOID
194 #define B_TRANSLATE_MARK_SYSTEM_NAME_VOID(string)
195 
196 // Translation macros which cause collectcatkeys to ignore this key
197 // (useful in combination with the marking macros above):
198 #undef B_TRANSLATE_NOCOLLECT
199 #define B_TRANSLATE_NOCOLLECT(string) \
200 	B_TRANSLATE(string)
201 
202 #undef B_TRANSLATE_NOCOLLECT_COMMENT
203 #define B_TRANSLATE_NOCOLLECT_COMMENT(string, comment) \
204 	B_TRANSLATE_COMMENT(string, comment)
205 
206 #undef B_TRANSLATE_NOCOLLECT_ALL
207 #define B_TRANSLATE_NOCOLLECT_ALL(string, context, comment) \
208 	B_TRANSLATE_ALL(string, context, comment)
209 
210 #undef B_TRANSLATE_NOCOLLECT_ID
211 #define B_TRANSLATE_NOCOLLECT_ID(id) \
212 	B_TRANSLATE_ID(id)
213 
214 #undef B_TRANSLATE_NOCOLLECT_SYSTEM_NAME
215 #define B_TRANSLATE_NOCOLLECT_SYSTEM_NAME(string) \
216 	B_TRANSLATE_SYSTEM_NAME(string)
217 
218 #endif	/* B_COLLECTING_CATKEYS */
219 
220 #endif	/* B_AVOID_TRANSLATION_MACROS */
221 
222 
223 #endif /* _CATALOG_H_ */
224