2011-08-10 01:46:13 +04:00
|
|
|
/*
|
2013-02-07 06:05:00 +04:00
|
|
|
* Copyright 2011-2012 Haiku, Inc. All rights reserved.
|
2011-08-10 01:46:13 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Axel Dörfler, axeld@pinc-software.de
|
|
|
|
* John Scipione, jscipione@gmail.com
|
|
|
|
* Oliver Tappe, zooey@hirschkaefer.de
|
2013-02-07 06:05:00 +04:00
|
|
|
*
|
2011-08-10 01:46:13 +04:00
|
|
|
* Corresponds to:
|
2020-08-25 15:49:37 +03:00
|
|
|
* headers/os/locale/Catalog.h hrev54533
|
|
|
|
* src/kits/locale/Catalog.cpp hrev54533
|
2011-08-10 01:46:13 +04:00
|
|
|
*/
|
|
|
|
|
2010-08-04 16:01:38 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
/*!
|
|
|
|
\file Catalog.h
|
2013-02-07 06:05:00 +04:00
|
|
|
\ingroup locale
|
|
|
|
\ingroup libbe
|
2011-08-10 01:46:13 +04:00
|
|
|
\brief Provides the BCatalog class.
|
2010-08-03 20:02:55 +04:00
|
|
|
*/
|
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2010-08-03 20:02:55 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\class BCatalog
|
|
|
|
\ingroup locale
|
2012-12-25 04:07:56 +04:00
|
|
|
\ingroup libbe
|
|
|
|
\brief String localization handling.
|
2011-08-10 01:46:13 +04:00
|
|
|
|
|
|
|
BCatalog is the class that allows you to perform string localization. This
|
|
|
|
means you give it a string in english, and it automatically returns the
|
|
|
|
translation of this string in the user's specified language, if available.
|
|
|
|
|
|
|
|
Most of the time, you don't have to deal with BCatalog directly. You use
|
|
|
|
the translation macros instead. However, there are some cases where you
|
|
|
|
will have to use catalogs directly. These include :
|
2012-12-25 04:07:56 +04:00
|
|
|
- Tools for managing catalogs : if you want to add, remove or edit
|
|
|
|
entries in a catalog, you need to do it using the BCatalog class.
|
|
|
|
- Accessing catalogs other than your own : the macros only grant you
|
|
|
|
access to the catalog linked with your application. To access
|
|
|
|
other catalogs (for example if you create a script interpreter and
|
|
|
|
want to localize the scripts), you will have to open a catalog
|
|
|
|
associated with your script.
|
2011-08-10 01:46:13 +04:00
|
|
|
|
|
|
|
\section macros Using the macros
|
2017-09-05 18:18:39 +03:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
You don't have to do much in your program to handle catalogs. You must
|
2012-04-16 23:31:22 +04:00
|
|
|
first set the B_TRANSLATION_CONTEXT define to a string that identifies which
|
2011-08-10 01:46:13 +04:00
|
|
|
part of the application the strings you will translate are in. This allows
|
|
|
|
the translators to keep track of the strings in the catalog more easily,
|
|
|
|
and find where they are visible in the application. then, all you have to
|
|
|
|
do, is enclose any string you want to make translatable in the
|
|
|
|
B_TRANSLATE() macro. This macro has two uses, it will allow your text to
|
|
|
|
be replaced at run-time by the proper localized one, but it will also
|
|
|
|
allow to build the base catalog, the one that you will send to the
|
|
|
|
translator team, from your sourcecode.
|
|
|
|
|
2017-09-05 18:18:39 +03:00
|
|
|
Note that each image (application, library or add-on) using these macros
|
|
|
|
must be linked with liblocalestub.a. This allows the Locale Kit to identify
|
|
|
|
it and locate the matching string catalogs for translation.
|
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
\section chaining Chaining of catalogs
|
2017-09-05 18:18:39 +03:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
The catalogs you get from the locale kit are designed to use a fallback
|
|
|
|
system so that the user get strings in the language he's the most fluent
|
|
|
|
with, depending on what catalogs are available.
|
|
|
|
|
2017-09-05 18:18:39 +03:00
|
|
|
For example, if the user sets his language preferences as French(France),
|
2011-08-10 01:46:13 +04:00
|
|
|
spanish, english, when an application loads a catalog, the following rules
|
2012-12-25 04:07:56 +04:00
|
|
|
are used:
|
2017-09-05 18:18:39 +03:00
|
|
|
- Try to load a French(France) catalog. If it is found, this catalog
|
2012-12-25 04:07:56 +04:00
|
|
|
will automatically include strings from the generic french catalog.
|
|
|
|
- Try to load a generic french catalog.
|
|
|
|
- Try to load a generic spanish catalog.
|
|
|
|
- Try to load a generic english catalog.
|
|
|
|
- If all of them failed, use the strings that are in the source code.
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2017-09-05 18:18:39 +03:00
|
|
|
Note that French(France) will failback to French, but then directly to the
|
2011-08-10 01:46:13 +04:00
|
|
|
language in the source code. This avoids mixing 3 or more languages in the
|
|
|
|
same application if the catalogs are incomplete and avoids confusion.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-03 20:02:55 +04:00
|
|
|
*/
|
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2010-08-03 20:02:55 +04:00
|
|
|
/*!
|
2012-12-25 04:07:56 +04:00
|
|
|
\fn BCatalog::BCatalog()
|
|
|
|
\brief Construct an empty BCatalog object.
|
|
|
|
|
|
|
|
Should be followed by SetTo() method to set the catalog.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2012-12-25 04:07:56 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn BCatalog::BCatalog(const entry_ref& catalogOwner, const char* language,
|
|
|
|
uint32 fingerprint)
|
|
|
|
\brief Construct a BCatalog object for the given \a catalogOwner.
|
2011-08-10 01:46:13 +04:00
|
|
|
|
|
|
|
If you don't specify a language, the system default list will be used.
|
|
|
|
The language is passed here as a 2 letter ISO code.
|
|
|
|
|
|
|
|
The fingerprint is a way to check that the catalog that will be loaded
|
|
|
|
matches the current version of the application. A catalog made for a
|
|
|
|
different version of the application can be loaded if you set the
|
|
|
|
fingerprint to \c 0. This is usually not a problem, it only means that
|
|
|
|
some strings may not be translated properly. But if you want to provide
|
|
|
|
different versions of your application, it may be useful to separate their
|
|
|
|
catalogs.
|
|
|
|
|
2020-08-25 15:49:37 +03:00
|
|
|
\param catalogOwner entry_ref or application, add-on or library for which
|
|
|
|
to load a catalog.
|
2011-11-02 12:36:02 +04:00
|
|
|
\param language The language of the catalog to load. If \c NULL, the user
|
2014-06-12 00:48:17 +04:00
|
|
|
settings will be used.
|
2011-08-10 01:46:13 +04:00
|
|
|
\param fingerprint The fingerprint version-info for the catalog to load.
|
2014-06-12 00:48:17 +04:00
|
|
|
If \c 0, the fingerprint will not be checked,and any version of the
|
|
|
|
catalog will be loaded.
|
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2020-08-25 15:49:37 +03:00
|
|
|
/*!
|
|
|
|
\fn BCatalog::BCatalog(const char* signature, const char* language)
|
2020-09-25 00:48:43 +03:00
|
|
|
\brief Construct a BCatalog object for the given application \a signature.
|
2020-08-25 15:49:37 +03:00
|
|
|
|
|
|
|
If you don't specify a language, the system default list will be used.
|
|
|
|
The language is passed here as a 2 letter ISO code.
|
|
|
|
|
|
|
|
This constructor is used to load catalogs that are not related to an
|
|
|
|
executable or library file (so there is no entry_ref usable to identify the
|
|
|
|
catalog). As it uses only the MIME signature, it cannot load catalogs from
|
|
|
|
application resources or a catalog file located next to the application.
|
|
|
|
Only the catalogs in the standard system directories (data/locale/catalogs)
|
|
|
|
are checked. Moreover, only the default catalog format is available, not
|
|
|
|
custom formats from catalog add-ons.
|
|
|
|
|
2020-09-25 00:48:43 +03:00
|
|
|
\param signature MIME signature for which to load a catalog.
|
2020-08-25 15:49:37 +03:00
|
|
|
\param language The language of the catalog to load. If \c NULL, the user
|
|
|
|
settings will be used.
|
|
|
|
|
|
|
|
\since Haiku R1
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-12-25 04:07:56 +04:00
|
|
|
/*!
|
|
|
|
\fn BCatalog::~BCatalog()
|
|
|
|
\brief Destroys the BCatalog object freeing memory used by it.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2012-12-25 04:07:56 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn const char* BCatalog::GetString(const char* string,
|
2012-12-25 04:07:56 +04:00
|
|
|
const char* context, const char* comment)
|
2011-08-10 01:46:13 +04:00
|
|
|
\brief Get a string from the catalog.
|
|
|
|
|
2014-06-12 00:48:17 +04:00
|
|
|
This method access the data of the catalog and returns you the translated
|
2011-08-10 01:46:13 +04:00
|
|
|
version of the string. You must pass it the context where the string is, as
|
2014-06-12 00:48:17 +04:00
|
|
|
the same string may appear somewhere else and need a different translation.
|
2011-08-10 01:46:13 +04:00
|
|
|
The comment is optional. It is meant as an help to translators, when the
|
|
|
|
string alone is not helpful enough or there are special things to note.
|
|
|
|
The comment is also used as a way to uniquely identify a string, so if two
|
|
|
|
identical strings share the same context, it is still possible to provide
|
|
|
|
different translations.
|
|
|
|
|
|
|
|
\param string The string to translate.
|
|
|
|
\param context The context where the string is located.
|
|
|
|
\param comment Supplementary comment for translators.
|
|
|
|
|
|
|
|
\returns The translated string, or the one passed as a parameter if no
|
2014-06-12 00:48:17 +04:00
|
|
|
translation was found.
|
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-03 20:02:55 +04:00
|
|
|
*/
|
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2010-08-04 16:01:38 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn const char* BCatalog::GetString(uint32 id)
|
|
|
|
\brief Get a string by id from the catalog.
|
|
|
|
|
|
|
|
The id based version of this method is slightly faster, as it doesn't
|
|
|
|
have to compute the hash from the 3 parameters. However, it will fail
|
|
|
|
if there is an hash collision, so you should still fallback to the first
|
|
|
|
one in case of problems. Also note that the hash value may be different
|
|
|
|
from one catalog to another, depending on the file format they are stored
|
|
|
|
in, so you shouldn't rely on this method unless you are sure you can keep
|
|
|
|
all the catalog files under control.
|
|
|
|
|
|
|
|
\param id The identifier of the string.
|
2012-12-25 04:07:56 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
\returns The translated string if found, or an empty string.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-04 16:01:38 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn status_t BCatalog::GetData(const char* name, BMessage* msg)
|
|
|
|
\brief Get custom data from the catalog.
|
|
|
|
|
2011-11-02 12:36:02 +04:00
|
|
|
This method allows you to localize something else than raw text. This
|
2011-08-10 01:46:13 +04:00
|
|
|
may include pictures, sounds, videos, or anything else. Note there is no
|
|
|
|
support for generating a catalog with such data inside, and the current
|
|
|
|
format may not support it. If you need to localize data that is not text,
|
|
|
|
it is advised to handle it by yourself.
|
|
|
|
|
|
|
|
\param name The name of the data to retrieve.
|
|
|
|
\param msg The BMessage to fill in with the data.
|
|
|
|
|
2012-12-25 04:07:56 +04:00
|
|
|
\returns A status code.
|
|
|
|
\retval B_OK Everything went fine.
|
|
|
|
\retval B_ERROR Unable to get an exclusive lock on data.
|
|
|
|
\retval B_NO_INIT Catalog is \c NULL.
|
|
|
|
\retval B_NAME_NOT_FOUND catalog with the specified \a name could not be
|
2014-06-12 00:48:17 +04:00
|
|
|
found.
|
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-11 12:09:12 +04:00
|
|
|
*/
|
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2010-08-11 12:09:12 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn status_t BCatalog::GetData(uint32 id, BMessage* msg)
|
|
|
|
\brief Get custom data from the catalog.
|
2010-08-11 12:09:12 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
As for GetString, the id-based version may be subject to hash-collisions,
|
|
|
|
but is faster.
|
2010-08-11 12:09:12 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
Note the current catalog format doesn't allow storing custom data in
|
2011-11-02 12:36:02 +04:00
|
|
|
catalogs, so the only way to use this method is providing your own
|
2011-08-10 01:46:13 +04:00
|
|
|
catalog add-on for storing the data.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-04 16:01:38 +04:00
|
|
|
*/
|
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2010-08-04 16:01:38 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn status_t BCatalog::GetSignature(BString* sig)
|
|
|
|
\brief Get the catalog mime-signature.
|
|
|
|
|
2011-11-02 12:36:02 +04:00
|
|
|
This method fills the sig string with the mime-signature associated to the
|
2011-08-10 01:46:13 +04:00
|
|
|
catalog.
|
2010-08-04 16:01:38 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
\param sig The string where to copy the signature.
|
2010-08-04 16:01:38 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
\returns An error code.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-04 16:01:38 +04:00
|
|
|
*/
|
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2010-08-04 16:01:38 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn status_t BCatalog::GetLanguage(BString* lang)
|
|
|
|
\brief Get the catalog language.
|
2010-08-04 16:01:38 +04:00
|
|
|
|
2011-11-02 12:36:02 +04:00
|
|
|
This method fills the lang string with the language name for the catalog.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
\param lang The string where to copy the language.
|
|
|
|
|
|
|
|
\returns An error code.
|
2012-12-25 04:07:56 +04:00
|
|
|
\retval B_OK Everything went as expected.
|
|
|
|
\retval B_ERROR Could not get exclusive lock on catalog.
|
|
|
|
\retval B_BAD_VALUE \a lang is \c NULL.
|
|
|
|
\retval B_NO_INIT Catalog data is \c NULL.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-04 16:01:38 +04:00
|
|
|
*/
|
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2010-08-04 16:01:38 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn status_t BCatalog::GetFingerprint(uint32* fp)
|
|
|
|
\brief Get the catalog fingerprint.
|
|
|
|
|
2011-11-02 12:36:02 +04:00
|
|
|
This method setsfp to the fingerprint of the catalog. This allows you
|
2011-08-10 01:46:13 +04:00
|
|
|
to check which version of the sourcecode this catalog was generated from.
|
2010-08-04 16:01:38 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
\param fp The integer to set to the fingerprint value.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
\returns An error code.
|
2012-12-25 04:07:56 +04:00
|
|
|
\retval B_OK Everything went as expected.
|
|
|
|
\retval B_ERROR Could not get exclusive lock on catalog.
|
|
|
|
\retval B_BAD_VALUE \a fp is \c NULL.
|
|
|
|
\retval B_NO_INIT Catalog data is \c NULL.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-04 16:01:38 +04:00
|
|
|
*/
|
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2010-08-04 16:01:38 +04:00
|
|
|
/*!
|
2012-12-25 04:07:56 +04:00
|
|
|
\fn status_t BCatalog::SetTo(const entry_ref& catalogOwner,
|
|
|
|
const char* language, uint32 fingerprint)
|
2011-08-10 01:46:13 +04:00
|
|
|
\brief Reload the string data.
|
|
|
|
|
2020-08-25 15:49:37 +03:00
|
|
|
This method reloads the data for the given file, language and fingerprint.
|
2010-08-04 16:01:38 +04:00
|
|
|
|
2020-08-25 15:49:37 +03:00
|
|
|
\param catalogOwner The \c entry_ref of the application, add-on or library
|
|
|
|
for which you want to load a catalog.
|
2012-12-25 04:07:56 +04:00
|
|
|
\param language The language of the catalog to load. If \c NULL, the user
|
2014-06-12 00:48:17 +04:00
|
|
|
settings will be used.
|
2011-08-10 01:46:13 +04:00
|
|
|
\param fingerprint The fingerprint of the catalog you want to load.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2012-12-25 04:07:56 +04:00
|
|
|
\returns A status code, \c B_OK on success, \c B_ERROR on error.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-04 16:01:38 +04:00
|
|
|
*/
|
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2020-08-25 15:49:37 +03:00
|
|
|
/*!
|
|
|
|
\fn status_t BCatalog::SetTo(const char* signature,
|
2020-09-25 00:48:43 +03:00
|
|
|
const char* language = \c NULL)
|
2020-08-25 15:49:37 +03:00
|
|
|
\brief Reload the string data.
|
|
|
|
|
|
|
|
This method reloads the data for the given signature and language.
|
|
|
|
|
|
|
|
\param signature The MIME signature identifying the catalog to load.
|
|
|
|
\param language The language of the catalog to load. If \c NULL, the user
|
|
|
|
settings will be used.
|
|
|
|
|
|
|
|
\returns A status code, \c B_OK on success, \c B_ERROR on error.
|
|
|
|
|
|
|
|
\since Haiku R1
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-08-04 16:01:38 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn status_t BCatalog::InitCheck() const
|
2012-12-25 04:07:56 +04:00
|
|
|
\brief Check if the catalog is in a valid and usable state.
|
2010-08-04 16:01:38 +04:00
|
|
|
|
2012-12-25 04:07:56 +04:00
|
|
|
\returns A status code.
|
|
|
|
\retval B_OK The catalog is initialized properly.
|
|
|
|
\retval B_ERROR Could not get exclusive lock on catalog.
|
|
|
|
\retval B_NO_INIT Catalog data is \c NULL.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-04 16:01:38 +04:00
|
|
|
*/
|
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2010-08-04 16:01:38 +04:00
|
|
|
/*!
|
2013-10-04 18:52:49 +04:00
|
|
|
\fn int32 BCatalog::CountItems() const
|
2012-12-25 04:07:56 +04:00
|
|
|
\brief Gets the number of items in the catalog.
|
2010-08-04 16:01:38 +04:00
|
|
|
|
2012-12-25 04:07:56 +04:00
|
|
|
\returns the number of strings in the catalog or 0 on error.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-04 16:01:38 +04:00
|
|
|
*/
|