]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/testlang.c
License change: Apache License, Version 2.0.
[thirdparty/cups.git] / cups / testlang.c
CommitLineData
ef416fc2 1/*
503b54c9 2 * Localization test program for CUPS.
ef416fc2 3 *
1a9743f9 4 * Copyright 2007-2017 by Apple Inc.
503b54c9 5 * Copyright 1997-2006 by Easy Software Products.
ef416fc2 6 *
e3101897 7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
ef416fc2 8 */
9
10/*
11 * Include necessary headers...
12 */
13
71e16022 14#include "cups-private.h"
f787e1e3 15#include "ppd-private.h"
1a9743f9
MS
16#ifdef __APPLE__
17# include <CoreFoundation/CoreFoundation.h>
18#endif /* __APPLE__ */
ef416fc2 19
20
9964a31a
MS
21/*
22 * Local functions...
23 */
24
25static int test_string(cups_lang_t *language, const char *msgid);
26
27
ef416fc2 28/*
29 * 'main()' - Load the specified language and show the strings for yes and no.
30 */
31
32int /* O - Exit status */
33main(int argc, /* I - Number of command-line arguments */
34 char *argv[]) /* I - Command-line arguments */
35{
7594b224 36 int i; /* Looping var */
37 int errors = 0; /* Number of errors */
ef416fc2 38 cups_lang_t *language; /* Message catalog */
39 cups_lang_t *language2; /* Message catalog */
7594b224 40 struct lconv *loc; /* Locale data */
41 char buffer[1024]; /* String buffer */
42 double number; /* Number */
43 static const char * const tests[] = /* Test strings */
44 {
45 "1",
46 "-1",
47 "3",
48 "5.125"
49 };
50
ef416fc2 51
ef416fc2 52 if (argc == 1)
53 {
54 language = cupsLangDefault();
55 language2 = cupsLangDefault();
56 }
57 else
58 {
59 language = cupsLangGet(argv[1]);
60 language2 = cupsLangGet(argv[1]);
36474350
MS
61
62 setenv("LANG", argv[1], 1);
63 setenv("SOFTWARE", "CUPS/" CUPS_SVERSION, 1);
ef416fc2 64 }
65
36474350
MS
66 _cupsSetLocale(argv);
67
ef416fc2 68 if (language != language2)
69 {
7594b224 70 errors ++;
71
ef416fc2 72 puts("**** ERROR: Language cache did not work! ****");
73 puts("First result from cupsLangGet:");
74 }
75
76 printf("Language = \"%s\"\n", language->language);
77 printf("Encoding = \"%s\"\n", _cupsEncodingName(language->encoding));
997db404 78
9964a31a
MS
79 errors += test_string(language, "No");
80 errors += test_string(language, "Yes");
ef416fc2 81
82 if (language != language2)
83 {
84 puts("Second result from cupsLangGet:");
85
86 printf("Language = \"%s\"\n", language2->language);
87 printf("Encoding = \"%s\"\n", _cupsEncodingName(language2->encoding));
88 printf("No = \"%s\"\n", _cupsLangString(language2, "No"));
89 printf("Yes = \"%s\"\n", _cupsLangString(language2, "Yes"));
90 }
91
7594b224 92 loc = localeconv();
93
94 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i ++)
95 {
96 number = _cupsStrScand(tests[i], NULL, loc);
97
98 printf("_cupsStrScand(\"%s\") number=%f\n", tests[i], number);
99
100 _cupsStrFormatd(buffer, buffer + sizeof(buffer), number, loc);
101
102 printf("_cupsStrFormatd(%f) buffer=\"%s\"\n", number, buffer);
103
104 if (strcmp(buffer, tests[i]))
105 {
106 errors ++;
107 puts("**** ERROR: Bad formatted number! ****");
108 }
109 }
110
36474350
MS
111 if (argc == 3)
112 {
113 ppd_file_t *ppd; /* PPD file */
114 ppd_option_t *option; /* PageSize option */
115 ppd_choice_t *choice; /* PageSize/Letter choice */
116
117 if ((ppd = ppdOpenFile(argv[2])) == NULL)
118 {
119 printf("Unable to open PPD file \"%s\".\n", argv[2]);
120 errors ++;
121 }
122 else
123 {
124 ppdLocalize(ppd);
125
126 if ((option = ppdFindOption(ppd, "PageSize")) == NULL)
127 {
128 puts("No PageSize option.");
129 errors ++;
130 }
131 else
132 {
133 printf("PageSize: %s\n", option->text);
134
135 if ((choice = ppdFindChoice(option, "Letter")) == NULL)
136 {
137 puts("No Letter PageSize choice.");
138 errors ++;
139 }
140 else
141 {
142 printf("Letter: %s\n", choice->text);
143 }
144 }
145
2650d637
MS
146 printf("media-empty: %s\n", ppdLocalizeIPPReason(ppd, "media-empty", NULL, buffer, sizeof(buffer)));
147
36474350
MS
148 ppdClose(ppd);
149 }
150 }
1a9743f9
MS
151#ifdef __APPLE__
152 else
153 {
154 /*
155 * Test all possible language IDs for compatibility with _cupsAppleLocale...
156 */
157
158 CFIndex j, /* Looping var */
159 num_locales; /* Number of locales */
160 CFArrayRef locales; /* Locales */
161 CFStringRef locale_id, /* Current locale ID */
162 language_id; /* Current language ID */
163 char locale_str[256], /* Locale ID C string */
164 language_str[256], /* Language ID C string */
165 *bufptr; /* Pointer to ".UTF-8" in POSIX locale */
166 size_t buflen; /* Length of POSIX locale */
167# if TEST_COUNTRY_CODES
168 CFIndex k, /* Looping var */
169 num_country_codes; /* Number of country codes */
170 CFArrayRef country_codes; /* Country codes */
171 CFStringRef country_code, /* Current country code */
172 temp_id; /* Temporary language ID */
173 char country_str[256]; /* Country code C string */
174# endif /* TEST_COUNTRY_CODES */
175
176 locales = CFLocaleCopyAvailableLocaleIdentifiers();
177 num_locales = CFArrayGetCount(locales);
178
179# if TEST_COUNTRY_CODES
180 country_codes = CFLocaleCopyISOCountryCodes();
181 num_country_codes = CFArrayGetCount(country_codes);
182# endif /* TEST_COUNTRY_CODES */
183
184 printf("%d locales are available:\n", (int)num_locales);
185
186 for (j = 0; j < num_locales; j ++)
187 {
188 locale_id = CFArrayGetValueAtIndex(locales, j);
189 language_id = CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorDefault, locale_id);
190
191 if (!locale_id || !CFStringGetCString(locale_id, locale_str, (CFIndex)sizeof(locale_str), kCFStringEncodingASCII))
192 {
193 printf("%d: FAIL (unable to get locale ID string)\n", (int)j + 1);
194 errors ++;
195 continue;
196 }
197
198 if (!language_id || !CFStringGetCString(language_id, language_str, (CFIndex)sizeof(language_str), kCFStringEncodingASCII))
199 {
200 printf("%d %s: FAIL (unable to get language ID string)\n", (int)j + 1, locale_str);
201 errors ++;
202 continue;
203 }
204
205 if (!_cupsAppleLocale(language_id, buffer, sizeof(buffer)))
206 {
207 printf("%d %s(%s): FAIL (unable to convert language ID string to POSIX locale)\n", (int)j + 1, locale_str, language_str);
208 errors ++;
209 continue;
210 }
211
212 if ((bufptr = strstr(buffer, ".UTF-8")) != NULL)
213 buflen = (size_t)(bufptr - buffer);
214 else
215 buflen = strlen(buffer);
216
217 if ((language = cupsLangGet(buffer)) == NULL)
218 {
219 printf("%d %s(%s): FAIL (unable to load POSIX locale \"%s\")\n", (int)j + 1, locale_str, language_str, buffer);
220 errors ++;
221 continue;
222 }
223
224 if (strncasecmp(language->language, buffer, buflen))
225 {
226 printf("%d %s(%s): FAIL (unable to load POSIX locale \"%s\", got \"%s\")\n", (int)j + 1, locale_str, language_str, buffer, language->language);
227 errors ++;
228 continue;
229 }
230
231 printf("%d %s(%s): PASS (POSIX locale is \"%s\")\n", (int)j + 1, locale_str, language_str, buffer);
232 }
233
234 CFRelease(locales);
235
236# if TEST_COUNTRY_CODES
237 CFRelease(country_codes);
238# endif /* TEST_COUNTRY_CODES */
239 }
240#endif /* __APPLE__ */
36474350 241
6579c998
MS
242 if (errors == 0)
243 puts("ALL TESTS PASSED");
244
7594b224 245 return (errors > 0);
ef416fc2 246}
9964a31a
MS
247
248
249/*
250 * 'test_string()' - Test the localization of a string.
251 */
252
253static int /* O - 1 on failure, 0 on success */
254test_string(cups_lang_t *language, /* I - Language */
255 const char *msgid) /* I - Message */
256{
257 const char *msgstr; /* Localized string */
258
259
260 /*
261 * Get the localized string and then see if we got what we expected.
262 *
263 * For the POSIX locale, the string pointers should be the same.
264 * For any other locale, the string pointers should be different.
265 */
266
267 msgstr = _cupsLangString(language, msgid);
268 if (strcmp(language->language, "C") && msgid == msgstr)
269 {
270 printf("%-8s = \"%s\" (FAIL - no message catalog loaded)\n", msgid, msgstr);
271 return (1);
272 }
273 else if (!strcmp(language->language, "C") && msgid != msgstr)
274 {
275 printf("%-8s = \"%s\" (FAIL - POSIX locale is localized)\n", msgid, msgstr);
276 return (1);
277 }
278
279 printf("%-8s = \"%s\" (PASS)\n", msgid, msgstr);
280
281 return (0);
282}
283