xref: /haiku/docs/user/locale/Catalog.dox (revision defdefa89a75167b2c53acc6b5aaadf851ce56ee)
1a33f8fbdSAdrien Destugues/*
2820dca4dSJohn Scipione * Copyright 2011-2012 Haiku, Inc. All rights reserved.
3a33f8fbdSAdrien Destugues * Distributed under the terms of the MIT License.
4a33f8fbdSAdrien Destugues *
5a33f8fbdSAdrien Destugues * Authors:
6a33f8fbdSAdrien Destugues *		Axel Dörfler, axeld@pinc-software.de
7a33f8fbdSAdrien Destugues *		John Scipione, jscipione@gmail.com
8a33f8fbdSAdrien Destugues *		Oliver Tappe, zooey@hirschkaefer.de
9a33f8fbdSAdrien Destugues *
10a33f8fbdSAdrien Destugues * Corresponds to:
11820dca4dSJohn Scipione *		headers/os/locale/Catalog.h	 hrev45083
12820dca4dSJohn Scipione *		src/kits/locale/Catalog.cpp	 hrev45083
13a33f8fbdSAdrien Destugues */
14a33f8fbdSAdrien Destugues
15a33f8fbdSAdrien Destugues
16a33f8fbdSAdrien Destugues/*!
17a33f8fbdSAdrien Destugues	\file Catalog.h
18820dca4dSJohn Scipione	\ingroup locale
19820dca4dSJohn Scipione	\ingroup libbe
20a33f8fbdSAdrien Destugues	\brief Provides the BCatalog class.
21a33f8fbdSAdrien Destugues*/
22a33f8fbdSAdrien Destugues
23a33f8fbdSAdrien Destugues
24e9fd63ecSAdrien Destugues/*!
25e9fd63ecSAdrien Destugues	\class BCatalog
26e9fd63ecSAdrien Destugues	\ingroup locale
2789dfe27fSJohn Scipione	\ingroup libbe
2889dfe27fSJohn Scipione	\brief String localization handling.
29e9fd63ecSAdrien Destugues
30a33f8fbdSAdrien Destugues	BCatalog is the class that allows you to perform string localization. This
31a33f8fbdSAdrien Destugues	means you give it a string in english, and it automatically returns the
32a33f8fbdSAdrien Destugues	translation of this string in the user's specified language, if available.
33e9fd63ecSAdrien Destugues
34a33f8fbdSAdrien Destugues	Most of the time, you don't have to deal with BCatalog directly. You use
35a33f8fbdSAdrien Destugues	the translation macros instead. However, there are some cases where you
36a33f8fbdSAdrien Destugues	will have to use catalogs directly. These include :
3789dfe27fSJohn Scipione	- Tools for managing catalogs : if you want to add, remove or edit
38e9fd63ecSAdrien Destugues	  entries in a catalog, you need to do it using the BCatalog class.
3989dfe27fSJohn Scipione	- Accessing catalogs other than your own : the macros only grant you
40a33f8fbdSAdrien Destugues	  access to the catalog linked with your application. To access
41a33f8fbdSAdrien Destugues	  other catalogs (for example if you create a script interpreter and
42a33f8fbdSAdrien Destugues	  want to localize the scripts), you will have to open a catalog
43a33f8fbdSAdrien Destugues	  associated with your script.
44e9fd63ecSAdrien Destugues
455393193dSAdrien Destugues	\section macros Using the macros
46*defdefa8SAkshay Agarwal
47a33f8fbdSAdrien Destugues	You don't have to do much in your program to handle catalogs. You must
48546208a5SOliver Tappe	first set the B_TRANSLATION_CONTEXT define to a string that identifies which
49a33f8fbdSAdrien Destugues	part of the application the strings you will translate are in. This allows
50a33f8fbdSAdrien Destugues	the translators to keep track of the strings in the catalog more easily,
51a33f8fbdSAdrien Destugues	and find where they are visible in the application. then, all you have to
52a33f8fbdSAdrien Destugues	do, is enclose any string you want to make translatable in the
53a33f8fbdSAdrien Destugues	B_TRANSLATE() macro. This macro has two uses, it will allow your text to
54a33f8fbdSAdrien Destugues	be replaced at run-time by the proper localized one, but it will also
55a33f8fbdSAdrien Destugues	allow to build the base catalog, the one that you will send to the
56a33f8fbdSAdrien Destugues	translator team, from your sourcecode.
57e9fd63ecSAdrien Destugues
58*defdefa8SAkshay Agarwal	Note that each image (application, library or add-on) using these macros
59*defdefa8SAkshay Agarwal	must be linked with liblocalestub.a. This allows the Locale Kit to identify
60*defdefa8SAkshay Agarwal	it and locate the matching string catalogs for translation.
61*defdefa8SAkshay Agarwal
625393193dSAdrien Destugues	\section chaining Chaining of catalogs
63*defdefa8SAkshay Agarwal
64a33f8fbdSAdrien Destugues	The catalogs you get from the locale kit are designed to use a fallback
65a33f8fbdSAdrien Destugues	system so that the user get strings in the language he's the most fluent
66a33f8fbdSAdrien Destugues	with, depending on what catalogs are available.
67c6247544SAdrien Destugues
68*defdefa8SAkshay Agarwal	For example, if the user sets his language preferences as French(France),
69a33f8fbdSAdrien Destugues	spanish, english, when an application loads a catalog, the following rules
70a33f8fbdSAdrien Destugues	are used:
71*defdefa8SAkshay Agarwal	- Try to load a French(France) catalog. If it is found, this catalog
72c6247544SAdrien Destugues	  will automatically include strings from the generic french catalog.
7389dfe27fSJohn Scipione	- Try to load a generic french catalog.
7489dfe27fSJohn Scipione	- Try to load a generic spanish catalog.
7589dfe27fSJohn Scipione	- Try to load a generic english catalog.
7689dfe27fSJohn Scipione	- If all of them failed, use the strings that are in the source code.
77c6247544SAdrien Destugues
78*defdefa8SAkshay Agarwal	Note that French(France) will failback to French, but then directly to the
79a33f8fbdSAdrien Destugues	language in the source code. This avoids mixing 3 or more languages in the
80a33f8fbdSAdrien Destugues	same application if the catalogs are incomplete and avoids confusion.
81edc845a3SJohn Scipione
82edc845a3SJohn Scipione	\since Haiku R1
83e9fd63ecSAdrien Destugues*/
84e9fd63ecSAdrien Destugues
85a33f8fbdSAdrien Destugues
86e9fd63ecSAdrien Destugues/*!
8789dfe27fSJohn Scipione	\fn BCatalog::BCatalog()
8889dfe27fSJohn Scipione	\brief Construct an empty BCatalog object.
8989dfe27fSJohn Scipione
9089dfe27fSJohn Scipione	Should be followed by SetTo() method to set the catalog.
91edc845a3SJohn Scipione
92edc845a3SJohn Scipione	\since Haiku R1
9389dfe27fSJohn Scipione*/
9489dfe27fSJohn Scipione
9589dfe27fSJohn Scipione
9689dfe27fSJohn Scipione/*!
9789dfe27fSJohn Scipione	\fn BCatalog::BCatalog(const entry_ref& catalogOwner, const char* language,
9889dfe27fSJohn Scipione		uint32 fingerprint)
9989dfe27fSJohn Scipione	\brief Construct a BCatalog object for the given \a catalogOwner.
100e9fd63ecSAdrien Destugues
101e9fd63ecSAdrien Destugues	If you don't specify a language, the system default list will be used.
102e9fd63ecSAdrien Destugues	The language is passed here as a 2 letter ISO code.
103e9fd63ecSAdrien Destugues
104a33f8fbdSAdrien Destugues	The fingerprint is a way to check that the catalog that will be loaded
105a33f8fbdSAdrien Destugues	matches the current version of the application. A catalog made for a
106a33f8fbdSAdrien Destugues	different version of the application can be loaded if you set the
107a33f8fbdSAdrien Destugues	fingerprint to \c 0. This is usually not a problem, it only means that
108a33f8fbdSAdrien Destugues	some strings may not be translated properly. But if you want to provide
109a33f8fbdSAdrien Destugues	different versions of your application, it may be useful to separate their
110a33f8fbdSAdrien Destugues	catalogs.
111e9fd63ecSAdrien Destugues
1126ac7032dSJohn Scipione	\param catalogOwner entry_ref for which to load a catalog.
1136ac7032dSJohn Scipione	\param language The language of the catalog to load. If \c NULL, the user
114a33f8fbdSAdrien Destugues	       settings will be used.
115a33f8fbdSAdrien Destugues	\param fingerprint The fingerprint version-info for the catalog to load.
116a33f8fbdSAdrien Destugues	       If \c 0, the fingerprint will not be checked,and any version of the
117a33f8fbdSAdrien Destugues	       catalog will be loaded.
118edc845a3SJohn Scipione
119edc845a3SJohn Scipione	\since Haiku R1
120e9fd63ecSAdrien Destugues*/
121e9fd63ecSAdrien Destugues
122a33f8fbdSAdrien Destugues
123e9fd63ecSAdrien Destugues/*!
12489dfe27fSJohn Scipione	\fn BCatalog::~BCatalog()
12589dfe27fSJohn Scipione	\brief Destroys the BCatalog object freeing memory used by it.
126edc845a3SJohn Scipione
127edc845a3SJohn Scipione	\since Haiku R1
12889dfe27fSJohn Scipione*/
12989dfe27fSJohn Scipione
13089dfe27fSJohn Scipione
13189dfe27fSJohn Scipione/*!
132a33f8fbdSAdrien Destugues	\fn const char* BCatalog::GetString(const char* string,
13389dfe27fSJohn Scipione		const char* context, const char* comment)
134c6247544SAdrien Destugues	\brief Get a string from the catalog.
135e9fd63ecSAdrien Destugues
136edc845a3SJohn Scipione	This method access the data of the catalog and returns you the translated
137e9fd63ecSAdrien Destugues	version of the string. You must pass it the context where the string is, as
138edc845a3SJohn Scipione	the same string may appear somewhere else and need a different translation.
139a33f8fbdSAdrien Destugues	The comment is optional. It is meant as an help to translators, when the
140a33f8fbdSAdrien Destugues	string alone is not helpful enough or there are special things to note.
141a33f8fbdSAdrien Destugues	The comment is also used as a way to uniquely identify a string, so if two
142a33f8fbdSAdrien Destugues	identical strings share the same context, it is still possible to provide
143a33f8fbdSAdrien Destugues	different translations.
144e9fd63ecSAdrien Destugues
145e82e8f36SAdrien Destugues	\param string The string to translate.
146e82e8f36SAdrien Destugues	\param context The context where the string is located.
147e82e8f36SAdrien Destugues	\param comment Supplementary comment for translators.
148a33f8fbdSAdrien Destugues
149a33f8fbdSAdrien Destugues	\returns The translated string, or the one passed as a parameter if no
150a33f8fbdSAdrien Destugues	         translation was found.
151edc845a3SJohn Scipione
152edc845a3SJohn Scipione	\since Haiku R1
153e82e8f36SAdrien Destugues*/
154e82e8f36SAdrien Destugues
155a33f8fbdSAdrien Destugues
156e82e8f36SAdrien Destugues/*!
157cae874d3SAdrien Destugues	\fn const char* BCatalog::GetString(uint32 id)
158e82e8f36SAdrien Destugues	\brief Get a string by id from the catalog.
159e82e8f36SAdrien Destugues
160a33f8fbdSAdrien Destugues	The id based version of this method is slightly faster, as it doesn't
161a33f8fbdSAdrien Destugues	have to compute the hash from the 3 parameters. However, it will fail
162a33f8fbdSAdrien Destugues	if there is an hash collision, so you should still fallback to the first
163a33f8fbdSAdrien Destugues	one in case of problems. Also note that the hash value may be different
164a33f8fbdSAdrien Destugues	from one catalog to another, depending on the file format they are stored
165a33f8fbdSAdrien Destugues	in, so you shouldn't rely on this method unless you are sure you can keep
166a33f8fbdSAdrien Destugues	all the catalog files under control.
167e9fd63ecSAdrien Destugues
168e82e8f36SAdrien Destugues	\param id The identifier of the string.
16989dfe27fSJohn Scipione
170a33f8fbdSAdrien Destugues	\returns The translated string if found, or an empty string.
171edc845a3SJohn Scipione
172edc845a3SJohn Scipione	\since Haiku R1
173c6247544SAdrien Destugues*/
174c6247544SAdrien Destugues
175c6247544SAdrien Destugues
176c6247544SAdrien Destugues/*!
177c6247544SAdrien Destugues	\fn status_t BCatalog::GetData(const char* name, BMessage* msg)
178c6247544SAdrien Destugues	\brief Get custom data from the catalog.
179c6247544SAdrien Destugues
1806ac7032dSJohn Scipione	This method allows you to localize something else than raw text. This
181a33f8fbdSAdrien Destugues	may include pictures, sounds, videos, or anything else. Note there is no
182a33f8fbdSAdrien Destugues	support for generating a catalog with such data inside, and the current
183a33f8fbdSAdrien Destugues	format may not support it. If you need to localize data that is not text,
184a33f8fbdSAdrien Destugues	it is advised to handle it by yourself.
185c6247544SAdrien Destugues
186e82e8f36SAdrien Destugues	\param name The name of the data to retrieve.
187e82e8f36SAdrien Destugues	\param msg The BMessage to fill in with the data.
188a33f8fbdSAdrien Destugues
18989dfe27fSJohn Scipione	\returns A status code.
19089dfe27fSJohn Scipione	\retval B_OK Everything went fine.
19189dfe27fSJohn Scipione	\retval B_ERROR Unable to get an exclusive lock on data.
19289dfe27fSJohn Scipione	\retval B_NO_INIT Catalog is \c NULL.
19389dfe27fSJohn Scipione	\retval B_NAME_NOT_FOUND catalog with the specified \a name could not be
19489dfe27fSJohn Scipione	        found.
195edc845a3SJohn Scipione
196edc845a3SJohn Scipione	\since Haiku R1
197c6247544SAdrien Destugues*/
198c6247544SAdrien Destugues
199a33f8fbdSAdrien Destugues
200c6247544SAdrien Destugues/*!
201cae874d3SAdrien Destugues	\fn status_t BCatalog::GetData(uint32 id, BMessage* msg)
202cae874d3SAdrien Destugues	\brief Get custom data from the catalog.
203cae874d3SAdrien Destugues
204a33f8fbdSAdrien Destugues	As for GetString, the id-based version may be subject to hash-collisions,
205a33f8fbdSAdrien Destugues	but is faster.
206cae874d3SAdrien Destugues
207a33f8fbdSAdrien Destugues	Note the current catalog format doesn't allow storing custom data in
2086ac7032dSJohn Scipione	catalogs, so the only way to use this method is providing your own
209a33f8fbdSAdrien Destugues	catalog add-on for storing the data.
210edc845a3SJohn Scipione
211edc845a3SJohn Scipione	\since Haiku R1
212cae874d3SAdrien Destugues*/
213cae874d3SAdrien Destugues
214a33f8fbdSAdrien Destugues
215cae874d3SAdrien Destugues/*!
216c6247544SAdrien Destugues	\fn status_t BCatalog::GetSignature(BString* sig)
217c6247544SAdrien Destugues	\brief Get the catalog mime-signature.
218c6247544SAdrien Destugues
2196ac7032dSJohn Scipione	This method fills the sig string with the mime-signature associated to the
220c6247544SAdrien Destugues	catalog.
221c6247544SAdrien Destugues
222e82e8f36SAdrien Destugues	\param sig The string where to copy the signature.
223a33f8fbdSAdrien Destugues
224e82e8f36SAdrien Destugues	\returns An error code.
225edc845a3SJohn Scipione
226edc845a3SJohn Scipione	\since Haiku R1
227c6247544SAdrien Destugues*/
228c6247544SAdrien Destugues
229a33f8fbdSAdrien Destugues
230c6247544SAdrien Destugues/*!
231c6247544SAdrien Destugues	\fn status_t BCatalog::GetLanguage(BString* lang)
232c6247544SAdrien Destugues	\brief Get the catalog language.
233c6247544SAdrien Destugues
2346ac7032dSJohn Scipione	This method fills the lang string with the language name for the catalog.
235e82e8f36SAdrien Destugues
236a33f8fbdSAdrien Destugues	\param lang The string where to copy the language.
237a33f8fbdSAdrien Destugues
238e82e8f36SAdrien Destugues	\returns An error code.
23989dfe27fSJohn Scipione	\retval B_OK Everything went as expected.
24089dfe27fSJohn Scipione	\retval B_ERROR Could not get exclusive lock on catalog.
24189dfe27fSJohn Scipione	\retval B_BAD_VALUE \a lang is \c NULL.
24289dfe27fSJohn Scipione	\retval B_NO_INIT Catalog data is \c NULL.
243edc845a3SJohn Scipione
244edc845a3SJohn Scipione	\since Haiku R1
245c6247544SAdrien Destugues*/
246c6247544SAdrien Destugues
247a33f8fbdSAdrien Destugues
248c6247544SAdrien Destugues/*!
249c6247544SAdrien Destugues	\fn status_t BCatalog::GetFingerprint(uint32* fp)
250c6247544SAdrien Destugues	\brief Get the catalog fingerprint.
251c6247544SAdrien Destugues
2526ac7032dSJohn Scipione	This method setsfp to the fingerprint of the catalog. This allows you
253a33f8fbdSAdrien Destugues	to check which version of the sourcecode this catalog was generated from.
254a33f8fbdSAdrien Destugues
255a33f8fbdSAdrien Destugues	\param fp The integer to set to the fingerprint value.
256e82e8f36SAdrien Destugues
257e82e8f36SAdrien Destugues	\returns An error code.
25889dfe27fSJohn Scipione	\retval B_OK Everything went as expected.
25989dfe27fSJohn Scipione	\retval B_ERROR Could not get exclusive lock on catalog.
26089dfe27fSJohn Scipione	\retval B_BAD_VALUE \a fp is \c NULL.
26189dfe27fSJohn Scipione	\retval B_NO_INIT Catalog data is \c NULL.
262edc845a3SJohn Scipione
263edc845a3SJohn Scipione	\since Haiku R1
264c6247544SAdrien Destugues*/
265c6247544SAdrien Destugues
266a33f8fbdSAdrien Destugues
267c6247544SAdrien Destugues/*!
26889dfe27fSJohn Scipione	\fn status_t BCatalog::SetTo(const entry_ref& catalogOwner,
26989dfe27fSJohn Scipione		const char* language, uint32 fingerprint)
270c6247544SAdrien Destugues	\brief Reload the string data.
271c6247544SAdrien Destugues
2726ac7032dSJohn Scipione	This method reloads the data for the given signature and fingerprint.
273e82e8f36SAdrien Destugues
27489dfe27fSJohn Scipione	\param catalogOwner The \c entry_ref of the catalog that you want to load.
27589dfe27fSJohn Scipione	\param language The language of the catalog to load. If \c NULL, the user
27689dfe27fSJohn Scipione	       settings will be used.
277e82e8f36SAdrien Destugues	\param fingerprint The fingerprint of the catalog you want to load.
278a33f8fbdSAdrien Destugues
27989dfe27fSJohn Scipione	\returns A status code, \c B_OK on success, \c B_ERROR on error.
280edc845a3SJohn Scipione
281edc845a3SJohn Scipione	\since Haiku R1
282c6247544SAdrien Destugues*/
283c6247544SAdrien Destugues
284a33f8fbdSAdrien Destugues
285c6247544SAdrien Destugues/*!
286cae874d3SAdrien Destugues	\fn status_t BCatalog::InitCheck() const
28789dfe27fSJohn Scipione	\brief Check if the catalog is in a valid and usable state.
288c6247544SAdrien Destugues
28989dfe27fSJohn Scipione	\returns A status code.
29089dfe27fSJohn Scipione	\retval B_OK The catalog is initialized properly.
29189dfe27fSJohn Scipione	\retval B_ERROR Could not get exclusive lock on catalog.
29289dfe27fSJohn Scipione	\retval B_NO_INIT Catalog data is \c NULL.
293edc845a3SJohn Scipione
294edc845a3SJohn Scipione	\since Haiku R1
295c6247544SAdrien Destugues*/
296c6247544SAdrien Destugues
297a33f8fbdSAdrien Destugues
298c6247544SAdrien Destugues/*!
2996ec9625aSAdrien Destugues	\fn int32 BCatalog::CountItems() const
30089dfe27fSJohn Scipione	\brief Gets the number of items in the catalog.
301c6247544SAdrien Destugues
30289dfe27fSJohn Scipione	\returns the number of strings in the catalog or 0 on error.
303edc845a3SJohn Scipione
304edc845a3SJohn Scipione	\since Haiku R1
305c6247544SAdrien Destugues*/
306