xref: /haiku/src/tests/kits/locale/catalogTestAddOn.cpp (revision 4d8811742fa447ec05b4993a16a0931bc29aafab)
1 /*
2 ** Copyright 2003, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved.
3 ** Distributed under the terms of the MIT 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_TRANSLATION_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_TRANSLATION_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_TRANSLATION_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
44 		// translation
45 	assert(res == B_OK);
46 	res = cat1.SetString("string", "String_A", "programming");
47 	assert(res == B_OK);
48 	res = cat1.SetString("string", "Textpuffer_A", "programming",
49 		"Deutsches Fachbuch");
50 	assert(res == B_OK);
51 	res = cat1.SetString("string", "Leine_A", B_TRANSLATION_CONTEXT,
52 		"Deutsches Fachbuch");
53 	assert(res == B_OK);
54 	res = cat1.WriteToFile("./locale/catalogs/"catSig"/german.catalog");
55 	assert(res == B_OK);
56 
57 	// check if we are getting back the correct strings:
58 	s = cat1.GetString("string", B_TRANSLATION_CONTEXT);
59 	assert(s == "Schnur_A");
60 	s = cat1.GetString(hashVal);
61 	assert(s == "Schnur_id_A");
62 	s = cat1.GetString("string", "programming");
63 	assert(s == "String_A");
64 	s = cat1.GetString("string", "programming", "Deutsches Fachbuch");
65 	assert(s == "Textpuffer_A");
66 	s = cat1.GetString("string", B_TRANSLATION_CONTEXT, "Deutsches Fachbuch");
67 	assert(s == "Leine_A");
68 
69 	// now we create a new (base) catalog and embed this one into the
70 	// add-on-file:
71 	BPrivate::EditableCatalog cat2("Default", catSig, "English");
72 	assert(cat2.InitCheck() == B_OK);
73 	// the following string is unique to the embedded catalog:
74 	res = cat2.SetString("string", "string_A", "base");
75 	assert(res == B_OK);
76 	// the following id is unique to the embedded catalog:
77 	res = cat2.SetString(32, "hashed string_A");
78 	assert(res == B_OK);
79 	// the following string will be hidden by the definition inside the german
80 	// catalog:
81 	res = cat2.SetString("string", "hidden_A", B_TRANSLATION_CONTEXT);
82 	assert(res == B_OK);
83 	entry_ref addOnRef;
84 	res = get_add_on_ref(&addOnRef);
85 	assert(res == B_OK);
86 	res = cat2.WriteToResource(&addOnRef);
87 	assert(res == B_OK);
88 
89 	printf("ok.\n");
90 	Check();
91 }
92 
93 
94 void
95 CatalogTestAddOn::Check() {
96 	status_t res;
97 	printf("addon-check...");
98 	BString s;
99 	s << "string" << "\x01" << B_TRANSLATION_CONTEXT << "\x01";
100 	size_t hashVal = CatKey::HashFun(s.String());
101 	// ok, we now try to re-load the catalog that has just been written:
102 	//
103 	// actually, the following code can be seen as an example of what an
104 	// add_on needs in order to translate strings:
105 	BCatalog cat;
106 	res = get_add_on_catalog(&cat, catSig);
107 	assert(res == B_OK);
108 	// fetch basic data:
109 	uint32 fingerprint;
110 	res = cat.GetFingerprint(&fingerprint);
111 	assert(res == B_OK);
112 	BString lang;
113 	res = cat.GetLanguage(&lang);
114 	assert(res == B_OK);
115 	assert(lang == "german");
116 	BString sig;
117 	res = cat.GetSignature(&sig);
118 	assert(res == B_OK);
119 	assert(sig == catSig);
120 
121 	// now check strings:
122 	s = B_TRANSLATE_ID(hashVal);
123 	assert(s == "Schnur_id_A");
124 	s = B_TRANSLATE_ALL("string", "programming", "");
125 	assert(s == "String_A");
126 	s = B_TRANSLATE_ALL("string", "programming", "Deutsches Fachbuch");
127 	assert(s == "Textpuffer_A");
128 	s = B_TRANSLATE_COMMENT("string", "Deutsches Fachbuch");
129 	assert(s == "Leine_A");
130 	// the following string should be found in the embedded catalog only:
131 	s = B_TRANSLATE_ALL("string", "base", "");
132 	assert(s == "string_A");
133 	// the following id should be found in the embedded catalog only:
134 	s = B_TRANSLATE_ID(32);
135 	assert(s == "hashed string_A");
136 	// the following id doesn't exist anywhere (hopefully):
137 	s = B_TRANSLATE_ID(-1);
138 	assert(s == "");
139 	// the following string exists twice, in the embedded as well as in the
140 	// external catalog. So we should get the external translation (as it should
141 	// override the embedded one):
142 	s = B_TRANSLATE("string");
143 	assert(s == "Schnur_A");
144 
145 	// check access to app-catalog from inside add-on:
146 	BCatalog appCat;
147 	res = be_locale->GetAppCatalog(&appCat);
148 	assert(res == B_OK);
149 	s = be_app_catalog->GetString("string", "CatalogTest");
150 	assert(s == "Schnur");
151 	s = be_app_catalog->GetString("string", "CatalogTest",
152 		"Deutsches Fachbuch");
153 	assert(s == "Leine");
154 	s = be_app_catalog->GetString("string", "programming",
155 		"Deutsches Fachbuch");
156 	assert(s == "Textpuffer");
157 
158 	printf("ok.\n");
159 }
160 
161 
162 extern "C" _EXPORT void run_test_add_on()
163 {
164 	CatalogTestAddOn catTest;
165 	catTest.Run();
166 }
167