2009-09-19 16:19:46 +04:00
|
|
|
/*
|
2009-09-25 01:03:57 +04:00
|
|
|
* Copyright 2003-2009, Haiku, Inc.
|
2009-09-19 16:19:46 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2009-05-01 23:23:59 +04:00
|
|
|
#ifndef _CATALOG_H_
|
|
|
|
#define _CATALOG_H_
|
|
|
|
|
|
|
|
#include <SupportDefs.h>
|
|
|
|
#include <String.h>
|
|
|
|
|
|
|
|
class BCatalogAddOn;
|
|
|
|
class BLocale;
|
|
|
|
class BMessage;
|
|
|
|
struct entry_ref;
|
|
|
|
|
|
|
|
|
2009-05-02 01:56:16 +04:00
|
|
|
class BCatalog {
|
2009-05-01 23:23:59 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
BCatalog();
|
|
|
|
BCatalog(const char *signature, const char *language = NULL,
|
2009-09-19 02:23:34 +04:00
|
|
|
uint32 fingerprint = 0);
|
2009-05-01 23:23:59 +04:00
|
|
|
virtual ~BCatalog();
|
|
|
|
|
|
|
|
const char *GetString(const char *string, const char *context = NULL,
|
|
|
|
const char *comment = NULL);
|
|
|
|
const char *GetString(uint32 id);
|
|
|
|
|
|
|
|
status_t GetData(const char *name, BMessage *msg);
|
|
|
|
status_t GetData(uint32 id, BMessage *msg);
|
|
|
|
|
|
|
|
status_t GetSignature(BString *sig);
|
|
|
|
status_t GetLanguage(BString *lang);
|
2009-09-19 02:23:34 +04:00
|
|
|
status_t GetFingerprint(uint32 *fp);
|
2009-05-01 23:23:59 +04:00
|
|
|
|
|
|
|
status_t InitCheck() const;
|
|
|
|
int32 CountItems() const;
|
|
|
|
|
|
|
|
BCatalogAddOn *CatalogAddOn();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
BCatalog(const BCatalog&);
|
|
|
|
const BCatalog& operator= (const BCatalog&);
|
|
|
|
// hide assignment and copy-constructor
|
|
|
|
|
|
|
|
static status_t GetAppCatalog(BCatalog*);
|
|
|
|
|
|
|
|
BCatalogAddOn *fCatalog;
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class BLocale;
|
|
|
|
friend status_t get_add_on_catalog(BCatalog*, const char *);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-05-02 01:56:16 +04:00
|
|
|
extern BCatalog* be_catalog;
|
|
|
|
extern BCatalog* be_app_catalog;
|
2009-05-01 23:23:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef B_AVOID_TRANSLATION_MACROS
|
|
|
|
// macros for easy catalog-access, define B_AVOID_TRANSLATION_MACROS if
|
|
|
|
// you don't want these:
|
|
|
|
|
|
|
|
#undef TR_CONTEXT
|
2009-05-02 01:56:16 +04:00
|
|
|
// In a single application, several strings (e.g. 'Ok') will be used
|
|
|
|
// more than once, in different contexts.
|
2009-05-01 23:23:59 +04:00
|
|
|
// As the application programmer can not know if all translations of
|
|
|
|
// this string will be the same for all languages, each occurrence of
|
|
|
|
// the string must be translated on its own.
|
|
|
|
// Specifying the context explicitly with each string allows the person
|
|
|
|
// translating a catalog to separate these different occurrences of the
|
2009-05-02 01:56:16 +04:00
|
|
|
// same string and tell which strings appears in what context of the
|
2009-05-01 23:23:59 +04:00
|
|
|
// application.
|
|
|
|
// In order to give the translator a useful hint, the application
|
2009-05-02 01:56:16 +04:00
|
|
|
// programmer needs to define TR_CONTEXT with the context he'd like
|
2009-05-01 23:23:59 +04:00
|
|
|
// to be associated with the strings used in this specifc source file.
|
|
|
|
// example:
|
|
|
|
// #define TR_CONTEXT "Folder-Window"
|
2009-05-02 01:56:16 +04:00
|
|
|
// Tip: Use a descriptive name of the class implemented in that
|
|
|
|
// source-file.
|
2009-05-01 23:23:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
// Translation macros which may be used to shorten translation requests:
|
|
|
|
#undef TR
|
|
|
|
#define TR(str) \
|
|
|
|
be_catalog->GetString((str), TR_CONTEXT)
|
|
|
|
|
|
|
|
#undef TR_CMT
|
2009-09-20 21:57:06 +04:00
|
|
|
#define TR_CMT(str, cmt) \
|
2009-05-01 23:23:59 +04:00
|
|
|
be_catalog->GetString((str), TR_CONTEXT, (cmt))
|
|
|
|
|
|
|
|
#undef TR_ALL
|
2009-09-20 21:57:06 +04:00
|
|
|
#define TR_ALL(str, ctx, cmt) \
|
2009-05-01 23:23:59 +04:00
|
|
|
be_catalog->GetString((str), (ctx), (cmt))
|
|
|
|
|
|
|
|
#undef TR_ID
|
|
|
|
#define TR_ID(id) \
|
|
|
|
be_catalog->GetString((id))
|
|
|
|
|
|
|
|
|
|
|
|
// Translation markers which can be used to mark static strings/IDs which
|
|
|
|
// are used as key for translation requests (at other places in the code):
|
|
|
|
/* example:
|
|
|
|
#define TR_CONTEXT "MyDecentApp-Menu"
|
|
|
|
|
|
|
|
static const char *choices[] = {
|
|
|
|
TR_MARK("left"),
|
|
|
|
TR_MARK("right"),
|
|
|
|
TR_MARK("up"),
|
|
|
|
TR_MARK("down")
|
|
|
|
};
|
|
|
|
|
|
|
|
void MyClass::AddChoices(BMenu *menu) {
|
|
|
|
for (char **ch = choices; *ch; ch++) {
|
|
|
|
menu->AddItem(
|
|
|
|
new BMenuItem(
|
2009-05-02 01:56:16 +04:00
|
|
|
TR(*ch),
|
2009-05-01 23:23:59 +04:00
|
|
|
new BMessage(...)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
#undef TR_MARK
|
|
|
|
#define TR_MARK(str) \
|
|
|
|
BCatalogAddOn::MarkForTranslation((str), TR_CONTEXT, "")
|
|
|
|
|
|
|
|
#undef TR_MARK_CMT
|
2009-09-20 21:57:06 +04:00
|
|
|
#define TR_MARK_CMT(str, cmt) \
|
2009-05-01 23:23:59 +04:00
|
|
|
BCatalogAddOn::MarkForTranslation((str), TR_CONTEXT, (cmt))
|
|
|
|
|
|
|
|
#undef TR_MARK_ALL
|
2009-09-20 21:57:06 +04:00
|
|
|
#define TR_MARK_ALL(str, ctx, cmt) \
|
2009-05-01 23:23:59 +04:00
|
|
|
BCatalogAddOn::MarkForTranslation((str), (ctx), (cmt))
|
2009-05-02 01:56:16 +04:00
|
|
|
|
2009-05-01 23:23:59 +04:00
|
|
|
#undef TR_MARK_ID
|
|
|
|
#define TR_MARK_ID(id) \
|
|
|
|
BCatalogAddOn::MarkForTranslation((id))
|
2009-05-02 01:56:16 +04:00
|
|
|
|
2009-05-01 23:23:59 +04:00
|
|
|
#endif /* B_AVOID_TRANSLATION_MACROS */
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
// For BCatalog add-on implementations:
|
|
|
|
|
2009-05-02 01:56:16 +04:00
|
|
|
class BCatalogAddOn {
|
2009-05-01 23:23:59 +04:00
|
|
|
friend class BLocaleRoster;
|
|
|
|
public:
|
|
|
|
BCatalogAddOn(const char *signature, const char *language,
|
2009-09-20 21:57:06 +04:00
|
|
|
uint32 fingerprint);
|
2009-05-01 23:23:59 +04:00
|
|
|
virtual ~BCatalogAddOn();
|
|
|
|
|
2009-05-02 01:56:16 +04:00
|
|
|
virtual const char *GetString(const char *string,
|
2009-09-20 21:57:06 +04:00
|
|
|
const char *context = NULL,
|
|
|
|
const char *comment = NULL) = 0;
|
2009-05-01 23:23:59 +04:00
|
|
|
virtual const char *GetString(uint32 id) = 0;
|
|
|
|
|
|
|
|
status_t InitCheck() const;
|
|
|
|
BCatalogAddOn *Next();
|
|
|
|
|
2009-09-20 21:57:06 +04:00
|
|
|
// the following could be used to localize non-textual data (e.g.
|
|
|
|
// icons), but these will only be implemented if there's demand for such
|
|
|
|
// a feature:
|
2009-05-01 23:23:59 +04:00
|
|
|
virtual bool CanHaveData() const;
|
|
|
|
virtual status_t GetData(const char *name, BMessage *msg);
|
|
|
|
virtual status_t GetData(uint32 id, BMessage *msg);
|
|
|
|
|
|
|
|
// interface for catalog-editor-app and testing apps:
|
2009-05-02 01:56:16 +04:00
|
|
|
virtual status_t SetString(const char *string,
|
2009-05-01 23:23:59 +04:00
|
|
|
const char *translated,
|
2009-09-20 21:57:06 +04:00
|
|
|
const char *context = NULL,
|
|
|
|
const char *comment = NULL);
|
2009-05-01 23:23:59 +04:00
|
|
|
virtual status_t SetString(int32 id, const char *translated);
|
2009-09-20 21:57:06 +04:00
|
|
|
|
2009-05-01 23:23:59 +04:00
|
|
|
virtual bool CanWriteData() const;
|
|
|
|
virtual status_t SetData(const char *name, BMessage *msg);
|
|
|
|
virtual status_t SetData(uint32 id, BMessage *msg);
|
2009-09-20 21:57:06 +04:00
|
|
|
|
2009-05-01 23:23:59 +04:00
|
|
|
virtual status_t ReadFromFile(const char *path = NULL);
|
|
|
|
virtual status_t ReadFromAttribute(entry_ref *appOrAddOnRef);
|
|
|
|
virtual status_t ReadFromResource(entry_ref *appOrAddOnRef);
|
|
|
|
virtual status_t WriteToFile(const char *path = NULL);
|
|
|
|
virtual status_t WriteToAttribute(entry_ref *appOrAddOnRef);
|
|
|
|
virtual status_t WriteToResource(entry_ref *appOrAddOnRef);
|
2009-09-20 21:57:06 +04:00
|
|
|
|
2009-05-01 23:23:59 +04:00
|
|
|
virtual void MakeEmpty();
|
|
|
|
virtual int32 CountItems() const;
|
|
|
|
|
|
|
|
// magic marker functions which are used to mark a string/id
|
|
|
|
// which will be translated elsewhere in the code (where it can
|
|
|
|
// not be found since it is references by a variable):
|
2009-05-02 01:56:16 +04:00
|
|
|
static const char *MarkForTranslation(const char *str, const char *ctx,
|
2009-05-01 23:23:59 +04:00
|
|
|
const char *cmt);
|
|
|
|
static int32 MarkForTranslation(int32 id);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void UpdateFingerprint();
|
|
|
|
|
|
|
|
status_t fInitCheck;
|
|
|
|
BString fSignature;
|
|
|
|
BString fLanguageName;
|
2009-09-19 02:23:34 +04:00
|
|
|
uint32 fFingerprint;
|
2009-05-01 23:23:59 +04:00
|
|
|
BCatalogAddOn *fNext;
|
2009-05-02 01:56:16 +04:00
|
|
|
|
2009-05-01 23:23:59 +04:00
|
|
|
friend class BCatalog;
|
|
|
|
friend status_t get_add_on_catalog(BCatalog*, const char *);
|
|
|
|
};
|
|
|
|
|
|
|
|
// every catalog-add-on should export these symbols...
|
|
|
|
// ...the function that instantiates a catalog for this add-on-type...
|
2009-05-02 01:56:16 +04:00
|
|
|
extern "C"
|
2009-05-01 23:23:59 +04:00
|
|
|
BCatalogAddOn *instantiate_catalog(const char *signature,
|
2009-09-19 02:23:34 +04:00
|
|
|
const char *language, uint32 fingerprint);
|
2009-05-01 23:23:59 +04:00
|
|
|
// ...the function that creates an empty catalog for this add-on-type...
|
2009-05-02 01:56:16 +04:00
|
|
|
extern "C"
|
2009-05-01 23:23:59 +04:00
|
|
|
BCatalogAddOn *create_catalog(const char *signature,
|
|
|
|
const char *language);
|
|
|
|
// ...and the priority which will be used to order the catalog-add-ons:
|
2009-05-02 01:56:16 +04:00
|
|
|
extern uint8 gCatalogAddOnPriority;
|
2009-05-01 23:23:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BCatalog - inlines for trivial accessors:
|
|
|
|
*/
|
|
|
|
inline status_t
|
2009-05-02 01:56:16 +04:00
|
|
|
BCatalog::GetSignature(BString *sig)
|
2009-05-01 23:23:59 +04:00
|
|
|
{
|
|
|
|
if (!sig)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
if (!fCatalog)
|
|
|
|
return B_NO_INIT;
|
|
|
|
*sig = fCatalog->fSignature;
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-02 01:56:16 +04:00
|
|
|
inline status_t
|
|
|
|
BCatalog::GetLanguage(BString *lang)
|
2009-05-01 23:23:59 +04:00
|
|
|
{
|
|
|
|
if (!lang)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
if (!fCatalog)
|
|
|
|
return B_NO_INIT;
|
|
|
|
*lang = fCatalog->fLanguageName;
|
|
|
|
return B_OK;
|
2009-05-02 01:56:16 +04:00
|
|
|
}
|
2009-05-01 23:23:59 +04:00
|
|
|
|
|
|
|
|
2009-05-02 01:56:16 +04:00
|
|
|
inline status_t
|
2009-09-19 02:23:34 +04:00
|
|
|
BCatalog::GetFingerprint(uint32 *fp)
|
2009-05-01 23:23:59 +04:00
|
|
|
{
|
|
|
|
if (!fp)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
if (!fCatalog)
|
|
|
|
return B_NO_INIT;
|
|
|
|
*fp = fCatalog->fFingerprint;
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline status_t
|
|
|
|
BCatalog::InitCheck() const
|
|
|
|
{
|
2009-05-02 01:56:16 +04:00
|
|
|
return fCatalog
|
|
|
|
? fCatalog->InitCheck()
|
2009-05-01 23:23:59 +04:00
|
|
|
: B_NO_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-02 01:56:16 +04:00
|
|
|
inline int32
|
2009-05-01 23:23:59 +04:00
|
|
|
BCatalog::CountItems() const
|
|
|
|
{
|
|
|
|
if (!fCatalog)
|
|
|
|
return 0;
|
|
|
|
return fCatalog->CountItems();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline BCatalogAddOn *
|
2009-05-02 01:56:16 +04:00
|
|
|
BCatalog::CatalogAddOn()
|
2009-05-01 23:23:59 +04:00
|
|
|
{
|
|
|
|
return fCatalog;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BCatalogAddOn - inlines for trivial accessors:
|
|
|
|
*/
|
|
|
|
inline BCatalogAddOn *
|
2009-05-02 01:56:16 +04:00
|
|
|
BCatalogAddOn::Next()
|
2009-05-01 23:23:59 +04:00
|
|
|
{
|
|
|
|
return fNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline const char *
|
2009-05-02 01:56:16 +04:00
|
|
|
BCatalogAddOn::MarkForTranslation(const char *str, const char *ctx,
|
|
|
|
const char *cmt)
|
2009-05-01 23:23:59 +04:00
|
|
|
{
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline int32
|
2009-05-02 01:56:16 +04:00
|
|
|
BCatalogAddOn::MarkForTranslation(int32 id)
|
2009-05-01 23:23:59 +04:00
|
|
|
{
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* EditableCatalog
|
|
|
|
*/
|
2009-05-02 01:56:16 +04:00
|
|
|
class EditableCatalog : public BCatalog {
|
2009-05-01 23:23:59 +04:00
|
|
|
|
|
|
|
public:
|
2009-05-02 01:56:16 +04:00
|
|
|
EditableCatalog(const char *type, const char *signature,
|
2009-05-01 23:23:59 +04:00
|
|
|
const char *language);
|
|
|
|
~EditableCatalog();
|
|
|
|
|
2009-05-02 01:56:16 +04:00
|
|
|
status_t SetString(const char *string,
|
2009-05-01 23:23:59 +04:00
|
|
|
const char *translated,
|
2009-09-20 21:57:06 +04:00
|
|
|
const char *context = NULL,
|
|
|
|
const char *comment = NULL);
|
2009-05-01 23:23:59 +04:00
|
|
|
status_t SetString(int32 id, const char *translated);
|
2009-09-20 21:57:06 +04:00
|
|
|
|
2009-05-01 23:23:59 +04:00
|
|
|
bool CanWriteData() const;
|
|
|
|
status_t SetData(const char *name, BMessage *msg);
|
|
|
|
status_t SetData(uint32 id, BMessage *msg);
|
2009-09-20 21:57:06 +04:00
|
|
|
|
2009-05-01 23:23:59 +04:00
|
|
|
status_t ReadFromFile(const char *path = NULL);
|
|
|
|
status_t ReadFromAttribute(entry_ref *appOrAddOnRef);
|
|
|
|
status_t ReadFromResource(entry_ref *appOrAddOnRef);
|
|
|
|
status_t WriteToFile(const char *path = NULL);
|
|
|
|
status_t WriteToAttribute(entry_ref *appOrAddOnRef);
|
|
|
|
status_t WriteToResource(entry_ref *appOrAddOnRef);
|
2009-09-20 21:57:06 +04:00
|
|
|
|
2009-05-01 23:23:59 +04:00
|
|
|
void MakeEmpty();
|
|
|
|
|
|
|
|
private:
|
|
|
|
EditableCatalog();
|
|
|
|
EditableCatalog(const EditableCatalog&);
|
|
|
|
const EditableCatalog& operator= (const EditableCatalog&);
|
|
|
|
// hide assignment, default- and copy-constructor
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace BPrivate
|
|
|
|
|
2009-09-20 21:57:06 +04:00
|
|
|
#endif /* _CATALOG_H_ */
|