xref: /haiku/src/tests/kits/locale/catalogTest.cpp (revision d374a27286b8a52974a97dba0d5966ea026a665d)
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 #include <unistd.h>
10 
11 #include <Application.h>
12 #include <Catalog.h>
13 #include <DefaultCatalog.h>
14 #include <Entry.h>
15 #include <Locale.h>
16 #include <Path.h>
17 #include <Roster.h>
18 
19 class CatalogTest {
20 	public:
21 		void Run();
22 		void Check();
23 };
24 
25 #define B_TRANSLATE_CONTEXT "CatalogTest"
26 
27 
28 #define catSig "x-vnd.Be.locale.catalogTest"
29 #define catName catSig".catalog"
30 
31 void
32 CatalogTest::Run()
33 {
34 	printf("app...");
35 	status_t res;
36 	BString s;
37 	s << "string" << "\x01" << B_TRANSLATE_CONTEXT << "\x01";
38 	size_t hashVal = CatKey::HashFun(s.String());
39 	assert(be_locale != NULL);
40 	system("mkdir -p ./locale/catalogs/"catSig);
41 
42 	// create an empty catalog of default type...
43 	BPrivate::EditableCatalog cata("Default", catSig, "German");
44 	assert(cata.InitCheck() == B_OK);
45 
46 	// ...and populate the catalog with some data:
47 	res = cata.SetString("string", "Schnur", B_TRANSLATE_CONTEXT);
48 	assert(res == B_OK);
49 	res = cata.SetString(hashVal, "Schnur_id");
50 		// add a second entry for the same hash-value, but with different
51 		// translation
52 	assert(res == B_OK);
53 	res = cata.SetString("string", "String", "programming");
54 	assert(res == B_OK);
55 	res = cata.SetString("string", "Textpuffer", "programming",
56 		"Deutsches Fachbuch");
57 	assert(res == B_OK);
58 	res = cata.SetString("string", "Leine", B_TRANSLATE_CONTEXT, "Deutsches Fachbuch");
59 	assert(res == B_OK);
60 	res = cata.WriteToFile("./locale/catalogs/"catSig"/german.catalog");
61 	assert(res == B_OK);
62 
63 	// check if we are getting back the correct strings:
64 	s = cata.GetString(("string"), B_TRANSLATE_CONTEXT);
65 	assert(s == "Schnur");
66 	s = cata.GetString(hashVal);
67 	assert(s == "Schnur_id");
68 	s = cata.GetString("string", "programming");
69 	assert(s == "String");
70 	s = cata.GetString("string", "programming", "Deutsches Fachbuch");
71 	assert(s == "Textpuffer");
72 	s = cata.GetString("string", B_TRANSLATE_CONTEXT, "Deutsches Fachbuch");
73 	assert(s == "Leine");
74 
75 	// now we create a new (base) catalog and embed this one into the app-file:
76 	BPrivate::EditableCatalog catb("Default", catSig, "English");
77 	assert(catb.InitCheck() == B_OK);
78 	// the following string is unique to the embedded catalog:
79 	res = catb.SetString("string", "string", "base");
80 	assert(res == B_OK);
81 	// the following id is unique to the embedded catalog:
82 	res = catb.SetString(32, "hashed string");
83 	assert(res == B_OK);
84 	// the following string will be hidden by the definition inside the
85 	// german catalog:
86 	res = catb.SetString("string", "hidden", B_TRANSLATE_CONTEXT);
87 	assert(res == B_OK);
88 	app_info appInfo;
89 	res = be_app->GetAppInfo(&appInfo);
90 	assert(res == B_OK);
91 	// embed created catalog into application file (catalogTest):
92 	res = catb.WriteToResource(&appInfo.ref);
93 	assert(res == B_OK);
94 
95 	printf("ok.\n");
96 	Check();
97 }
98 
99 
100 void
101 CatalogTest::Check()
102 {
103 	status_t res;
104 	printf("app-check...");
105 	BString s;
106 	s << "string" << "\x01" << B_TRANSLATE_CONTEXT << "\x01";
107 	size_t hashVal = CatKey::HashFun(s.String());
108 	// ok, we now try to re-load the catalog that has just been written:
109 	//
110 	// actually, the following code can be seen as an example of what an
111 	// app needs in order to translate strings:
112 	BCatalog cat;
113 	res = be_locale->GetAppCatalog(&cat);
114 	assert(res == B_OK);
115 	// fetch basic data:
116 	uint32 fingerprint = 0;
117 	res = cat.GetFingerprint(&fingerprint);
118 	assert(res == B_OK);
119 	BString lang;
120 	res = cat.GetLanguage(&lang);
121 	assert(res == B_OK);
122 	BString sig;
123 	res = cat.GetSignature(&sig);
124 	assert(res == B_OK);
125 
126 	// now check strings:
127 	s = B_TRANSLATE_ID(hashVal);
128 	assert(s == "Schnur_id");
129 	s = B_TRANSLATE_ALL("string", "programming", "");
130 	assert(s == "String");
131 	s = B_TRANSLATE_ALL("string", "programming", "Deutsches Fachbuch");
132 	assert(s == "Textpuffer");
133 	s = B_TRANSLATE_COMMENT("string", "Deutsches Fachbuch");
134 	assert(s == "Leine");
135 	// the following string should be found in the embedded catalog only:
136 	s = B_TRANSLATE_ALL("string", "base", NULL);
137 	assert(s == "string");
138 	// the following id should be found in the embedded catalog only:
139 	s = B_TRANSLATE_ID(32);
140 	assert(s == "hashed string");
141 	// the following id doesn't exist anywhere (hopefully):
142 	s = B_TRANSLATE_ID(-1);
143 	assert(s == "");
144 	// the following string exists twice, in the embedded as well as in the
145 	// external catalog. So we should get the external translation (as it should
146 	// override the embedded one):
147 	s = B_TRANSLATE("string");
148 	assert(s == "Schnur");
149 
150 	// now check if trying to access same catalog by specifying its data works:
151 	BCatalog cat2(sig.String(), lang.String(), fingerprint);
152 	assert(cat2.InitCheck() == B_OK);
153 	// now check if trying to access same catalog with wrong fingerprint fails:
154 	BCatalog cat3(sig.String(), lang.String(), fingerprint*-1);
155 	assert(cat3.InitCheck() == B_NO_INIT);
156 	// translating through an invalid catalog should yield the native string:
157 	s = cat3.GetString("string");
158 	assert(s == "string");
159 
160 	printf("ok.\n");
161 }
162 
163 
164 int
165 main(int argc, char **argv)
166 {
167 	BApplication* testApp
168 		= new BApplication("application/"catSig);
169 
170 	// change to app-folder:
171 	app_info appInfo;
172 	be_app->GetAppInfo(&appInfo);
173 	BEntry appEntry(&appInfo.ref);
174 	BEntry appFolder;
175 	appEntry.GetParent( &appFolder);
176 	BPath appPath;
177 	appFolder.GetPath( &appPath);
178 	chdir( appPath.Path());
179 
180 	CatalogTest catTest;
181 	catTest.Run();
182 
183 	char cwd[B_FILE_NAME_LENGTH];
184 	getcwd(cwd, B_FILE_NAME_LENGTH);
185 	BString addonName(cwd);
186 	addonName << "/" "catalogTestAddOn";
187 	image_id image = load_add_on(addonName.String());
188 	assert(image >= B_OK);
189 	void (*runAddonFunc)() = 0;
190 	get_image_symbol(image, "run_test_add_on",
191 		B_SYMBOL_TYPE_TEXT, (void **)&runAddonFunc);
192 	assert(runAddonFunc);
193 	runAddonFunc();
194 
195 	catTest.Check();
196 
197 	delete testApp;
198 }
199