1 /* 2 * Copyright 2003-2012, Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _CATALOG_H_ 6 #define _CATALOG_H_ 7 8 9 #include <LocaleRoster.h> 10 #include <Locker.h> 11 #include <SupportDefs.h> 12 #include <String.h> 13 14 15 class BCatalogData; 16 class BLocale; 17 class BMessage; 18 struct entry_ref; 19 20 21 class BCatalog { 22 public: 23 BCatalog(); 24 BCatalog(const entry_ref& catalogOwner, 25 const char* language = NULL, 26 uint32 fingerprint = 0); 27 BCatalog(const char* signature, 28 const char* language = NULL); 29 30 virtual ~BCatalog(); 31 32 const char* GetString(const char* string, 33 const char* context = NULL, 34 const char* comment = NULL); 35 const char* GetString(uint32 id); 36 37 status_t GetData(const char* name, BMessage* msg); 38 status_t GetData(uint32 id, BMessage* msg); 39 40 status_t GetSignature(BString* signature); 41 status_t GetLanguage(BString* language); 42 status_t GetFingerprint(uint32* fingerprint); 43 44 status_t SetTo(const entry_ref& catalogOwner, 45 const char* language = NULL, 46 uint32 fingerprint = 0); 47 status_t SetTo(const char* signature, 48 const char* language = NULL); 49 50 status_t InitCheck() const; 51 int32 CountItems() const; 52 53 protected: 54 BCatalog(const BCatalog&); 55 const BCatalog& operator= (const BCatalog&); 56 // hide assignment and copy-constructor 57 58 BCatalogData* fCatalogData; 59 mutable BLocker fLock; 60 61 private: 62 friend class BLocale; 63 friend status_t get_add_on_catalog(BCatalog*, const char*); 64 }; 65 66 67 #undef B_TRANSLATION_SYSTEM_NAME_CONTEXT 68 #define B_TRANSLATION_SYSTEM_NAME_CONTEXT "System name" 69 70 71 #ifndef B_AVOID_TRANSLATION_MACROS 72 // macros for easy catalog-access, define B_AVOID_TRANSLATION_MACROS if 73 // you don't want these (in which case you need to collect the catalog keys 74 // manually, as collectcatkeys won't do it for you): 75 76 #undef B_TRANSLATION_CONTEXT 77 // In a single application, several strings (e.g. 'Ok') will be used 78 // more than once, in different contexts. 79 // As the application programmer can not know if all translations of 80 // this string will be the same for all languages, each occurrence of 81 // the string must be translated on its own. 82 // Specifying the context explicitly with each string allows the person 83 // translating a catalog to separate these different occurrences of the 84 // same string and tell which strings appears in what context of the 85 // application. 86 // In order to give the translator a useful hint, the application 87 // programmer needs to define B_TRANSLATION_CONTEXT with the context he'd 88 // like to be associated with the strings used in this specifc source file. 89 // example: 90 // #define B_TRANSLATION_CONTEXT "Folder-Window" 91 // Tip: Use a descriptive name of the class implemented in that 92 // source-file. 93 94 #ifdef B_COLLECTING_CATKEYS 95 96 // pull in all the macros used when collecting catalog keys. 97 #include <tools/CollectingCatalog.h> 98 99 #else 100 101 // Translation macros which may be used to shorten translation requests: 102 #undef B_TRANSLATE 103 #define B_TRANSLATE(string) \ 104 BLocaleRoster::Default()->GetCatalog()->GetString((string), \ 105 B_TRANSLATION_CONTEXT) 106 107 #undef B_TRANSLATE_CONTEXT 108 #define B_TRANSLATE_CONTEXT(string, context) \ 109 BLocaleRoster::Default()->GetCatalog()->GetString((string), (context)) 110 111 #undef B_TRANSLATE_COMMENT 112 #define B_TRANSLATE_COMMENT(string, comment) \ 113 BLocaleRoster::Default()->GetCatalog()->GetString((string), \ 114 B_TRANSLATION_CONTEXT, (comment)) 115 116 #undef B_TRANSLATE_ALL 117 #define B_TRANSLATE_ALL(string, context, comment) \ 118 BLocaleRoster::Default()->GetCatalog()->GetString((string), (context), \ 119 (comment)) 120 121 #undef B_TRANSLATE_ID 122 #define B_TRANSLATE_ID(id) \ 123 BLocaleRoster::Default()->GetCatalog()->GetString((id)) 124 125 #undef B_TRANSLATE_SYSTEM_NAME 126 #define B_TRANSLATE_SYSTEM_NAME(string) \ 127 (BLocaleRoster::Default()->IsFilesystemTranslationPreferred() \ 128 ? BLocaleRoster::Default()->GetCatalog()->GetString((string), \ 129 B_TRANSLATION_SYSTEM_NAME_CONTEXT) \ 130 : (string)) 131 132 // Translation markers which can be used to mark static strings/IDs which 133 // are used as key for translation requests (at other places in the code). 134 /* Example: 135 #define B_TRANSLATION_CONTEXT "MyDecentApp-Menu" 136 137 static const char* choices[] = { 138 B_TRANSLATE_MARK("left"), 139 B_TRANSLATE_MARK("right"), 140 B_TRANSLATE_MARK("up"), 141 B_TRANSLATE_MARK("down") 142 }; 143 144 void MyClass::AddChoices(BMenu* menu) 145 { 146 for (char** ch = choices; *ch != '\0'; ++ch) { 147 menu->AddItem( 148 new BMenuItem( 149 B_TRANSLATE(*ch), 150 new BMessage(...) 151 ) 152 ); 153 } 154 } 155 */ 156 #undef B_TRANSLATE_MARK 157 #define B_TRANSLATE_MARK(string) (string) 158 159 #undef B_TRANSLATE_MARK_CONTEXT 160 #define B_TRANSLATE_MARK_CONTEXT(string, context) (string) 161 162 #undef B_TRANSLATE_MARK_COMMENT 163 #define B_TRANSLATE_MARK_COMMENT(string, comment) (string) 164 165 #undef B_TRANSLATE_MARK_ALL 166 #define B_TRANSLATE_MARK_ALL(string, context, comment) (string) 167 168 #undef B_TRANSLATE_MARK_ID 169 #define B_TRANSLATE_MARK_ID(id) (id) 170 171 #undef B_TRANSLATE_MARK_SYSTEM_NAME 172 #define B_TRANSLATE_MARK_SYSTEM_NAME(string) (string) 173 174 // the same for void contexts: 175 #undef B_TRANSLATE_MARK_VOID 176 #define B_TRANSLATE_MARK_VOID(string) 177 178 #undef B_TRANSLATE_MARK_CONTEXT_VOID 179 #define B_TRANSLATE_MARK_CONTEXT_VOID(string, context) 180 181 #undef B_TRANSLATE_MARK_COMMENT_VOID 182 #define B_TRANSLATE_MARK_COMMENT_VOID(string, comment) 183 184 #undef B_TRANSLATE_MARK_ALL_VOID 185 #define B_TRANSLATE_MARK_ALL_VOID(string, context, comment) 186 187 #undef B_TRANSLATE_MARK_ID_VOID 188 #define B_TRANSLATE_MARK_ID_VOID(id) 189 190 #undef B_TRANSLATE_MARK_SYSTEM_NAME_VOID 191 #define B_TRANSLATE_MARK_SYSTEM_NAME_VOID(string) 192 193 // Translation macros which cause collectcatkeys to ignore this key 194 // (useful in combination with the marking macros above): 195 #undef B_TRANSLATE_NOCOLLECT 196 #define B_TRANSLATE_NOCOLLECT(string) \ 197 B_TRANSLATE(string) 198 199 #undef B_TRANSLATE_NOCOLLECT_COMMENT 200 #define B_TRANSLATE_NOCOLLECT_COMMENT(string, comment) \ 201 B_TRANSLATE_COMMENT(string, comment) 202 203 #undef B_TRANSLATE_NOCOLLECT_ALL 204 #define B_TRANSLATE_NOCOLLECT_ALL(string, context, comment) \ 205 B_TRANSLATE_ALL(string, context, comment) 206 207 #undef B_TRANSLATE_NOCOLLECT_ID 208 #define B_TRANSLATE_NOCOLLECT_ID(id) \ 209 B_TRANSLATE_ID(id) 210 211 #undef B_TRANSLATE_NOCOLLECT_SYSTEM_NAME 212 #define B_TRANSLATE_NOCOLLECT_SYSTEM_NAME(string) \ 213 B_TRANSLATE_SYSTEM_NAME(string) 214 215 #endif /* B_COLLECTING_CATKEYS */ 216 217 #endif /* B_AVOID_TRANSLATION_MACROS */ 218 219 220 #endif /* _CATALOG_H_ */ 221