1 /* 2 ** Copyright 2003, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved. 3 ** Distributed under the terms of the OpenBeOS License. 4 */ 5 6 #include <assert.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 #include <CatalogInAddOn.h> 11 #include <DefaultCatalog.h> 12 13 class CatalogTestAddOn { 14 public: 15 void Run(); 16 void Check(); 17 }; 18 19 #define B_TRANSLATE_CONTEXT "CatalogTestAddOn" 20 21 #define catSig "add-ons/catalogTest/catalogTestAddOn" 22 #define catName catSig".catalog" 23 24 25 void 26 CatalogTestAddOn::Run() { 27 printf("addon..."); 28 status_t res; 29 BString s; 30 s << "string" << "\x01" << B_TRANSLATE_CONTEXT << "\x01"; 31 size_t hashVal = CatKey::HashFun(s.String()); 32 assert(be_locale != NULL); 33 system("mkdir -p ./locale/catalogs/"catSig); 34 35 // create an empty catalog of default type... 36 BPrivate::EditableCatalog cat1("Default", catSig, "German"); 37 assert(cat1.InitCheck() == B_OK); 38 39 // ...and populate the catalog with some data: 40 res = cat1.SetString("string", "Schnur_A", B_TRANSLATE_CONTEXT); 41 assert(res == B_OK); 42 res = cat1.SetString(hashVal, "Schnur_id_A"); 43 // add a second entry for the same hash-value, but with different translation 44 assert(res == B_OK); 45 res = cat1.SetString("string", "String_A", "programming"); 46 assert(res == B_OK); 47 res = cat1.SetString("string", "Textpuffer_A", "programming", "Deutsches Fachbuch"); 48 assert(res == B_OK); 49 res = cat1.SetString("string", "Leine_A", B_TRANSLATE_CONTEXT, "Deutsches Fachbuch"); 50 assert(res == B_OK); 51 res = cat1.WriteToFile("./locale/catalogs/"catSig"/german.catalog"); 52 assert(res == B_OK); 53 54 // check if we are getting back the correct strings: 55 s = cat1.GetString("string", B_TRANSLATE_CONTEXT); 56 assert(s == "Schnur_A"); 57 s = cat1.GetString(hashVal); 58 assert(s == "Schnur_id_A"); 59 s = cat1.GetString("string", "programming"); 60 assert(s == "String_A"); 61 s = cat1.GetString("string", "programming", "Deutsches Fachbuch"); 62 assert(s == "Textpuffer_A"); 63 s = cat1.GetString("string", B_TRANSLATE_CONTEXT, "Deutsches Fachbuch"); 64 assert(s == "Leine_A"); 65 66 // now we create a new (base) catalog and embed this one into the add-on-file: 67 BPrivate::EditableCatalog cat2("Default", catSig, "English"); 68 assert(cat2.InitCheck() == B_OK); 69 // the following string is unique to the embedded catalog: 70 res = cat2.SetString("string", "string_A", "base"); 71 assert(res == B_OK); 72 // the following id is unique to the embedded catalog: 73 res = cat2.SetString(32, "hashed string_A"); 74 assert(res == B_OK); 75 // the following string will be hidden by the definition inside the german catalog: 76 res = cat2.SetString("string", "hidden_A", B_TRANSLATE_CONTEXT); 77 assert(res == B_OK); 78 entry_ref addOnRef; 79 res = get_add_on_ref(&addOnRef); 80 assert(res == B_OK); 81 res = cat2.WriteToResource(&addOnRef); 82 assert(res == B_OK); 83 84 printf("ok.\n"); 85 Check(); 86 } 87 88 89 void 90 CatalogTestAddOn::Check() { 91 status_t res; 92 printf("addon-check..."); 93 BString s; 94 s << "string" << "\x01" << B_TRANSLATE_CONTEXT << "\x01"; 95 size_t hashVal = CatKey::HashFun(s.String()); 96 // ok, we now try to re-load the catalog that has just been written: 97 // 98 // actually, the following code can be seen as an example of what an 99 // add_on needs in order to translate strings: 100 BCatalog cat; 101 res = get_add_on_catalog(&cat, catSig); 102 assert(res == B_OK); 103 // fetch basic data: 104 uint32 fingerprint; 105 res = cat.GetFingerprint(&fingerprint); 106 assert(res == B_OK); 107 BString lang; 108 res = cat.GetLanguage(&lang); 109 assert(res == B_OK); 110 assert(lang == "german"); 111 BString sig; 112 res = cat.GetSignature(&sig); 113 assert(res == B_OK); 114 assert(sig == catSig); 115 116 // now check strings: 117 s = B_TRANSLATE_ID(hashVal); 118 assert(s == "Schnur_id_A"); 119 s = B_TRANSLATE_ALL("string", "programming", ""); 120 assert(s == "String_A"); 121 s = B_TRANSLATE_ALL("string", "programming", "Deutsches Fachbuch"); 122 assert(s == "Textpuffer_A"); 123 s = B_TRANSLATE_COMMENT("string", "Deutsches Fachbuch"); 124 assert(s == "Leine_A"); 125 // the following string should be found in the embedded catalog only: 126 s = B_TRANSLATE_ALL("string", "base", ""); 127 assert(s == "string_A"); 128 // the following id should be found in the embedded catalog only: 129 s = B_TRANSLATE_ID(32); 130 assert(s == "hashed string_A"); 131 // the following id doesn't exist anywhere (hopefully): 132 s = B_TRANSLATE_ID(-1); 133 assert(s == ""); 134 // the following string exists twice, in the embedded as well as in the 135 // external catalog. So we should get the external translation (as it should 136 // override the embedded one): 137 s = B_TRANSLATE("string"); 138 assert(s == "Schnur_A"); 139 140 // check access to app-catalog from inside add-on: 141 BCatalog appCat; 142 res = be_locale->GetAppCatalog(&appCat); 143 assert(res == B_OK); 144 s = be_app_catalog->GetString("string", "CatalogTest"); 145 assert(s == "Schnur"); 146 s = be_app_catalog->GetString("string", "CatalogTest", "Deutsches Fachbuch"); 147 assert(s == "Leine"); 148 s = be_app_catalog->GetString("string", "programming", "Deutsches Fachbuch"); 149 assert(s == "Textpuffer"); 150 151 printf("ok.\n"); 152 } 153 154 155 extern "C" _EXPORT void run_test_add_on() 156 { 157 CatalogTestAddOn catTest; 158 catTest.Run(); 159 } 160