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