]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testlang.c
2 * Localization test program for CUPS.
6 * ./testlang [-l locale] [-p ppd] ["String to localize"]
8 * Copyright 2007-2017 by Apple Inc.
9 * Copyright 1997-2006 by Easy Software Products.
11 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
15 * Include necessary headers...
18 #include "cups-private.h"
19 #include "ppd-private.h"
21 # include <CoreFoundation/CoreFoundation.h>
22 #endif /* __APPLE__ */
29 static int show_ppd(const char *filename
);
30 static int test_string(cups_lang_t
*language
, const char *msgid
);
31 static void usage(void);
35 * 'main()' - Load the specified language and show the strings for yes and no.
38 int /* O - Exit status */
39 main(int argc
, /* I - Number of command-line arguments */
40 char *argv
[]) /* I - Command-line arguments */
42 int i
; /* Looping var */
43 const char *opt
; /* Current option */
44 int errors
= 0; /* Number of errors */
45 int dotests
= 1; /* Do standard tests? */
46 cups_lang_t
*language
= NULL
;/* Message catalog */
47 cups_lang_t
*language2
= NULL
;
48 /* Message catalog (second time) */
49 struct lconv
*loc
; /* Locale data */
50 char buffer
[1024]; /* String buffer */
51 double number
; /* Number */
52 static const char * const tests
[] = /* Test strings */
62 * Parse command-line...
67 for (i
= 1; i
< argc
; i
++)
69 if (argv
[i
][0] == '-')
71 if (!strcmp(argv
[i
], "--help"))
77 for (opt
= argv
[i
] + 1; *opt
; opt
++)
89 language
= cupsLangGet(argv
[i
]);
90 language2
= cupsLangGet(argv
[i
]);
92 setenv("LANG", argv
[i
], 1);
93 setenv("SOFTWARE", "CUPS/" CUPS_SVERSION
, 1);
106 language
= cupsLangDefault();
107 language2
= cupsLangDefault();
111 errors
+= show_ppd(argv
[i
]);
125 language
= cupsLangDefault();
126 language2
= cupsLangDefault();
130 errors
+= test_string(language
, argv
[i
]);
136 language
= cupsLangDefault();
137 language2
= cupsLangDefault();
140 if (language
!= language2
)
144 puts("**** ERROR: Language cache did not work! ****");
145 puts("First result from cupsLangGet:");
148 printf("Language = \"%s\"\n", language
->language
);
149 printf("Encoding = \"%s\"\n", _cupsEncodingName(language
->encoding
));
153 errors
+= test_string(language
, "No");
154 errors
+= test_string(language
, "Yes");
156 if (language
!= language2
)
158 puts("Second result from cupsLangGet:");
160 printf("Language = \"%s\"\n", language2
->language
);
161 printf("Encoding = \"%s\"\n", _cupsEncodingName(language2
->encoding
));
162 printf("No = \"%s\"\n", _cupsLangString(language2
, "No"));
163 printf("Yes = \"%s\"\n", _cupsLangString(language2
, "Yes"));
168 for (i
= 0; i
< (int)(sizeof(tests
) / sizeof(tests
[0])); i
++)
170 number
= _cupsStrScand(tests
[i
], NULL
, loc
);
172 printf("_cupsStrScand(\"%s\") number=%f\n", tests
[i
], number
);
174 _cupsStrFormatd(buffer
, buffer
+ sizeof(buffer
), number
, loc
);
176 printf("_cupsStrFormatd(%f) buffer=\"%s\"\n", number
, buffer
);
178 if (strcmp(buffer
, tests
[i
]))
181 puts("**** ERROR: Bad formatted number! ****");
187 * Test all possible language IDs for compatibility with _cupsAppleLocale...
190 CFIndex j
, /* Looping var */
191 num_locales
; /* Number of locales */
192 CFArrayRef locales
; /* Locales */
193 CFStringRef locale_id
, /* Current locale ID */
194 language_id
; /* Current language ID */
195 char locale_str
[256], /* Locale ID C string */
196 language_str
[256], /* Language ID C string */
197 *bufptr
; /* Pointer to ".UTF-8" in POSIX locale */
198 size_t buflen
; /* Length of POSIX locale */
199 # if TEST_COUNTRY_CODES
200 CFIndex k
, /* Looping var */
201 num_country_codes
; /* Number of country codes */
202 CFArrayRef country_codes
; /* Country codes */
203 CFStringRef country_code
, /* Current country code */
204 temp_id
; /* Temporary language ID */
205 char country_str
[256]; /* Country code C string */
206 # endif /* TEST_COUNTRY_CODES */
208 locales
= CFLocaleCopyAvailableLocaleIdentifiers();
209 num_locales
= CFArrayGetCount(locales
);
211 # if TEST_COUNTRY_CODES
212 country_codes
= CFLocaleCopyISOCountryCodes();
213 num_country_codes
= CFArrayGetCount(country_codes
);
214 # endif /* TEST_COUNTRY_CODES */
216 printf("%d locales are available:\n", (int)num_locales
);
218 for (j
= 0; j
< num_locales
; j
++)
220 locale_id
= CFArrayGetValueAtIndex(locales
, j
);
221 language_id
= CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorDefault
, locale_id
);
223 if (!locale_id
|| !CFStringGetCString(locale_id
, locale_str
, (CFIndex
)sizeof(locale_str
), kCFStringEncodingASCII
))
225 printf("%d: FAIL (unable to get locale ID string)\n", (int)j
+ 1);
230 if (!language_id
|| !CFStringGetCString(language_id
, language_str
, (CFIndex
)sizeof(language_str
), kCFStringEncodingASCII
))
232 printf("%d %s: FAIL (unable to get language ID string)\n", (int)j
+ 1, locale_str
);
237 if (!_cupsAppleLocale(language_id
, buffer
, sizeof(buffer
)))
239 printf("%d %s(%s): FAIL (unable to convert language ID string to POSIX locale)\n", (int)j
+ 1, locale_str
, language_str
);
244 if ((bufptr
= strstr(buffer
, ".UTF-8")) != NULL
)
245 buflen
= (size_t)(bufptr
- buffer
);
247 buflen
= strlen(buffer
);
249 if ((language
= cupsLangGet(buffer
)) == NULL
)
251 printf("%d %s(%s): FAIL (unable to load POSIX locale \"%s\")\n", (int)j
+ 1, locale_str
, language_str
, buffer
);
256 if (strncasecmp(language
->language
, buffer
, buflen
))
258 printf("%d %s(%s): FAIL (unable to load POSIX locale \"%s\", got \"%s\")\n", (int)j
+ 1, locale_str
, language_str
, buffer
, language
->language
);
263 printf("%d %s(%s): PASS (POSIX locale is \"%s\")\n", (int)j
+ 1, locale_str
, language_str
, buffer
);
268 # if TEST_COUNTRY_CODES
269 CFRelease(country_codes
);
270 # endif /* TEST_COUNTRY_CODES */
271 #endif /* __APPLE__ */
274 if (errors
== 0 && dotests
)
275 puts("ALL TESTS PASSED");
282 * 'show_ppd()' - Show localized strings in a PPD file.
285 static int /* O - Number of errors */
286 show_ppd(const char *filename
) /* I - Filename */
288 ppd_file_t
*ppd
; /* PPD file */
289 ppd_option_t
*option
; /* PageSize option */
290 ppd_choice_t
*choice
; /* PageSize/Letter choice */
291 char buffer
[1024]; /* String buffer */
294 if ((ppd
= ppdOpenFile(filename
)) == NULL
)
296 printf("Unable to open PPD file \"%s\".\n", filename
);
302 if ((option
= ppdFindOption(ppd
, "PageSize")) == NULL
)
304 puts("No PageSize option.");
309 printf("PageSize: %s\n", option
->text
);
311 if ((choice
= ppdFindChoice(option
, "Letter")) == NULL
)
313 puts("No Letter PageSize choice.");
318 printf("Letter: %s\n", choice
->text
);
322 printf("media-empty: %s\n", ppdLocalizeIPPReason(ppd
, "media-empty", NULL
, buffer
, sizeof(buffer
)));
331 * 'test_string()' - Test the localization of a string.
334 static int /* O - 1 on failure, 0 on success */
335 test_string(cups_lang_t
*language
, /* I - Language */
336 const char *msgid
) /* I - Message */
338 const char *msgstr
; /* Localized string */
342 * Get the localized string and then see if we got what we expected.
344 * For the POSIX locale, the string pointers should be the same.
345 * For any other locale, the string pointers should be different.
348 msgstr
= _cupsLangString(language
, msgid
);
349 if (strcmp(language
->language
, "C") && msgid
== msgstr
)
351 printf("%-8s = \"%s\" (FAIL - no message catalog loaded)\n", msgid
, msgstr
);
354 else if (!strcmp(language
->language
, "C") && msgid
!= msgstr
)
356 printf("%-8s = \"%s\" (FAIL - POSIX locale is localized)\n", msgid
, msgstr
);
360 printf("%-8s = \"%s\" (PASS)\n", msgid
, msgstr
);
367 * 'usage()' - Show program usage.
373 puts("./testlang [-l locale] [-p ppd] [\"String to localize\"]");