2 ** Copyright 2003, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved.
3 ** Distributed under the terms of the MIT License.
11 #include <Application.h>
13 #include <DefaultCatalog.h>
25 #define B_TRANSLATION_CONTEXT "CatalogTest"
28 #define catSig "x-vnd.Be.locale.catalogTest"
29 #define catName catSig".catalog"
37 s
<< "string" << "\x01" << B_TRANSLATION_CONTEXT
<< "\x01";
38 size_t hashVal
= CatKey::HashFun(s
.String());
39 assert(be_locale
!= NULL
);
40 system("mkdir -p ./locale/catalogs/"catSig
);
42 // create an empty catalog of default type...
43 BPrivate::EditableCatalog
cata("Default", catSig
, "German");
44 assert(cata
.InitCheck() == B_OK
);
46 // ...and populate the catalog with some data:
47 res
= cata
.SetString("string", "Schnur", B_TRANSLATION_CONTEXT
);
49 res
= cata
.SetString(hashVal
, "Schnur_id");
50 // add a second entry for the same hash-value, but with different
53 res
= cata
.SetString("string", "String", "programming");
55 res
= cata
.SetString("string", "Textpuffer", "programming",
56 "Deutsches Fachbuch");
58 res
= cata
.SetString("string", "Leine", B_TRANSLATION_CONTEXT
,
59 "Deutsches Fachbuch");
61 res
= cata
.WriteToFile("./locale/catalogs/"catSig
"/german.catalog");
64 // check if we are getting back the correct strings:
65 s
= cata
.GetString(("string"), B_TRANSLATION_CONTEXT
);
66 assert(s
== "Schnur");
67 s
= cata
.GetString(hashVal
);
68 assert(s
== "Schnur_id");
69 s
= cata
.GetString("string", "programming");
70 assert(s
== "String");
71 s
= cata
.GetString("string", "programming", "Deutsches Fachbuch");
72 assert(s
== "Textpuffer");
73 s
= cata
.GetString("string", B_TRANSLATION_CONTEXT
, "Deutsches Fachbuch");
76 // now we create a new (base) catalog and embed this one into the app-file:
77 BPrivate::EditableCatalog
catb("Default", catSig
, "English");
78 assert(catb
.InitCheck() == B_OK
);
79 // the following string is unique to the embedded catalog:
80 res
= catb
.SetString("string", "string", "base");
82 // the following id is unique to the embedded catalog:
83 res
= catb
.SetString(32, "hashed string");
85 // the following string will be hidden by the definition inside the
87 res
= catb
.SetString("string", "hidden", B_TRANSLATION_CONTEXT
);
90 res
= be_app
->GetAppInfo(&appInfo
);
92 // embed created catalog into application file (catalogTest):
93 res
= catb
.WriteToResource(&appInfo
.ref
);
105 printf("app-check...");
107 s
<< "string" << "\x01" << B_TRANSLATION_CONTEXT
<< "\x01";
108 size_t hashVal
= CatKey::HashFun(s
.String());
109 // ok, we now try to re-load the catalog that has just been written:
111 // actually, the following code can be seen as an example of what an
112 // app needs in order to translate strings:
114 res
= be_locale
->GetAppCatalog(&cat
);
117 uint32 fingerprint
= 0;
118 res
= cat
.GetFingerprint(&fingerprint
);
121 res
= cat
.GetLanguage(&lang
);
124 res
= cat
.GetSignature(&sig
);
127 // now check strings:
128 s
= B_TRANSLATE_ID(hashVal
);
129 assert(s
== "Schnur_id");
130 s
= B_TRANSLATE_ALL("string", "programming", "");
131 assert(s
== "String");
132 s
= B_TRANSLATE_ALL("string", "programming", "Deutsches Fachbuch");
133 assert(s
== "Textpuffer");
134 s
= B_TRANSLATE_COMMENT("string", "Deutsches Fachbuch");
135 assert(s
== "Leine");
136 // the following string should be found in the embedded catalog only:
137 s
= B_TRANSLATE_ALL("string", "base", NULL
);
138 assert(s
== "string");
139 // the following id should be found in the embedded catalog only:
140 s
= B_TRANSLATE_ID(32);
141 assert(s
== "hashed string");
142 // the following id doesn't exist anywhere (hopefully):
143 s
= B_TRANSLATE_ID(-1);
145 // the following string exists twice, in the embedded as well as in the
146 // external catalog. So we should get the external translation (as it should
147 // override the embedded one):
148 s
= B_TRANSLATE("string");
149 assert(s
== "Schnur");
151 // now check if trying to access same catalog by specifying its data works:
152 BCatalog
cat2(sig
.String(), lang
.String(), fingerprint
);
153 assert(cat2
.InitCheck() == B_OK
);
154 // now check if trying to access same catalog with wrong fingerprint fails:
155 BCatalog
cat3(sig
.String(), lang
.String(), fingerprint
*-1);
156 assert(cat3
.InitCheck() == B_NO_INIT
);
157 // translating through an invalid catalog should yield the native string:
158 s
= cat3
.GetString("string");
159 assert(s
== "string");
166 main(int argc
, char **argv
)
168 BApplication
* testApp
169 = new BApplication("application/"catSig
);
171 // change to app-folder:
173 be_app
->GetAppInfo(&appInfo
);
174 BEntry
appEntry(&appInfo
.ref
);
176 appEntry
.GetParent( &appFolder
);
178 appFolder
.GetPath( &appPath
);
179 chdir( appPath
.Path());
184 char cwd
[B_FILE_NAME_LENGTH
];
185 getcwd(cwd
, B_FILE_NAME_LENGTH
);
186 BString
addonName(cwd
);
187 addonName
<< "/" "catalogTestAddOn";
188 image_id image
= load_add_on(addonName
.String());
189 assert(image
>= B_OK
);
190 void (*runAddonFunc
)() = 0;
191 get_image_symbol(image
, "run_test_add_on",
192 B_SYMBOL_TYPE_TEXT
, (void **)&runAddonFunc
);
193 assert(runAddonFunc
);