1/* 2 * Copyright 2011-2012 Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Axel Dörfler, axeld@pinc-software.de 7 * John Scipione, jscipione@gmail.com 8 * Oliver Tappe, zooey@hirschkaefer.de 9 * 10 * Corresponds to: 11 * /trunk/headers/os/locale/Catalog.h hrev45083 12 * /trunk/src/kits/locale/Catalog.cpp hrev45083 13 */ 14 15 16/*! 17 \file Catalog.h 18 \brief Provides the BCatalog class. 19*/ 20 21 22/*! 23 \class BCatalog 24 \ingroup locale 25 \ingroup libbe 26 \brief String localization handling. 27 28 BCatalog is the class that allows you to perform string localization. This 29 means you give it a string in english, and it automatically returns the 30 translation of this string in the user's specified language, if available. 31 32 Most of the time, you don't have to deal with BCatalog directly. You use 33 the translation macros instead. However, there are some cases where you 34 will have to use catalogs directly. These include : 35 - Tools for managing catalogs : if you want to add, remove or edit 36 entries in a catalog, you need to do it using the BCatalog class. 37 - Accessing catalogs other than your own : the macros only grant you 38 access to the catalog linked with your application. To access 39 other catalogs (for example if you create a script interpreter and 40 want to localize the scripts), you will have to open a catalog 41 associated with your script. 42 43 \section macros Using the macros 44 You don't have to do much in your program to handle catalogs. You must 45 first set the B_TRANSLATION_CONTEXT define to a string that identifies which 46 part of the application the strings you will translate are in. This allows 47 the translators to keep track of the strings in the catalog more easily, 48 and find where they are visible in the application. then, all you have to 49 do, is enclose any string you want to make translatable in the 50 B_TRANSLATE() macro. This macro has two uses, it will allow your text to 51 be replaced at run-time by the proper localized one, but it will also 52 allow to build the base catalog, the one that you will send to the 53 translator team, from your sourcecode. 54 55 \section chaining Chaining of catalogs 56 The catalogs you get from the locale kit are designed to use a fallback 57 system so that the user get strings in the language he's the most fluent 58 with, depending on what catalogs are available. 59 60 For example, if the user sets his language preferences as french(France), 61 spanish, english, when an application loads a catalog, the following rules 62 are used: 63 - Try to load a french(France) catalog. If it is found, this catalog 64 will automatically include strings from the generic french catalog. 65 - Try to load a generic french catalog. 66 - Try to load a generic spanish catalog. 67 - Try to load a generic english catalog. 68 - If all of them failed, use the strings that are in the source code. 69 70 Note that french(France) will failback to french, but then directly to the 71 language in the source code. This avoids mixing 3 or more languages in the 72 same application if the catalogs are incomplete and avoids confusion. 73*/ 74 75 76/*! 77 \fn BCatalog::BCatalog() 78 \brief Construct an empty BCatalog object. 79 80 Should be followed by SetTo() method to set the catalog. 81*/ 82 83 84/*! 85 \fn BCatalog::BCatalog(const entry_ref& catalogOwner, const char* language, 86 uint32 fingerprint) 87 \brief Construct a BCatalog object for the given \a catalogOwner. 88 89 If you don't specify a language, the system default list will be used. 90 The language is passed here as a 2 letter ISO code. 91 92 The fingerprint is a way to check that the catalog that will be loaded 93 matches the current version of the application. A catalog made for a 94 different version of the application can be loaded if you set the 95 fingerprint to \c 0. This is usually not a problem, it only means that 96 some strings may not be translated properly. But if you want to provide 97 different versions of your application, it may be useful to separate their 98 catalogs. 99 100 \param catalogOwner entry_ref for which to load a catalog. 101 \param language The language of the catalog to load. If \c NULL, the user 102 settings will be used. 103 \param fingerprint The fingerprint version-info for the catalog to load. 104 If \c 0, the fingerprint will not be checked,and any version of the 105 catalog will be loaded. 106*/ 107 108 109/*! 110 \fn BCatalog::~BCatalog() 111 \brief Destroys the BCatalog object freeing memory used by it. 112*/ 113 114 115/*! 116 \fn const char* BCatalog::GetString(const char* string, 117 const char* context, const char* comment) 118 \brief Get a string from the catalog. 119 120 This method access the data of the catalog and reeturns you the translated 121 version of the string. You must pass it the context where the string is, as 122 the same string may appear somewhere else and need a differnet translation. 123 The comment is optional. It is meant as an help to translators, when the 124 string alone is not helpful enough or there are special things to note. 125 The comment is also used as a way to uniquely identify a string, so if two 126 identical strings share the same context, it is still possible to provide 127 different translations. 128 129 \param string The string to translate. 130 \param context The context where the string is located. 131 \param comment Supplementary comment for translators. 132 133 \returns The translated string, or the one passed as a parameter if no 134 translation was found. 135*/ 136 137 138/*! 139 \fn const char* BCatalog::GetString(uint32 id) 140 \brief Get a string by id from the catalog. 141 142 The id based version of this method is slightly faster, as it doesn't 143 have to compute the hash from the 3 parameters. However, it will fail 144 if there is an hash collision, so you should still fallback to the first 145 one in case of problems. Also note that the hash value may be different 146 from one catalog to another, depending on the file format they are stored 147 in, so you shouldn't rely on this method unless you are sure you can keep 148 all the catalog files under control. 149 150 \param id The identifier of the string. 151 152 \returns The translated string if found, or an empty string. 153*/ 154 155 156/*! 157 \fn status_t BCatalog::GetData(const char* name, BMessage* msg) 158 \brief Get custom data from the catalog. 159 160 This method allows you to localize something else than raw text. This 161 may include pictures, sounds, videos, or anything else. Note there is no 162 support for generating a catalog with such data inside, and the current 163 format may not support it. If you need to localize data that is not text, 164 it is advised to handle it by yourself. 165 166 \param name The name of the data to retrieve. 167 \param msg The BMessage to fill in with the data. 168 169 \returns A status code. 170 \retval B_OK Everything went fine. 171 \retval B_ERROR Unable to get an exclusive lock on data. 172 \retval B_NO_INIT Catalog is \c NULL. 173 \retval B_NAME_NOT_FOUND catalog with the specified \a name could not be 174 found. 175*/ 176 177 178/*! 179 \fn status_t BCatalog::GetData(uint32 id, BMessage* msg) 180 \brief Get custom data from the catalog. 181 182 As for GetString, the id-based version may be subject to hash-collisions, 183 but is faster. 184 185 Note the current catalog format doesn't allow storing custom data in 186 catalogs, so the only way to use this method is providing your own 187 catalog add-on for storing the data. 188*/ 189 190 191/*! 192 \fn status_t BCatalog::GetSignature(BString* sig) 193 \brief Get the catalog mime-signature. 194 195 This method fills the sig string with the mime-signature associated to the 196 catalog. 197 198 \param sig The string where to copy the signature. 199 200 \returns An error code. 201*/ 202 203 204/*! 205 \fn status_t BCatalog::GetLanguage(BString* lang) 206 \brief Get the catalog language. 207 208 This method fills the lang string with the language name for the catalog. 209 210 \param lang The string where to copy the language. 211 212 \returns An error code. 213 \retval B_OK Everything went as expected. 214 \retval B_ERROR Could not get exclusive lock on catalog. 215 \retval B_BAD_VALUE \a lang is \c NULL. 216 \retval B_NO_INIT Catalog data is \c NULL. 217*/ 218 219 220/*! 221 \fn status_t BCatalog::GetFingerprint(uint32* fp) 222 \brief Get the catalog fingerprint. 223 224 This method setsfp to the fingerprint of the catalog. This allows you 225 to check which version of the sourcecode this catalog was generated from. 226 227 \param fp The integer to set to the fingerprint value. 228 229 \returns An error code. 230 \retval B_OK Everything went as expected. 231 \retval B_ERROR Could not get exclusive lock on catalog. 232 \retval B_BAD_VALUE \a fp is \c NULL. 233 \retval B_NO_INIT Catalog data is \c NULL. 234*/ 235 236 237/*! 238 \fn status_t BCatalog::SetTo(const entry_ref& catalogOwner, 239 const char* language, uint32 fingerprint) 240 \brief Reload the string data. 241 242 This method reloads the data for the given signature and fingerprint. 243 244 \param catalogOwner The \c entry_ref of the catalog that you want to load. 245 \param language The language of the catalog to load. If \c NULL, the user 246 settings will be used. 247 \param fingerprint The fingerprint of the catalog you want to load. 248 249 \returns A status code, \c B_OK on success, \c B_ERROR on error. 250*/ 251 252 253/*! 254 \fn status_t BCatalog::InitCheck() const 255 \brief Check if the catalog is in a valid and usable state. 256 257 \returns A status code. 258 \retval B_OK The catalog is initialized properly. 259 \retval B_ERROR Could not get exclusive lock on catalog. 260 \retval B_NO_INIT Catalog data is \c NULL. 261*/ 262 263 264/*! 265 \fn int32 BCatalog::CountItems() 266 \brief Gets the number of items in the catalog. 267 268 \returns the number of strings in the catalog or 0 on error. 269*/ 270