]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testlang.c
5d9a4719af41a707fc44edf6c2b80b0bfc47b412
2 * Localization test program for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
21 #include "ppd-private.h"
23 # include <CoreFoundation/CoreFoundation.h>
24 #endif /* __APPLE__ */
31 static int test_string(cups_lang_t
*language
, const char *msgid
);
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 int errors
= 0; /* Number of errors */
44 cups_lang_t
*language
; /* Message catalog */
45 cups_lang_t
*language2
; /* Message catalog */
46 struct lconv
*loc
; /* Locale data */
47 char buffer
[1024]; /* String buffer */
48 double number
; /* Number */
49 static const char * const tests
[] = /* Test strings */
60 language
= cupsLangDefault();
61 language2
= cupsLangDefault();
65 language
= cupsLangGet(argv
[1]);
66 language2
= cupsLangGet(argv
[1]);
68 setenv("LANG", argv
[1], 1);
69 setenv("SOFTWARE", "CUPS/" CUPS_SVERSION
, 1);
74 if (language
!= language2
)
78 puts("**** ERROR: Language cache did not work! ****");
79 puts("First result from cupsLangGet:");
82 printf("Language = \"%s\"\n", language
->language
);
83 printf("Encoding = \"%s\"\n", _cupsEncodingName(language
->encoding
));
85 errors
+= test_string(language
, "No");
86 errors
+= test_string(language
, "Yes");
88 if (language
!= language2
)
90 puts("Second result from cupsLangGet:");
92 printf("Language = \"%s\"\n", language2
->language
);
93 printf("Encoding = \"%s\"\n", _cupsEncodingName(language2
->encoding
));
94 printf("No = \"%s\"\n", _cupsLangString(language2
, "No"));
95 printf("Yes = \"%s\"\n", _cupsLangString(language2
, "Yes"));
100 for (i
= 0; i
< (int)(sizeof(tests
) / sizeof(tests
[0])); i
++)
102 number
= _cupsStrScand(tests
[i
], NULL
, loc
);
104 printf("_cupsStrScand(\"%s\") number=%f\n", tests
[i
], number
);
106 _cupsStrFormatd(buffer
, buffer
+ sizeof(buffer
), number
, loc
);
108 printf("_cupsStrFormatd(%f) buffer=\"%s\"\n", number
, buffer
);
110 if (strcmp(buffer
, tests
[i
]))
113 puts("**** ERROR: Bad formatted number! ****");
119 ppd_file_t
*ppd
; /* PPD file */
120 ppd_option_t
*option
; /* PageSize option */
121 ppd_choice_t
*choice
; /* PageSize/Letter choice */
123 if ((ppd
= ppdOpenFile(argv
[2])) == NULL
)
125 printf("Unable to open PPD file \"%s\".\n", argv
[2]);
132 if ((option
= ppdFindOption(ppd
, "PageSize")) == NULL
)
134 puts("No PageSize option.");
139 printf("PageSize: %s\n", option
->text
);
141 if ((choice
= ppdFindChoice(option
, "Letter")) == NULL
)
143 puts("No Letter PageSize choice.");
148 printf("Letter: %s\n", choice
->text
);
152 printf("media-empty: %s\n", ppdLocalizeIPPReason(ppd
, "media-empty", NULL
, buffer
, sizeof(buffer
)));
161 * Test all possible language IDs for compatibility with _cupsAppleLocale...
164 CFIndex j
, /* Looping var */
165 num_locales
; /* Number of locales */
166 CFArrayRef locales
; /* Locales */
167 CFStringRef locale_id
, /* Current locale ID */
168 language_id
; /* Current language ID */
169 char locale_str
[256], /* Locale ID C string */
170 language_str
[256], /* Language ID C string */
171 *bufptr
; /* Pointer to ".UTF-8" in POSIX locale */
172 size_t buflen
; /* Length of POSIX locale */
173 # if TEST_COUNTRY_CODES
174 CFIndex k
, /* Looping var */
175 num_country_codes
; /* Number of country codes */
176 CFArrayRef country_codes
; /* Country codes */
177 CFStringRef country_code
, /* Current country code */
178 temp_id
; /* Temporary language ID */
179 char country_str
[256]; /* Country code C string */
180 # endif /* TEST_COUNTRY_CODES */
182 locales
= CFLocaleCopyAvailableLocaleIdentifiers();
183 num_locales
= CFArrayGetCount(locales
);
185 # if TEST_COUNTRY_CODES
186 country_codes
= CFLocaleCopyISOCountryCodes();
187 num_country_codes
= CFArrayGetCount(country_codes
);
188 # endif /* TEST_COUNTRY_CODES */
190 printf("%d locales are available:\n", (int)num_locales
);
192 for (j
= 0; j
< num_locales
; j
++)
194 locale_id
= CFArrayGetValueAtIndex(locales
, j
);
195 language_id
= CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorDefault
, locale_id
);
197 if (!locale_id
|| !CFStringGetCString(locale_id
, locale_str
, (CFIndex
)sizeof(locale_str
), kCFStringEncodingASCII
))
199 printf("%d: FAIL (unable to get locale ID string)\n", (int)j
+ 1);
204 if (!language_id
|| !CFStringGetCString(language_id
, language_str
, (CFIndex
)sizeof(language_str
), kCFStringEncodingASCII
))
206 printf("%d %s: FAIL (unable to get language ID string)\n", (int)j
+ 1, locale_str
);
211 if (!_cupsAppleLocale(language_id
, buffer
, sizeof(buffer
)))
213 printf("%d %s(%s): FAIL (unable to convert language ID string to POSIX locale)\n", (int)j
+ 1, locale_str
, language_str
);
218 if ((bufptr
= strstr(buffer
, ".UTF-8")) != NULL
)
219 buflen
= (size_t)(bufptr
- buffer
);
221 buflen
= strlen(buffer
);
223 if ((language
= cupsLangGet(buffer
)) == NULL
)
225 printf("%d %s(%s): FAIL (unable to load POSIX locale \"%s\")\n", (int)j
+ 1, locale_str
, language_str
, buffer
);
230 if (strncasecmp(language
->language
, buffer
, buflen
))
232 printf("%d %s(%s): FAIL (unable to load POSIX locale \"%s\", got \"%s\")\n", (int)j
+ 1, locale_str
, language_str
, buffer
, language
->language
);
237 printf("%d %s(%s): PASS (POSIX locale is \"%s\")\n", (int)j
+ 1, locale_str
, language_str
, buffer
);
242 # if TEST_COUNTRY_CODES
243 CFRelease(country_codes
);
244 # endif /* TEST_COUNTRY_CODES */
246 #endif /* __APPLE__ */
253 * 'test_string()' - Test the localization of a string.
256 static int /* O - 1 on failure, 0 on success */
257 test_string(cups_lang_t
*language
, /* I - Language */
258 const char *msgid
) /* I - Message */
260 const char *msgstr
; /* Localized string */
264 * Get the localized string and then see if we got what we expected.
266 * For the POSIX locale, the string pointers should be the same.
267 * For any other locale, the string pointers should be different.
270 msgstr
= _cupsLangString(language
, msgid
);
271 if (strcmp(language
->language
, "C") && msgid
== msgstr
)
273 printf("%-8s = \"%s\" (FAIL - no message catalog loaded)\n", msgid
, msgstr
);
276 else if (!strcmp(language
->language
, "C") && msgid
!= msgstr
)
278 printf("%-8s = \"%s\" (FAIL - POSIX locale is localized)\n", msgid
, msgstr
);
282 printf("%-8s = \"%s\" (PASS)\n", msgid
, msgstr
);