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