]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/localize.c
Merge changes from CUPS 1.4svn-r8394.
[thirdparty/cups.git] / cups / localize.c
CommitLineData
fa73b229 1/*
75bd9771 2 * "$Id: localize.c 7679 2008-06-19 23:37:45Z mike $"
fa73b229 3 *
5a738aea 4 * PPD localization routines for the Common UNIX Printing System (CUPS).
fa73b229 5 *
5a738aea 6 * Copyright 2007-2008 by Apple Inc.
7594b224 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
fa73b229 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
fa73b229 14 *
15 * PostScript is a trademark of Adobe Systems, Inc.
16 *
17 * This code and any derivative of it may be used and distributed
18 * freely under the terms of the GNU General Public License when
19 * used with GNU Ghostscript or its derivatives. Use of the code
20 * (or any derivative of it) with software other than GNU
21 * GhostScript (or its derivatives) is governed by the CUPS license
22 * agreement.
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
5a738aea 28 * ppdLocalize() - Localize the PPD file to the current locale.
75bd9771 29 * ppdLocalizeAttr() - Localize an attribute.
5a738aea
MS
30 * ppdLocalizeIPPReason() - Get the localized version of a cupsIPPReason
31 * attribute.
32 * ppdLocalizeMarkerName() - Get the localized version of a marker-names
33 * attribute value.
a603edef
MS
34 * _ppdFreeLanguages() - Free an array of languages from _ppdGetLanguages.
35 * _ppdGetLanguages() - Get an array of languages from a PPD file.
36 * _ppdHashName() - Generate a hash value for a device or profile
37 * name.
38 * _ppdLocalizedAttr() - Find a localized attribute.
5a738aea 39 * ppd_ll_CC() - Get the current locale names.
fa73b229 40 */
41
42/*
43 * Include necessary headers.
44 */
45
46#include "globals.h"
a603edef 47#include "ppd-private.h"
fa73b229 48#include "debug.h"
49
50
51/*
52 * Local functions...
53 */
54
a603edef 55static void ppd_ll_CC(char *ll_CC, int ll_CC_size);
fa73b229 56
57
58/*
59 * 'ppdLocalize()' - Localize the PPD file to the current locale.
89d46774 60 *
bc44d920 61 * All groups, options, and choices are localized, as are ICC profile
62 * descriptions, printer presets, and custom option parameters. Each
63 * localized string uses the UTF-8 character encoding.
64 *
426c6a59 65 * @since CUPS 1.2/Mac OS X 10.5@
fa73b229 66 */
67
68int /* O - 0 on success, -1 on error */
69ppdLocalize(ppd_file_t *ppd) /* I - PPD file */
70{
71 int i, j, k; /* Looping vars */
72 ppd_group_t *group; /* Current group */
73 ppd_option_t *option; /* Current option */
74 ppd_choice_t *choice; /* Current choice */
75 ppd_coption_t *coption; /* Current custom option */
76 ppd_cparam_t *cparam; /* Current custom parameter */
bc44d920 77 ppd_attr_t *attr, /* Current attribute */
78 *locattr; /* Localized attribute */
fa73b229 79 char ckeyword[PPD_MAX_NAME], /* Custom keyword */
a603edef 80 ll_CC[6]; /* Language + country locale */
fa73b229 81
82
83 /*
84 * Range check input...
85 */
86
d09495fa 87 DEBUG_printf(("ppdLocalize(ppd=%p)\n", ppd));
88
fa73b229 89 if (!ppd)
90 return (-1);
91
92 /*
93 * Get the default language...
94 */
95
a603edef 96 ppd_ll_CC(ll_CC, sizeof(ll_CC));
d09495fa 97
fa73b229 98 /*
99 * Now lookup all of the groups, options, choices, etc.
100 */
101
102 for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++)
103 {
a603edef
MS
104 if ((locattr = _ppdLocalizedAttr(ppd, "Translation", group->name,
105 ll_CC)) != NULL)
bc44d920 106 strlcpy(group->text, locattr->text, sizeof(group->text));
fa73b229 107
108 for (j = group->num_options, option = group->options; j > 0; j --, option ++)
109 {
a603edef
MS
110 if ((locattr = _ppdLocalizedAttr(ppd, "Translation", option->keyword,
111 ll_CC)) != NULL)
bc44d920 112 strlcpy(option->text, locattr->text, sizeof(option->text));
fa73b229 113
114 for (k = option->num_choices, choice = option->choices;
115 k > 0;
116 k --, choice ++)
117 {
b9faaae1
MS
118 if (strcmp(choice->choice, "Custom") ||
119 !ppdFindCustomOption(ppd, option->keyword))
a603edef
MS
120 locattr = _ppdLocalizedAttr(ppd, option->keyword, choice->choice,
121 ll_CC);
fa73b229 122 else
123 {
124 snprintf(ckeyword, sizeof(ckeyword), "Custom%s", option->keyword);
125
a603edef 126 locattr = _ppdLocalizedAttr(ppd, ckeyword, "True", ll_CC);
fa73b229 127 }
128
bc44d920 129 if (locattr)
130 strlcpy(choice->text, locattr->text, sizeof(choice->text));
fa73b229 131 }
132 }
133 }
134
135 /*
136 * Translate any custom parameters...
137 */
138
139 for (coption = (ppd_coption_t *)cupsArrayFirst(ppd->coptions);
140 coption;
141 coption = (ppd_coption_t *)cupsArrayNext(ppd->coptions))
142 {
143 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
144 cparam;
145 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
146 {
147 snprintf(ckeyword, sizeof(ckeyword), "ParamCustom%s", coption->keyword);
148
a603edef
MS
149 if ((locattr = _ppdLocalizedAttr(ppd, ckeyword, cparam->name,
150 ll_CC)) != NULL)
bc44d920 151 strlcpy(cparam->text, locattr->text, sizeof(cparam->text));
fa73b229 152 }
153 }
154
f42414bf 155 /*
156 * Translate ICC profile names...
157 */
158
159 if ((attr = ppdFindAttr(ppd, "APCustomColorMatchingName", NULL)) != NULL)
160 {
a603edef
MS
161 if ((locattr = _ppdLocalizedAttr(ppd, "APCustomColorMatchingName",
162 attr->spec, ll_CC)) != NULL)
bc44d920 163 strlcpy(attr->text, locattr->text, sizeof(attr->text));
f42414bf 164 }
165
166 for (attr = ppdFindAttr(ppd, "cupsICCProfile", NULL);
167 attr;
168 attr = ppdFindNextAttr(ppd, "cupsICCProfile", NULL))
169 {
170 cupsArraySave(ppd->sorted_attrs);
171
a603edef
MS
172 if ((locattr = _ppdLocalizedAttr(ppd, "cupsICCProfile", attr->spec,
173 ll_CC)) != NULL)
bc44d920 174 strlcpy(attr->text, locattr->text, sizeof(attr->text));
f42414bf 175
176 cupsArrayRestore(ppd->sorted_attrs);
177 }
178
09a101d6 179 /*
180 * Translate printer presets...
181 */
182
183 for (attr = ppdFindAttr(ppd, "APPrinterPreset", NULL);
184 attr;
185 attr = ppdFindNextAttr(ppd, "APPrinterPreset", NULL))
186 {
187 cupsArraySave(ppd->sorted_attrs);
188
a603edef
MS
189 if ((locattr = _ppdLocalizedAttr(ppd, "APPrinterPreset", attr->spec,
190 ll_CC)) != NULL)
bc44d920 191 strlcpy(attr->text, locattr->text, sizeof(attr->text));
09a101d6 192
193 cupsArrayRestore(ppd->sorted_attrs);
194 }
195
fa73b229 196 return (0);
197}
198
199
75bd9771
MS
200/*
201 * 'ppdLocalizeAttr()' - Localize an attribute.
202 *
203 * This function uses the current locale to find the localized attribute for
204 * the given main and option keywords. If no localized version of the
205 * attribute exists for the current locale, the unlocalized version is returned.
206 */
207
208ppd_attr_t * /* O - Localized attribute or @code NULL@ if none exists */
209ppdLocalizeAttr(ppd_file_t *ppd, /* I - PPD file */
210 const char *keyword, /* I - Main keyword */
211 const char *spec) /* I - Option keyword or @code NULL@ for none */
212{
213 ppd_attr_t *locattr; /* Localized attribute */
214 char ll_CC[6]; /* Language + country locale */
215
216
217 /*
218 * Get the default language...
219 */
220
221 ppd_ll_CC(ll_CC, sizeof(ll_CC));
222
223 /*
224 * Find the localized attribute...
225 */
226
227 if (spec)
228 locattr = _ppdLocalizedAttr(ppd, keyword, spec, ll_CC);
229 else
230 locattr = _ppdLocalizedAttr(ppd, "Translation", keyword, ll_CC);
231
232 if (!locattr)
233 locattr = ppdFindAttr(ppd, keyword, spec);
234
235 return (locattr);
236}
237
238
fa73b229 239/*
bc44d920 240 * 'ppdLocalizeIPPReason()' - Get the localized version of a cupsIPPReason
241 * attribute.
242 *
243 * This function uses the current locale to find the corresponding reason
244 * text or URI from the attribute value. If "scheme" is NULL or "text",
245 * the returned value contains human-readable (UTF-8) text from the translation
246 * string or attribute value. Otherwise the corresponding URI is returned.
247 *
248 * If no value of the requested scheme can be found, NULL is returned.
249 *
426c6a59 250 * @since CUPS 1.3/Mac OS X 10.5@
bc44d920 251 */
252
253const char * /* O - Value or NULL if not found */
254ppdLocalizeIPPReason(
255 ppd_file_t *ppd, /* I - PPD file */
256 const char *reason, /* I - IPP reason keyword to look up */
257 const char *scheme, /* I - URI scheme or NULL for text */
258 char *buffer, /* I - Value buffer */
259 size_t bufsize) /* I - Size of value buffer */
260{
261 ppd_attr_t *locattr; /* Localized attribute */
262 char ll_CC[6], /* Language + country locale */
bc44d920 263 *bufptr, /* Pointer into buffer */
264 *bufend, /* Pointer to end of buffer */
265 *valptr; /* Pointer into value */
266 int ch, /* Hex-encoded character */
267 schemelen; /* Length of scheme name */
268
269
270 /*
271 * Range check input...
272 */
273
274 if (buffer)
275 *buffer = '\0';
276
277 if (!ppd || !reason || (scheme && !*scheme) ||
278 !buffer || bufsize < PPD_MAX_TEXT)
279 return (NULL);
280
281 /*
282 * Get the default language...
283 */
284
a603edef 285 ppd_ll_CC(ll_CC, sizeof(ll_CC));
bc44d920 286
287 /*
288 * Find the localized attribute...
289 */
290
a603edef
MS
291 if ((locattr = _ppdLocalizedAttr(ppd, "cupsIPPReason", reason,
292 ll_CC)) == NULL)
bc44d920 293 locattr = ppdFindAttr(ppd, "cupsIPPReason", reason);
294
295 if (!locattr)
296 return (NULL);
297
298 /*
299 * Now find the value we need...
300 */
301
302 bufend = buffer + bufsize - 1;
303
304 if (!scheme || !strcmp(scheme, "text"))
305 {
306 /*
307 * Copy a text value (either the translation text or text:... URIs from
308 * the value...
309 */
310
311 strlcpy(buffer, locattr->text, bufsize);
312
313 for (valptr = locattr->value, bufptr = buffer; *valptr && bufptr < bufend;)
314 {
315 if (!strncmp(valptr, "text:", 5))
316 {
317 /*
318 * Decode text: URI and add to the buffer...
319 */
320
bc44d920 321 valptr += 5;
322
323 while (*valptr && !isspace(*valptr & 255) && bufptr < bufend)
324 {
325 if (*valptr == '%' && isxdigit(valptr[1] & 255) &&
326 isxdigit(valptr[2] & 255))
327 {
328 /*
329 * Pull a hex-encoded character from the URI...
330 */
331
332 valptr ++;
333
334 if (isdigit(*valptr & 255))
335 ch = (*valptr - '0') << 4;
336 else
337 ch = (tolower(*valptr) - 'a' + 10) << 4;
338 valptr ++;
339
340 if (isdigit(*valptr & 255))
341 *bufptr++ = ch | (*valptr - '0');
342 else
343 *bufptr++ = ch | (tolower(*valptr) - 'a' + 10);
344 valptr ++;
345 }
346 else if (*valptr == '+')
347 {
348 *bufptr++ = ' ';
349 valptr ++;
350 }
351 else
352 *bufptr++ = *valptr++;
353 }
354 }
355 else
356 {
357 /*
358 * Skip this URI...
359 */
360
361 while (*valptr && !isspace(*valptr & 255))
362 valptr++;
363 }
364
365 /*
366 * Skip whitespace...
367 */
368
369 while (isspace(*valptr & 255))
370 valptr ++;
371 }
372
373 if (bufptr > buffer)
374 *bufptr = '\0';
375
376 return (buffer);
377 }
378 else
379 {
380 /*
381 * Copy a URI...
382 */
383
384 schemelen = strlen(scheme);
385 if (scheme[schemelen - 1] == ':') /* Force scheme to be just the name */
386 schemelen --;
387
388 for (valptr = locattr->value, bufptr = buffer; *valptr && bufptr < bufend;)
389 {
390 if ((!strncmp(valptr, scheme, schemelen) && valptr[schemelen] == ':') ||
391 (*valptr == '/' && !strcmp(scheme, "file")))
392 {
393 /*
394 * Copy URI...
395 */
396
397 while (*valptr && !isspace(*valptr & 255) && bufptr < bufend)
398 *bufptr++ = *valptr++;
399
400 *bufptr = '\0';
401
402 return (buffer);
403 }
404 else
405 {
406 /*
407 * Skip this URI...
408 */
409
410 while (*valptr && !isspace(*valptr & 255))
411 valptr++;
412 }
413
414 /*
415 * Skip whitespace...
416 */
417
418 while (isspace(*valptr & 255))
419 valptr ++;
420 }
421
422 return (NULL);
423 }
424}
425
426
5a738aea
MS
427/*
428 * 'ppdLocalizeMarkerName()' - Get the localized version of a marker-names
429 * attribute value.
430 *
431 * This function uses the current locale to find the corresponding name
432 * text from the attribute value. If no localized text for the requested
433 * name can be found, @code NULL@ is returned.
434 *
435 * @since CUPS 1.4@
436 */
437
438const char * /* O - Value or @code NULL@ if not found */
439ppdLocalizeMarkerName(
440 ppd_file_t *ppd, /* I - PPD file */
441 const char *name) /* I - Marker name to look up */
442{
443 ppd_attr_t *locattr; /* Localized attribute */
a603edef 444 char ll_CC[6]; /* Language + country locale */
5a738aea
MS
445
446
447 /*
448 * Range check input...
449 */
450
451 if (!ppd || !name)
452 return (NULL);
453
454 /*
455 * Get the default language...
456 */
457
a603edef 458 ppd_ll_CC(ll_CC, sizeof(ll_CC));
5a738aea
MS
459
460 /*
461 * Find the localized attribute...
462 */
463
a603edef
MS
464 if ((locattr = _ppdLocalizedAttr(ppd, "cupsMarkerName", name,
465 ll_CC)) == NULL)
5a738aea
MS
466 locattr = ppdFindAttr(ppd, "cupsMarkerName", name);
467
468 return (locattr ? locattr->text : NULL);
469}
470
471
bc44d920 472/*
a603edef 473 * '_ppdFreeLanguages()' - Free an array of languages from _ppdGetLanguages.
bc44d920 474 */
475
a603edef
MS
476void
477_ppdFreeLanguages(
478 cups_array_t *languages) /* I - Languages array */
bc44d920 479{
a603edef
MS
480 char *language; /* Current language */
481
482
483 for (language = (char *)cupsArrayFirst(languages);
484 language;
485 language = (char *)cupsArrayNext(languages))
486 free(language);
487
488 cupsArrayDelete(languages);
489}
490
491
492/*
493 * '_ppdGetLanguages()' - Get an array of languages from a PPD file.
494 */
495
496cups_array_t * /* O - Languages array */
497_ppdGetLanguages(ppd_file_t *ppd) /* I - PPD file */
498{
499 cups_array_t *languages; /* Languages array */
500 ppd_attr_t *attr; /* cupsLanguages attribute */
501 char *value, /* Copy of attribute value */
502 *start, /* Start of current language */
503 *ptr; /* Pointer into languages */
bc44d920 504
505
506 /*
a603edef 507 * See if we have a cupsLanguages attribute...
bc44d920 508 */
509
a603edef
MS
510 if ((attr = ppdFindAttr(ppd, "cupsLanguages", NULL)) == NULL || !attr->value)
511 return (NULL);
bc44d920 512
513 /*
a603edef 514 * Yes, load the list...
bc44d920 515 */
516
a603edef
MS
517 if ((languages = cupsArrayNew((cups_array_func_t)strcmp, NULL)) == NULL)
518 return (NULL);
db1f069b 519
a603edef
MS
520 if ((value = strdup(attr->value)) == NULL)
521 {
522 cupsArrayDelete(languages);
523 return (NULL);
524 }
bc44d920 525
a603edef 526 for (ptr = value; *ptr;)
bc44d920 527 {
528 /*
a603edef 529 * Skip leading whitespace...
bc44d920 530 */
531
a603edef
MS
532 while (isspace(*ptr & 255))
533 ptr ++;
534
535 if (!*ptr)
536 break;
537
538 /*
539 * Find the end of this language name...
540 */
541
542 for (start = ptr; *ptr && !isspace(*ptr & 255); ptr ++);
543
544 if (*ptr)
545 *ptr++ = '\0';
546
547 if (!strcmp(start, "en"))
548 continue;
549
550 cupsArrayAdd(languages, strdup(start));
551 }
552
553 /*
554 * Free the temporary string and return either an array with one or more
555 * values or a NULL pointer...
556 */
557
558 free(value);
559
560 if (cupsArrayCount(languages) == 0)
561 {
562 cupsArrayDelete(languages);
563 return (NULL);
bc44d920 564 }
a603edef
MS
565 else
566 return (languages);
567}
568
569
570/*
571 * '_ppdHashName()' - Generate a hash value for a device or profile name.
572 *
573 * This function is primarily used on Mac OS X, but is generally accessible
574 * since cupstestppd needs to check for profile name collisions in PPD files...
575 */
576
577unsigned /* O - Hash value */
578_ppdHashName(const char *name) /* I - Name to hash */
579{
580 int mult; /* Multiplier */
581 unsigned hash = 0; /* Hash value */
582
bc44d920 583
a603edef
MS
584 for (mult = 1; *name && mult <= 128; mult ++, name ++)
585 hash += (*name & 255) * mult;
586
587 return (hash);
bc44d920 588}
589
590
591/*
a603edef 592 * '_ppdLocalizedAttr()' - Find a localized attribute.
fa73b229 593 */
594
a603edef
MS
595ppd_attr_t * /* O - Localized attribute or NULL */
596_ppdLocalizedAttr(ppd_file_t *ppd, /* I - PPD file */
597 const char *keyword, /* I - Main keyword */
598 const char *spec, /* I - Option keyword */
599 const char *ll_CC) /* I - Language + country locale */
fa73b229 600{
601 char lkeyword[PPD_MAX_NAME]; /* Localization keyword */
602 ppd_attr_t *attr; /* Current attribute */
603
604
a603edef
MS
605 DEBUG_printf(("_ppdLocalizedAttr(ppd=%p, keyword=\"%s\", spec=\"%s\", "
606 "ll_CC=\"%s\")\n", ppd, keyword, spec, ll_CC));
d09495fa 607
fa73b229 608 /*
609 * Look for Keyword.ll_CC, then Keyword.ll...
610 */
611
d09495fa 612 snprintf(lkeyword, sizeof(lkeyword), "%s.%s", ll_CC, keyword);
fa73b229 613 if ((attr = ppdFindAttr(ppd, lkeyword, spec)) == NULL)
614 {
c9fc04c6 615 snprintf(lkeyword, sizeof(lkeyword), "%2.2s.%s", ll_CC, keyword);
fa73b229 616 attr = ppdFindAttr(ppd, lkeyword, spec);
7594b224 617
db1f069b 618 if (!attr)
7594b224 619 {
a603edef 620 if (!strncmp(ll_CC, "ja", 2))
db1f069b
MS
621 {
622 /*
623 * Due to a bug in the CUPS DDK 1.1.0 ppdmerge program, Japanese
624 * PPD files were incorrectly assigned "jp" as the locale name
625 * instead of "ja". Support both the old (incorrect) and new
626 * locale names for Japanese...
627 */
7594b224 628
db1f069b
MS
629 snprintf(lkeyword, sizeof(lkeyword), "jp.%s", keyword);
630 attr = ppdFindAttr(ppd, lkeyword, spec);
631 }
a603edef 632 else if (!strncmp(ll_CC, "no", 2))
db1f069b
MS
633 {
634 /*
635 * Norway has two languages, "Bokmal" (the primary one)
636 * and "Nynorsk" (new Norwegian); we map "no" to "nb" here as
637 * recommended by the locale folks...
638 */
639
640 snprintf(lkeyword, sizeof(lkeyword), "nb.%s", keyword);
641 attr = ppdFindAttr(ppd, lkeyword, spec);
642 }
7594b224 643 }
fa73b229 644 }
645
d09495fa 646#ifdef DEBUG
647 if (attr)
ae71f5de
MS
648 DEBUG_printf(("_ppdLocalizedAttr: *%s %s/%s: \"%s\"\n", attr->name,
649 attr->spec, attr->text, attr->value ? attr->value : ""));
d09495fa 650 else
ae71f5de 651 DEBUG_puts("_ppdLocalizedAttr: NOT FOUND");
d09495fa 652#endif /* DEBUG */
653
bc44d920 654 return (attr);
fa73b229 655}
656
657
658/*
a603edef
MS
659 * 'ppd_ll_CC()' - Get the current locale names.
660 */
661
662static void
663ppd_ll_CC(char *ll_CC, /* O - Country-specific locale name */
664 int ll_CC_size) /* I - Size of country-specific name */
665{
666 cups_lang_t *lang; /* Current language */
667
668
669 /*
670 * Get the current locale...
671 */
672
673 if ((lang = cupsLangDefault()) == NULL)
674 {
675 strlcpy(ll_CC, "en_US", ll_CC_size);
676 return;
677 }
678
679 /*
680 * Copy the locale name...
681 */
682
683 strlcpy(ll_CC, lang->language, ll_CC_size);
684
685 if (strlen(ll_CC) == 2)
686 {
687 /*
688 * Map "ll" to primary/origin country locales to have the best
689 * chance of finding a match...
690 */
691
692 if (!strcmp(ll_CC, "cs"))
693 strlcpy(ll_CC, "cs_CZ", ll_CC_size);
694 else if (!strcmp(ll_CC, "en"))
695 strlcpy(ll_CC, "en_US", ll_CC_size);
696 else if (!strcmp(ll_CC, "ja"))
697 strlcpy(ll_CC, "ja_JP", ll_CC_size);
698 else if (!strcmp(ll_CC, "sv"))
699 strlcpy(ll_CC, "sv_SE", ll_CC_size);
700 else if (!strcmp(ll_CC, "zh")) /* Simplified Chinese */
701 strlcpy(ll_CC, "zh_CN", ll_CC_size);
a603edef
MS
702 }
703
704 DEBUG_printf(("ppd_ll_CC: lang->language=\"%s\", ll_CC=\"%s\"...\n",
705 lang->language, ll_CC));
706}
707
708
709/*
75bd9771 710 * End of "$Id: localize.c 7679 2008-06-19 23:37:45Z mike $".
fa73b229 711 */