1227825Stheraven/*
2227825Stheraven * Copyright 2003-2012, Haiku, Inc.
3227825Stheraven * Distributed under the terms of the MIT License.
4227825Stheraven */
5227825Stheraven#ifndef _CATALOG_H_
6227825Stheraven#define _CATALOG_H_
7227825Stheraven
8227825Stheraven
9227825Stheraven#include <LocaleRoster.h>
10227825Stheraven#include <Locker.h>
11227825Stheraven#include <SupportDefs.h>
12227825Stheraven#include <String.h>
13227825Stheraven
14227825Stheraven
15227825Stheravenclass BCatalogData;
16227825Stheravenclass BLocale;
17227825Stheravenclass BMessage;
18227825Stheravenstruct entry_ref;
19227825Stheraven
20227825Stheraven
21227825Stheravenclass BCatalog {
22242945Stheravenpublic:
23227825Stheraven								BCatalog();
24232950Stheraven								BCatalog(const entry_ref& catalogOwner,
25232950Stheraven									const char* language = NULL,
26232950Stheraven									uint32 fingerprint = 0);
27227825Stheraven								BCatalog(const char* signature,
28227825Stheraven									const char* language = NULL);
29227825Stheraven
30227825Stheraven	virtual						~BCatalog();
31227825Stheraven
32227825Stheraven			const char*			GetString(const char* string,
33227825Stheraven									const char* context = NULL,
34227825Stheraven									const char* comment = NULL)
35227825Stheraven									__attribute__((format_arg(2)));
36242945Stheraven			const char*			GetString(uint32 id);
37227825Stheraven
38232950Stheraven			status_t			GetData(const char* name, BMessage* msg);
39232950Stheraven			status_t			GetData(uint32 id, BMessage* msg);
40232950Stheraven
41227825Stheraven			status_t			GetSignature(BString* signature);
42227825Stheraven			status_t			GetLanguage(BString* language);
43227825Stheraven			status_t			GetFingerprint(uint32* fingerprint);
44227825Stheraven
45227825Stheraven			status_t			SetTo(const entry_ref& catalogOwner,
46227825Stheraven									const char* language = NULL,
47227825Stheraven									uint32 fingerprint = 0);
48227825Stheraven			status_t			SetTo(const char* signature,
49227825Stheraven									const char* language = NULL);
50227825Stheraven
51227825Stheraven			status_t			InitCheck() const;
52227825Stheraven			int32				CountItems() const;
53227825Stheraven
54227825Stheravenprotected:
55227825Stheraven								BCatalog(const BCatalog&);
56227825Stheraven			const BCatalog&		operator= (const BCatalog&);
57227825Stheraven									// hide assignment and copy-constructor
58227825Stheraven
59227825Stheraven			BCatalogData*		fCatalogData;
60227825Stheraven	mutable	BLocker				fLock;
61227825Stheraven
62227825Stheravenprivate:
63227825Stheraven	friend	class BLocale;
64227825Stheraven	friend	status_t			get_add_on_catalog(BCatalog*, const char*);
65227825Stheraven};
66227825Stheraven
67227825Stheraven
68227825Stheraven#undef B_TRANSLATION_SYSTEM_NAME_CONTEXT
69227825Stheraven#define B_TRANSLATION_SYSTEM_NAME_CONTEXT "System name"
70227825Stheraven
71227825Stheraven
72227825Stheraven#ifndef B_AVOID_TRANSLATION_MACROS
73227825Stheraven// macros for easy catalog-access, define B_AVOID_TRANSLATION_MACROS if
74227825Stheraven// you don't want these (in which case you need to collect the catalog keys
75227825Stheraven// manually, as collectcatkeys won't do it for you):
76227825Stheraven
77227825Stheraven#undef B_TRANSLATION_CONTEXT
78227825Stheraven	// In a single application, several strings (e.g. 'Ok') will be used
79227825Stheraven	// more than once, in different contexts.
80227825Stheraven	// As the application programmer can not know if all translations of
81227825Stheraven	// this string will be the same for all languages, each occurrence of
82227825Stheraven	// the string must be translated on its own.
83227825Stheraven	// Specifying the context explicitly with each string allows the person
84227825Stheraven	// translating a catalog to separate these different occurrences of the
85227825Stheraven	// same string and tell which strings appears in what context of the
86227825Stheraven	// application.
87227825Stheraven	// In order to give the translator a useful hint, the application
88227825Stheraven	// programmer needs to define B_TRANSLATION_CONTEXT with the context he'd
89227825Stheraven	// like to be associated with the strings used in this specifc source file.
90227825Stheraven	// example:
91232950Stheraven	//		#define B_TRANSLATION_CONTEXT "Folder-Window"
92232950Stheraven	// Tip: Use a descriptive name of the class implemented in that
93227825Stheraven	//		source-file.
94232950Stheraven
95227825Stheraven#ifdef B_COLLECTING_CATKEYS
96227825Stheraven
97232950Stheraven// pull in all the macros used when collecting catalog keys.
98232950Stheraven#include <tools/CollectingCatalog.h>
99227825Stheraven
100232950Stheraven#else
101227825Stheraven
102227825Stheraven#ifndef B_CATALOG
103232950Stheraven#define B_CATALOG BLocaleRoster::Default()->GetCatalog()
104232950Stheraven#endif
105227825Stheraven
106232950Stheraven// Translation macros which may be used to shorten translation requests:
107227825Stheraven#undef B_TRANSLATE
108227825Stheraven#define B_TRANSLATE(string) \
109227825Stheraven	B_CATALOG->GetString((string), B_TRANSLATION_CONTEXT)
110227825Stheraven
111232950Stheraven#undef B_TRANSLATE_CONTEXT
112232950Stheraven#define B_TRANSLATE_CONTEXT(string, context) \
113232950Stheraven	B_CATALOG->GetString((string), (context))
114227825Stheraven
115227825Stheraven#undef B_TRANSLATE_COMMENT
116227825Stheraven#define B_TRANSLATE_COMMENT(string, comment) \
117232950Stheraven	B_CATALOG->GetString((string), \
118232950Stheraven		B_TRANSLATION_CONTEXT, (comment))
119232950Stheraven
120227825Stheraven#undef B_TRANSLATE_ALL
121227825Stheraven#define B_TRANSLATE_ALL(string, context, comment) \
122227825Stheraven	B_CATALOG->GetString((string), (context), \
123232950Stheraven		(comment))
124232950Stheraven
125232950Stheraven#undef B_TRANSLATE_ID
126227825Stheraven#define B_TRANSLATE_ID(id) \
127227825Stheraven	B_CATALOG->GetString((id))
128227825Stheraven
129232950Stheraven#undef B_TRANSLATE_SYSTEM_NAME
130232950Stheraven#define B_TRANSLATE_SYSTEM_NAME(string) \
131232950Stheraven	(BLocaleRoster::Default()->IsFilesystemTranslationPreferred() \
132227825Stheraven		? BLocaleRoster::Default()->GetCatalog()->GetString((string), \
133227825Stheraven			B_TRANSLATION_SYSTEM_NAME_CONTEXT) \
134227825Stheraven		: (string))
135232950Stheraven
136232950Stheraven// Translation markers which can be used to mark static strings/IDs which
137232950Stheraven// are used as key for translation requests (at other places in the code).
138227825Stheraven/* Example:
139227825Stheraven		#define B_TRANSLATION_CONTEXT "MyDecentApp-Menu"
140227825Stheraven
141232950Stheraven		static const char* choices[] = {
142232950Stheraven			B_TRANSLATE_MARK("left"),
143232950Stheraven			B_TRANSLATE_MARK("right"),
144227825Stheraven			B_TRANSLATE_MARK("up"),
145227825Stheraven			B_TRANSLATE_MARK("down")
146227825Stheraven		};
147232950Stheraven
148232950Stheraven		void MyClass::AddChoices(BMenu* menu)
149232950Stheraven		{
150227825Stheraven			for (char** ch = choices; *ch != '\0'; ++ch) {
151227825Stheraven				menu->AddItem(
152227825Stheraven					new BMenuItem(
153227825Stheraven						B_TRANSLATE(*ch),
154227825Stheraven						new BMessage(...)
155232950Stheraven					)
156232950Stheraven				);
157232950Stheraven			}
158227825Stheraven		}
159227825Stheraven*/
160227825Stheraven#undef B_TRANSLATE_MARK
161232950Stheraven#define B_TRANSLATE_MARK(string) (string)
162232950Stheraven
163232950Stheraven#undef B_TRANSLATE_MARK_CONTEXT
164227825Stheraven#define B_TRANSLATE_MARK_CONTEXT(string, context) (string)
165227825Stheraven
166227825Stheraven#undef B_TRANSLATE_MARK_COMMENT
167232950Stheraven#define B_TRANSLATE_MARK_COMMENT(string, comment) (string)
168232950Stheraven
169232950Stheraven#undef B_TRANSLATE_MARK_ALL
170227825Stheraven#define B_TRANSLATE_MARK_ALL(string, context, comment) (string)
171227825Stheraven
172227825Stheraven#undef B_TRANSLATE_MARK_ID
173232950Stheraven#define B_TRANSLATE_MARK_ID(id) (id)
174232950Stheraven
175232950Stheraven#undef B_TRANSLATE_MARK_SYSTEM_NAME
176227825Stheraven#define B_TRANSLATE_MARK_SYSTEM_NAME(string) (string)
177227825Stheraven
178227825Stheraven// the same for void contexts:
179232950Stheraven#undef B_TRANSLATE_MARK_VOID
180232950Stheraven#define B_TRANSLATE_MARK_VOID(string)
181232950Stheraven
182227825Stheraven#undef B_TRANSLATE_MARK_CONTEXT_VOID
183227825Stheraven#define B_TRANSLATE_MARK_CONTEXT_VOID(string, context)
184227825Stheraven
185232950Stheraven#undef B_TRANSLATE_MARK_COMMENT_VOID
186232950Stheraven#define B_TRANSLATE_MARK_COMMENT_VOID(string, comment)
187232950Stheraven
188227825Stheraven#undef B_TRANSLATE_MARK_ALL_VOID
189227825Stheraven#define B_TRANSLATE_MARK_ALL_VOID(string, context, comment)
190227825Stheraven
191232950Stheraven#undef B_TRANSLATE_MARK_ID_VOID
192232950Stheraven#define B_TRANSLATE_MARK_ID_VOID(id)
193232950Stheraven
194227825Stheraven#undef B_TRANSLATE_MARK_SYSTEM_NAME_VOID
195227825Stheraven#define B_TRANSLATE_MARK_SYSTEM_NAME_VOID(string)
196227825Stheraven
197227825Stheraven// Translation macros which cause collectcatkeys to ignore this key
198227825Stheraven// (useful in combination with the marking macros above):
199232950Stheraven#undef B_TRANSLATE_NOCOLLECT
200232950Stheraven#define B_TRANSLATE_NOCOLLECT(string) \
201227825Stheraven	B_TRANSLATE(string)
202232950Stheraven
203227825Stheraven#undef B_TRANSLATE_NOCOLLECT_COMMENT
204227825Stheraven#define B_TRANSLATE_NOCOLLECT_COMMENT(string, comment) \
205232950Stheraven	B_TRANSLATE_COMMENT(string, comment)
206232950Stheraven
207227825Stheraven#undef B_TRANSLATE_NOCOLLECT_ALL
208232950Stheraven#define B_TRANSLATE_NOCOLLECT_ALL(string, context, comment) \
209227825Stheraven	B_TRANSLATE_ALL(string, context, comment)
210227825Stheraven
211232950Stheraven#undef B_TRANSLATE_NOCOLLECT_ID
212232950Stheraven#define B_TRANSLATE_NOCOLLECT_ID(id) \
213227825Stheraven	B_TRANSLATE_ID(id)
214232950Stheraven
215227825Stheraven#undef B_TRANSLATE_NOCOLLECT_SYSTEM_NAME
216227825Stheraven#define B_TRANSLATE_NOCOLLECT_SYSTEM_NAME(string) \
217232950Stheraven	B_TRANSLATE_SYSTEM_NAME(string)
218232950Stheraven
219227825Stheraven#endif	/* B_COLLECTING_CATKEYS */
220232950Stheraven
221227825Stheraven#endif	/* B_AVOID_TRANSLATION_MACROS */
222227825Stheraven
223232950Stheraven
224232950Stheraven#endif /* _CATALOG_H_ */
225227825Stheraven