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: 1141853a8bSAdrien Destugues * headers/os/locale/Catalog.h hrev54533 1241853a8bSAdrien Destugues * src/kits/locale/Catalog.cpp hrev54533 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 46defdefa8SAkshay 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 58defdefa8SAkshay Agarwal Note that each image (application, library or add-on) using these macros 59defdefa8SAkshay Agarwal must be linked with liblocalestub.a. This allows the Locale Kit to identify 60defdefa8SAkshay Agarwal it and locate the matching string catalogs for translation. 61defdefa8SAkshay Agarwal 625393193dSAdrien Destugues \section chaining Chaining of catalogs 63defdefa8SAkshay 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 68defdefa8SAkshay 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: 71defdefa8SAkshay 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 78defdefa8SAkshay 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 11241853a8bSAdrien Destugues \param catalogOwner entry_ref or application, add-on or library for which 11341853a8bSAdrien Destugues to load a catalog. 1146ac7032dSJohn Scipione \param language The language of the catalog to load. If \c NULL, the user 115a33f8fbdSAdrien Destugues settings will be used. 116a33f8fbdSAdrien Destugues \param fingerprint The fingerprint version-info for the catalog to load. 117a33f8fbdSAdrien Destugues If \c 0, the fingerprint will not be checked,and any version of the 118a33f8fbdSAdrien Destugues catalog will be loaded. 119edc845a3SJohn Scipione 120edc845a3SJohn Scipione \since Haiku R1 121e9fd63ecSAdrien Destugues*/ 122e9fd63ecSAdrien Destugues 123a33f8fbdSAdrien Destugues 124e9fd63ecSAdrien Destugues/*! 12541853a8bSAdrien Destugues \fn BCatalog::BCatalog(const char* signature, const char* language) 126*edbeede4SAnarchos \brief Construct a BCatalog object for the given application \a signature. 12741853a8bSAdrien Destugues 12841853a8bSAdrien Destugues If you don't specify a language, the system default list will be used. 12941853a8bSAdrien Destugues The language is passed here as a 2 letter ISO code. 13041853a8bSAdrien Destugues 13141853a8bSAdrien Destugues This constructor is used to load catalogs that are not related to an 13241853a8bSAdrien Destugues executable or library file (so there is no entry_ref usable to identify the 13341853a8bSAdrien Destugues catalog). As it uses only the MIME signature, it cannot load catalogs from 13441853a8bSAdrien Destugues application resources or a catalog file located next to the application. 13541853a8bSAdrien Destugues Only the catalogs in the standard system directories (data/locale/catalogs) 13641853a8bSAdrien Destugues are checked. Moreover, only the default catalog format is available, not 13741853a8bSAdrien Destugues custom formats from catalog add-ons. 13841853a8bSAdrien Destugues 139*edbeede4SAnarchos \param signature MIME signature for which to load a catalog. 14041853a8bSAdrien Destugues \param language The language of the catalog to load. If \c NULL, the user 14141853a8bSAdrien Destugues settings will be used. 14241853a8bSAdrien Destugues 14341853a8bSAdrien Destugues \since Haiku R1 14441853a8bSAdrien Destugues*/ 14541853a8bSAdrien Destugues 14641853a8bSAdrien Destugues 14741853a8bSAdrien Destugues/*! 14889dfe27fSJohn Scipione \fn BCatalog::~BCatalog() 14989dfe27fSJohn Scipione \brief Destroys the BCatalog object freeing memory used by it. 150edc845a3SJohn Scipione 151edc845a3SJohn Scipione \since Haiku R1 15289dfe27fSJohn Scipione*/ 15389dfe27fSJohn Scipione 15489dfe27fSJohn Scipione 15589dfe27fSJohn Scipione/*! 156a33f8fbdSAdrien Destugues \fn const char* BCatalog::GetString(const char* string, 15789dfe27fSJohn Scipione const char* context, const char* comment) 158c6247544SAdrien Destugues \brief Get a string from the catalog. 159e9fd63ecSAdrien Destugues 160edc845a3SJohn Scipione This method access the data of the catalog and returns you the translated 161e9fd63ecSAdrien Destugues version of the string. You must pass it the context where the string is, as 162edc845a3SJohn Scipione the same string may appear somewhere else and need a different translation. 163a33f8fbdSAdrien Destugues The comment is optional. It is meant as an help to translators, when the 164a33f8fbdSAdrien Destugues string alone is not helpful enough or there are special things to note. 165a33f8fbdSAdrien Destugues The comment is also used as a way to uniquely identify a string, so if two 166a33f8fbdSAdrien Destugues identical strings share the same context, it is still possible to provide 167a33f8fbdSAdrien Destugues different translations. 168e9fd63ecSAdrien Destugues 169e82e8f36SAdrien Destugues \param string The string to translate. 170e82e8f36SAdrien Destugues \param context The context where the string is located. 171e82e8f36SAdrien Destugues \param comment Supplementary comment for translators. 172a33f8fbdSAdrien Destugues 173a33f8fbdSAdrien Destugues \returns The translated string, or the one passed as a parameter if no 174a33f8fbdSAdrien Destugues translation was found. 175edc845a3SJohn Scipione 176edc845a3SJohn Scipione \since Haiku R1 177e82e8f36SAdrien Destugues*/ 178e82e8f36SAdrien Destugues 179a33f8fbdSAdrien Destugues 180e82e8f36SAdrien Destugues/*! 181cae874d3SAdrien Destugues \fn const char* BCatalog::GetString(uint32 id) 182e82e8f36SAdrien Destugues \brief Get a string by id from the catalog. 183e82e8f36SAdrien Destugues 184a33f8fbdSAdrien Destugues The id based version of this method is slightly faster, as it doesn't 185a33f8fbdSAdrien Destugues have to compute the hash from the 3 parameters. However, it will fail 186a33f8fbdSAdrien Destugues if there is an hash collision, so you should still fallback to the first 187a33f8fbdSAdrien Destugues one in case of problems. Also note that the hash value may be different 188a33f8fbdSAdrien Destugues from one catalog to another, depending on the file format they are stored 189a33f8fbdSAdrien Destugues in, so you shouldn't rely on this method unless you are sure you can keep 190a33f8fbdSAdrien Destugues all the catalog files under control. 191e9fd63ecSAdrien Destugues 192e82e8f36SAdrien Destugues \param id The identifier of the string. 19389dfe27fSJohn Scipione 194a33f8fbdSAdrien Destugues \returns The translated string if found, or an empty string. 195edc845a3SJohn Scipione 196edc845a3SJohn Scipione \since Haiku R1 197c6247544SAdrien Destugues*/ 198c6247544SAdrien Destugues 199c6247544SAdrien Destugues 200c6247544SAdrien Destugues/*! 201c6247544SAdrien Destugues \fn status_t BCatalog::GetData(const char* name, BMessage* msg) 202c6247544SAdrien Destugues \brief Get custom data from the catalog. 203c6247544SAdrien Destugues 2046ac7032dSJohn Scipione This method allows you to localize something else than raw text. This 205a33f8fbdSAdrien Destugues may include pictures, sounds, videos, or anything else. Note there is no 206a33f8fbdSAdrien Destugues support for generating a catalog with such data inside, and the current 207a33f8fbdSAdrien Destugues format may not support it. If you need to localize data that is not text, 208a33f8fbdSAdrien Destugues it is advised to handle it by yourself. 209c6247544SAdrien Destugues 210e82e8f36SAdrien Destugues \param name The name of the data to retrieve. 211e82e8f36SAdrien Destugues \param msg The BMessage to fill in with the data. 212a33f8fbdSAdrien Destugues 21389dfe27fSJohn Scipione \returns A status code. 21489dfe27fSJohn Scipione \retval B_OK Everything went fine. 21589dfe27fSJohn Scipione \retval B_ERROR Unable to get an exclusive lock on data. 21689dfe27fSJohn Scipione \retval B_NO_INIT Catalog is \c NULL. 21789dfe27fSJohn Scipione \retval B_NAME_NOT_FOUND catalog with the specified \a name could not be 21889dfe27fSJohn Scipione found. 219edc845a3SJohn Scipione 220edc845a3SJohn Scipione \since Haiku R1 221c6247544SAdrien Destugues*/ 222c6247544SAdrien Destugues 223a33f8fbdSAdrien Destugues 224c6247544SAdrien Destugues/*! 225cae874d3SAdrien Destugues \fn status_t BCatalog::GetData(uint32 id, BMessage* msg) 226cae874d3SAdrien Destugues \brief Get custom data from the catalog. 227cae874d3SAdrien Destugues 228a33f8fbdSAdrien Destugues As for GetString, the id-based version may be subject to hash-collisions, 229a33f8fbdSAdrien Destugues but is faster. 230cae874d3SAdrien Destugues 231a33f8fbdSAdrien Destugues Note the current catalog format doesn't allow storing custom data in 2326ac7032dSJohn Scipione catalogs, so the only way to use this method is providing your own 233a33f8fbdSAdrien Destugues catalog add-on for storing the data. 234edc845a3SJohn Scipione 235edc845a3SJohn Scipione \since Haiku R1 236cae874d3SAdrien Destugues*/ 237cae874d3SAdrien Destugues 238a33f8fbdSAdrien Destugues 239cae874d3SAdrien Destugues/*! 240c6247544SAdrien Destugues \fn status_t BCatalog::GetSignature(BString* sig) 241c6247544SAdrien Destugues \brief Get the catalog mime-signature. 242c6247544SAdrien Destugues 2436ac7032dSJohn Scipione This method fills the sig string with the mime-signature associated to the 244c6247544SAdrien Destugues catalog. 245c6247544SAdrien Destugues 246e82e8f36SAdrien Destugues \param sig The string where to copy the signature. 247a33f8fbdSAdrien Destugues 248e82e8f36SAdrien Destugues \returns An error code. 249edc845a3SJohn Scipione 250edc845a3SJohn Scipione \since Haiku R1 251c6247544SAdrien Destugues*/ 252c6247544SAdrien Destugues 253a33f8fbdSAdrien Destugues 254c6247544SAdrien Destugues/*! 255c6247544SAdrien Destugues \fn status_t BCatalog::GetLanguage(BString* lang) 256c6247544SAdrien Destugues \brief Get the catalog language. 257c6247544SAdrien Destugues 2586ac7032dSJohn Scipione This method fills the lang string with the language name for the catalog. 259e82e8f36SAdrien Destugues 260a33f8fbdSAdrien Destugues \param lang The string where to copy the language. 261a33f8fbdSAdrien Destugues 262e82e8f36SAdrien Destugues \returns An error code. 26389dfe27fSJohn Scipione \retval B_OK Everything went as expected. 26489dfe27fSJohn Scipione \retval B_ERROR Could not get exclusive lock on catalog. 26589dfe27fSJohn Scipione \retval B_BAD_VALUE \a lang is \c NULL. 26689dfe27fSJohn Scipione \retval B_NO_INIT Catalog data is \c NULL. 267edc845a3SJohn Scipione 268edc845a3SJohn Scipione \since Haiku R1 269c6247544SAdrien Destugues*/ 270c6247544SAdrien Destugues 271a33f8fbdSAdrien Destugues 272c6247544SAdrien Destugues/*! 273c6247544SAdrien Destugues \fn status_t BCatalog::GetFingerprint(uint32* fp) 274c6247544SAdrien Destugues \brief Get the catalog fingerprint. 275c6247544SAdrien Destugues 2766ac7032dSJohn Scipione This method setsfp to the fingerprint of the catalog. This allows you 277a33f8fbdSAdrien Destugues to check which version of the sourcecode this catalog was generated from. 278a33f8fbdSAdrien Destugues 279a33f8fbdSAdrien Destugues \param fp The integer to set to the fingerprint value. 280e82e8f36SAdrien Destugues 281e82e8f36SAdrien Destugues \returns An error code. 28289dfe27fSJohn Scipione \retval B_OK Everything went as expected. 28389dfe27fSJohn Scipione \retval B_ERROR Could not get exclusive lock on catalog. 28489dfe27fSJohn Scipione \retval B_BAD_VALUE \a fp is \c NULL. 28589dfe27fSJohn Scipione \retval B_NO_INIT Catalog data is \c NULL. 286edc845a3SJohn Scipione 287edc845a3SJohn Scipione \since Haiku R1 288c6247544SAdrien Destugues*/ 289c6247544SAdrien Destugues 290a33f8fbdSAdrien Destugues 291c6247544SAdrien Destugues/*! 29289dfe27fSJohn Scipione \fn status_t BCatalog::SetTo(const entry_ref& catalogOwner, 29389dfe27fSJohn Scipione const char* language, uint32 fingerprint) 294c6247544SAdrien Destugues \brief Reload the string data. 295c6247544SAdrien Destugues 29641853a8bSAdrien Destugues This method reloads the data for the given file, language and fingerprint. 297e82e8f36SAdrien Destugues 29841853a8bSAdrien Destugues \param catalogOwner The \c entry_ref of the application, add-on or library 29941853a8bSAdrien Destugues for which you want to load a catalog. 30089dfe27fSJohn Scipione \param language The language of the catalog to load. If \c NULL, the user 30189dfe27fSJohn Scipione settings will be used. 302e82e8f36SAdrien Destugues \param fingerprint The fingerprint of the catalog you want to load. 303a33f8fbdSAdrien Destugues 30489dfe27fSJohn Scipione \returns A status code, \c B_OK on success, \c B_ERROR on error. 305edc845a3SJohn Scipione 306edc845a3SJohn Scipione \since Haiku R1 307c6247544SAdrien Destugues*/ 308c6247544SAdrien Destugues 309a33f8fbdSAdrien Destugues 310c6247544SAdrien Destugues/*! 31141853a8bSAdrien Destugues \fn status_t BCatalog::SetTo(const char* signature, 312*edbeede4SAnarchos const char* language = \c NULL) 31341853a8bSAdrien Destugues \brief Reload the string data. 31441853a8bSAdrien Destugues 31541853a8bSAdrien Destugues This method reloads the data for the given signature and language. 31641853a8bSAdrien Destugues 31741853a8bSAdrien Destugues \param signature The MIME signature identifying the catalog to load. 31841853a8bSAdrien Destugues \param language The language of the catalog to load. If \c NULL, the user 31941853a8bSAdrien Destugues settings will be used. 32041853a8bSAdrien Destugues 32141853a8bSAdrien Destugues \returns A status code, \c B_OK on success, \c B_ERROR on error. 32241853a8bSAdrien Destugues 32341853a8bSAdrien Destugues \since Haiku R1 32441853a8bSAdrien Destugues*/ 32541853a8bSAdrien Destugues 32641853a8bSAdrien Destugues 32741853a8bSAdrien Destugues/*! 328cae874d3SAdrien Destugues \fn status_t BCatalog::InitCheck() const 32989dfe27fSJohn Scipione \brief Check if the catalog is in a valid and usable state. 330c6247544SAdrien Destugues 33189dfe27fSJohn Scipione \returns A status code. 33289dfe27fSJohn Scipione \retval B_OK The catalog is initialized properly. 33389dfe27fSJohn Scipione \retval B_ERROR Could not get exclusive lock on catalog. 33489dfe27fSJohn Scipione \retval B_NO_INIT Catalog data is \c NULL. 335edc845a3SJohn Scipione 336edc845a3SJohn Scipione \since Haiku R1 337c6247544SAdrien Destugues*/ 338c6247544SAdrien Destugues 339a33f8fbdSAdrien Destugues 340c6247544SAdrien Destugues/*! 3416ec9625aSAdrien Destugues \fn int32 BCatalog::CountItems() const 34289dfe27fSJohn Scipione \brief Gets the number of items in the catalog. 343c6247544SAdrien Destugues 34489dfe27fSJohn Scipione \returns the number of strings in the catalog or 0 on error. 345edc845a3SJohn Scipione 346edc845a3SJohn Scipione \since Haiku R1 347c6247544SAdrien Destugues*/ 348