2 * "$Id: localize.c 10270 2012-02-13 17:13:21Z mike $"
4 * PPD localization routines for CUPS.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
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/".
15 * PostScript is a trademark of Adobe Systems, Inc.
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
24 * This file is subject to the Apple OS-Developed Software exception.
28 * ppdLocalize() - Localize the PPD file to the current locale.
29 * ppdLocalizeAttr() - Localize an attribute.
30 * ppdLocalizeIPPReason() - Get the localized version of a cupsIPPReason
32 * ppdLocalizeMarkerName() - Get the localized version of a marker-names
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
38 * _ppdLocalizedAttr() - Find a localized attribute.
39 * ppd_ll_CC() - Get the current locale names.
43 * Include necessary headers.
46 #include "cups-private.h"
47 #include "ppd-private.h"
54 static cups_lang_t *ppd_ll_CC(char *ll_CC, int ll_CC_size);
58 * 'ppdLocalize()' - Localize the PPD file to the current locale.
60 * All groups, options, and choices are localized, as are ICC profile
61 * descriptions, printer presets, and custom option parameters. Each
62 * localized string uses the UTF-8 character encoding.
64 * @since CUPS 1.2/Mac OS X 10.5@
67 int /* O - 0 on success, -1 on error */
68 ppdLocalize(ppd_file_t *ppd) /* I - PPD file */
70 int i, j, k; /* Looping vars */
71 ppd_group_t *group; /* Current group */
72 ppd_option_t *option; /* Current option */
73 ppd_choice_t *choice; /* Current choice */
74 ppd_coption_t *coption; /* Current custom option */
75 ppd_cparam_t *cparam; /* Current custom parameter */
76 ppd_attr_t *attr, /* Current attribute */
77 *locattr; /* Localized attribute */
78 char ckeyword[PPD_MAX_NAME], /* Custom keyword */
79 ll_CC[6]; /* Language + country locale */
83 * Range check input...
86 DEBUG_printf(("ppdLocalize(ppd=%p)", ppd));
92 * Get the default language...
95 ppd_ll_CC(ll_CC, sizeof(ll_CC));
98 * Now lookup all of the groups, options, choices, etc.
101 for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++)
103 if ((locattr = _ppdLocalizedAttr(ppd, "Translation", group->name,
105 strlcpy(group->text, locattr->text, sizeof(group->text));
107 for (j = group->num_options, option = group->options; j > 0; j --, option ++)
109 if ((locattr = _ppdLocalizedAttr(ppd, "Translation", option->keyword,
111 strlcpy(option->text, locattr->text, sizeof(option->text));
113 for (k = option->num_choices, choice = option->choices;
117 if (strcmp(choice->choice, "Custom") ||
118 !ppdFindCustomOption(ppd, option->keyword))
119 locattr = _ppdLocalizedAttr(ppd, option->keyword, choice->choice,
123 snprintf(ckeyword, sizeof(ckeyword), "Custom%s", option->keyword);
125 locattr = _ppdLocalizedAttr(ppd, ckeyword, "True", ll_CC);
129 strlcpy(choice->text, locattr->text, sizeof(choice->text));
135 * Translate any custom parameters...
138 for (coption = (ppd_coption_t *)cupsArrayFirst(ppd->coptions);
140 coption = (ppd_coption_t *)cupsArrayNext(ppd->coptions))
142 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
144 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
146 snprintf(ckeyword, sizeof(ckeyword), "ParamCustom%s", coption->keyword);
148 if ((locattr = _ppdLocalizedAttr(ppd, ckeyword, cparam->name,
150 strlcpy(cparam->text, locattr->text, sizeof(cparam->text));
155 * Translate ICC profile names...
158 if ((attr = ppdFindAttr(ppd, "APCustomColorMatchingName", NULL)) != NULL)
160 if ((locattr = _ppdLocalizedAttr(ppd, "APCustomColorMatchingName",
161 attr->spec, ll_CC)) != NULL)
162 strlcpy(attr->text, locattr->text, sizeof(attr->text));
165 for (attr = ppdFindAttr(ppd, "cupsICCProfile", NULL);
167 attr = ppdFindNextAttr(ppd, "cupsICCProfile", NULL))
169 cupsArraySave(ppd->sorted_attrs);
171 if ((locattr = _ppdLocalizedAttr(ppd, "cupsICCProfile", attr->spec,
173 strlcpy(attr->text, locattr->text, sizeof(attr->text));
175 cupsArrayRestore(ppd->sorted_attrs);
179 * Translate printer presets...
182 for (attr = ppdFindAttr(ppd, "APPrinterPreset", NULL);
184 attr = ppdFindNextAttr(ppd, "APPrinterPreset", NULL))
186 cupsArraySave(ppd->sorted_attrs);
188 if ((locattr = _ppdLocalizedAttr(ppd, "APPrinterPreset", attr->spec,
190 strlcpy(attr->text, locattr->text, sizeof(attr->text));
192 cupsArrayRestore(ppd->sorted_attrs);
200 * 'ppdLocalizeAttr()' - Localize an attribute.
202 * This function uses the current locale to find the localized attribute for
203 * the given main and option keywords. If no localized version of the
204 * attribute exists for the current locale, the unlocalized version is returned.
207 ppd_attr_t * /* O - Localized attribute or @code NULL@ if none exists */
208 ppdLocalizeAttr(ppd_file_t *ppd, /* I - PPD file */
209 const char *keyword, /* I - Main keyword */
210 const char *spec) /* I - Option keyword or @code NULL@ for none */
212 ppd_attr_t *locattr; /* Localized attribute */
213 char ll_CC[6]; /* Language + country locale */
217 * Get the default language...
220 ppd_ll_CC(ll_CC, sizeof(ll_CC));
223 * Find the localized attribute...
227 locattr = _ppdLocalizedAttr(ppd, keyword, spec, ll_CC);
229 locattr = _ppdLocalizedAttr(ppd, "Translation", keyword, ll_CC);
232 locattr = ppdFindAttr(ppd, keyword, spec);
239 * 'ppdLocalizeIPPReason()' - Get the localized version of a cupsIPPReason
242 * This function uses the current locale to find the corresponding reason
243 * text or URI from the attribute value. If "scheme" is NULL or "text",
244 * the returned value contains human-readable (UTF-8) text from the translation
245 * string or attribute value. Otherwise the corresponding URI is returned.
247 * If no value of the requested scheme can be found, NULL is returned.
249 * @since CUPS 1.3/Mac OS X 10.5@
252 const char * /* O - Value or NULL if not found */
253 ppdLocalizeIPPReason(
254 ppd_file_t *ppd, /* I - PPD file */
255 const char *reason, /* I - IPP reason keyword to look up */
256 const char *scheme, /* I - URI scheme or NULL for text */
257 char *buffer, /* I - Value buffer */
258 size_t bufsize) /* I - Size of value buffer */
260 cups_lang_t *lang; /* Current language */
261 ppd_attr_t *locattr; /* Localized attribute */
262 char ll_CC[6], /* Language + country locale */
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 */
271 * Range check input...
277 if (!ppd || !reason || (scheme && !*scheme) ||
278 !buffer || bufsize < PPD_MAX_TEXT)
282 * Get the default language...
285 lang = ppd_ll_CC(ll_CC, sizeof(ll_CC));
288 * Find the localized attribute...
291 if ((locattr = _ppdLocalizedAttr(ppd, "cupsIPPReason", reason,
293 locattr = ppdFindAttr(ppd, "cupsIPPReason", reason);
297 if (lang && (!scheme || !strcmp(scheme, "text")))
300 * Try to localize a standard printer-state-reason keyword...
303 const char *message = NULL; /* Localized message */
305 if (!strncmp(reason, "media-needed", 12))
306 message = _("The paper tray needs to be filled.");
307 else if (!strncmp(reason, "media-jam", 9))
308 message = _("There is a paper jam.");
309 else if (!strncmp(reason, "offline", 7) ||
310 !strncmp(reason, "shutdown", 8))
311 message = _("The printer is not connected.");
312 else if (!strncmp(reason, "toner-low", 9))
313 message = _("The printer is running low on toner.");
314 else if (!strncmp(reason, "toner-empty", 11))
315 message = _("The printer may be out of toner.");
316 else if (!strncmp(reason, "cover-open", 10))
317 message = _("The printer's cover is open.");
318 else if (!strncmp(reason, "interlock-open", 14))
319 message = _("The printer's interlock is open.");
320 else if (!strncmp(reason, "door-open", 9))
321 message = _("The printer's door is open.");
322 else if (!strncmp(reason, "input-tray-missing", 18))
323 message = _("The paper tray is missing.");
324 else if (!strncmp(reason, "media-low", 9))
325 message = _("The paper tray is almost empty.");
326 else if (!strncmp(reason, "media-empty", 11))
327 message = _("The paper tray is empty.");
328 else if (!strncmp(reason, "output-tray-missing", 19))
329 message = _("The output bin is missing.");
330 else if (!strncmp(reason, "output-area-almost-full", 23))
331 message = _("The output bin is almost full.");
332 else if (!strncmp(reason, "output-area-full", 16))
333 message = _("The output bin is full.");
334 else if (!strncmp(reason, "marker-supply-low", 17))
335 message = _("The printer is running low on ink.");
336 else if (!strncmp(reason, "marker-supply-empty", 19))
337 message = _("The printer is out of ink.");
338 else if (!strncmp(reason, "marker-waste-almost-full", 24))
339 message = _("The printer's waste bin is almost full.");
340 else if (!strncmp(reason, "marker-waste-full", 17))
341 message = _("The printer's waste bin is full.");
342 else if (!strncmp(reason, "fuser-over-temp", 15))
343 message = _("The fuser's temperature is high.");
344 else if (!strncmp(reason, "fuser-under-temp", 16))
345 message = _("The fuser's temperature is low.");
346 else if (!strncmp(reason, "opc-near-eol", 12))
347 message = _("The optical photoconductor will need to be replaced soon.");
348 else if (!strncmp(reason, "opc-life-over", 13))
349 message = _("The optical photoconductor needs to be replaced.");
350 else if (!strncmp(reason, "developer-low", 13))
351 message = _("The developer unit will need to be replaced soon.");
352 else if (!strncmp(reason, "developer-empty", 15))
353 message = _("The developer unit needs to be replaced.");
357 strlcpy(buffer, _cupsLangString(lang, message), bufsize);
366 * Now find the value we need...
369 bufend = buffer + bufsize - 1;
371 if (!scheme || !strcmp(scheme, "text"))
374 * Copy a text value (either the translation text or text:... URIs from
378 strlcpy(buffer, locattr->text, bufsize);
380 for (valptr = locattr->value, bufptr = buffer; *valptr && bufptr < bufend;)
382 if (!strncmp(valptr, "text:", 5))
385 * Decode text: URI and add to the buffer...
390 while (*valptr && !_cups_isspace(*valptr) && bufptr < bufend)
392 if (*valptr == '%' && isxdigit(valptr[1] & 255) &&
393 isxdigit(valptr[2] & 255))
396 * Pull a hex-encoded character from the URI...
401 if (isdigit(*valptr & 255))
402 ch = (*valptr - '0') << 4;
404 ch = (tolower(*valptr) - 'a' + 10) << 4;
407 if (isdigit(*valptr & 255))
408 *bufptr++ = ch | (*valptr - '0');
410 *bufptr++ = ch | (tolower(*valptr) - 'a' + 10);
413 else if (*valptr == '+')
419 *bufptr++ = *valptr++;
428 while (*valptr && !_cups_isspace(*valptr))
436 while (_cups_isspace(*valptr))
451 schemelen = strlen(scheme);
452 if (scheme[schemelen - 1] == ':') /* Force scheme to be just the name */
455 for (valptr = locattr->value, bufptr = buffer; *valptr && bufptr < bufend;)
457 if ((!strncmp(valptr, scheme, schemelen) && valptr[schemelen] == ':') ||
458 (*valptr == '/' && !strcmp(scheme, "file")))
464 while (*valptr && !_cups_isspace(*valptr) && bufptr < bufend)
465 *bufptr++ = *valptr++;
477 while (*valptr && !_cups_isspace(*valptr))
485 while (_cups_isspace(*valptr))
495 * 'ppdLocalizeMarkerName()' - Get the localized version of a marker-names
498 * This function uses the current locale to find the corresponding name
499 * text from the attribute value. If no localized text for the requested
500 * name can be found, @code NULL@ is returned.
502 * @since CUPS 1.4/Mac OS X 10.6@
505 const char * /* O - Value or @code NULL@ if not found */
506 ppdLocalizeMarkerName(
507 ppd_file_t *ppd, /* I - PPD file */
508 const char *name) /* I - Marker name to look up */
510 ppd_attr_t *locattr; /* Localized attribute */
511 char ll_CC[6]; /* Language + country locale */
515 * Range check input...
522 * Get the default language...
525 ppd_ll_CC(ll_CC, sizeof(ll_CC));
528 * Find the localized attribute...
531 if ((locattr = _ppdLocalizedAttr(ppd, "cupsMarkerName", name,
533 locattr = ppdFindAttr(ppd, "cupsMarkerName", name);
535 return (locattr ? locattr->text : NULL);
540 * '_ppdFreeLanguages()' - Free an array of languages from _ppdGetLanguages.
545 cups_array_t *languages) /* I - Languages array */
547 char *language; /* Current language */
550 for (language = (char *)cupsArrayFirst(languages);
552 language = (char *)cupsArrayNext(languages))
555 cupsArrayDelete(languages);
560 * '_ppdGetLanguages()' - Get an array of languages from a PPD file.
563 cups_array_t * /* O - Languages array */
564 _ppdGetLanguages(ppd_file_t *ppd) /* I - PPD file */
566 cups_array_t *languages; /* Languages array */
567 ppd_attr_t *attr; /* cupsLanguages attribute */
568 char *value, /* Copy of attribute value */
569 *start, /* Start of current language */
570 *ptr; /* Pointer into languages */
574 * See if we have a cupsLanguages attribute...
577 if ((attr = ppdFindAttr(ppd, "cupsLanguages", NULL)) == NULL || !attr->value)
581 * Yes, load the list...
584 if ((languages = cupsArrayNew((cups_array_func_t)strcmp, NULL)) == NULL)
587 if ((value = strdup(attr->value)) == NULL)
589 cupsArrayDelete(languages);
593 for (ptr = value; *ptr;)
596 * Skip leading whitespace...
599 while (_cups_isspace(*ptr))
606 * Find the end of this language name...
609 for (start = ptr; *ptr && !_cups_isspace(*ptr); ptr ++);
614 if (!strcmp(start, "en"))
617 cupsArrayAdd(languages, strdup(start));
621 * Free the temporary string and return either an array with one or more
622 * values or a NULL pointer...
627 if (cupsArrayCount(languages) == 0)
629 cupsArrayDelete(languages);
638 * '_ppdHashName()' - Generate a hash value for a device or profile name.
640 * This function is primarily used on Mac OS X, but is generally accessible
641 * since cupstestppd needs to check for profile name collisions in PPD files...
644 unsigned /* O - Hash value */
645 _ppdHashName(const char *name) /* I - Name to hash */
647 int mult; /* Multiplier */
648 unsigned hash = 0; /* Hash value */
651 for (mult = 1; *name && mult <= 128; mult ++, name ++)
652 hash += (*name & 255) * mult;
659 * '_ppdLocalizedAttr()' - Find a localized attribute.
662 ppd_attr_t * /* O - Localized attribute or NULL */
663 _ppdLocalizedAttr(ppd_file_t *ppd, /* I - PPD file */
664 const char *keyword, /* I - Main keyword */
665 const char *spec, /* I - Option keyword */
666 const char *ll_CC) /* I - Language + country locale */
668 char lkeyword[PPD_MAX_NAME]; /* Localization keyword */
669 ppd_attr_t *attr; /* Current attribute */
672 DEBUG_printf(("4_ppdLocalizedAttr(ppd=%p, keyword=\"%s\", spec=\"%s\", "
673 "ll_CC=\"%s\")", ppd, keyword, spec, ll_CC));
676 * Look for Keyword.ll_CC, then Keyword.ll...
679 snprintf(lkeyword, sizeof(lkeyword), "%s.%s", ll_CC, keyword);
680 if ((attr = ppdFindAttr(ppd, lkeyword, spec)) == NULL)
682 snprintf(lkeyword, sizeof(lkeyword), "%2.2s.%s", ll_CC, keyword);
683 attr = ppdFindAttr(ppd, lkeyword, spec);
687 if (!strncmp(ll_CC, "ja", 2))
690 * Due to a bug in the CUPS DDK 1.1.0 ppdmerge program, Japanese
691 * PPD files were incorrectly assigned "jp" as the locale name
692 * instead of "ja". Support both the old (incorrect) and new
693 * locale names for Japanese...
696 snprintf(lkeyword, sizeof(lkeyword), "jp.%s", keyword);
697 attr = ppdFindAttr(ppd, lkeyword, spec);
699 else if (!strncmp(ll_CC, "no", 2))
702 * Norway has two languages, "Bokmal" (the primary one)
703 * and "Nynorsk" (new Norwegian); we map "no" to "nb" here as
704 * recommended by the locale folks...
707 snprintf(lkeyword, sizeof(lkeyword), "nb.%s", keyword);
708 attr = ppdFindAttr(ppd, lkeyword, spec);
715 DEBUG_printf(("5_ppdLocalizedAttr: *%s %s/%s: \"%s\"\n", attr->name,
716 attr->spec, attr->text, attr->value ? attr->value : ""));
718 DEBUG_puts("5_ppdLocalizedAttr: NOT FOUND");
726 * 'ppd_ll_CC()' - Get the current locale names.
729 static cups_lang_t * /* O - Current language */
730 ppd_ll_CC(char *ll_CC, /* O - Country-specific locale name */
731 int ll_CC_size) /* I - Size of country-specific name */
733 cups_lang_t *lang; /* Current language */
737 * Get the current locale...
740 if ((lang = cupsLangDefault()) == NULL)
742 strlcpy(ll_CC, "en_US", ll_CC_size);
747 * Copy the locale name...
750 strlcpy(ll_CC, lang->language, ll_CC_size);
752 if (strlen(ll_CC) == 2)
755 * Map "ll" to primary/origin country locales to have the best
756 * chance of finding a match...
759 if (!strcmp(ll_CC, "cs"))
760 strlcpy(ll_CC, "cs_CZ", ll_CC_size);
761 else if (!strcmp(ll_CC, "en"))
762 strlcpy(ll_CC, "en_US", ll_CC_size);
763 else if (!strcmp(ll_CC, "ja"))
764 strlcpy(ll_CC, "ja_JP", ll_CC_size);
765 else if (!strcmp(ll_CC, "sv"))
766 strlcpy(ll_CC, "sv_SE", ll_CC_size);
767 else if (!strcmp(ll_CC, "zh")) /* Simplified Chinese */
768 strlcpy(ll_CC, "zh_CN", ll_CC_size);
771 DEBUG_printf(("8ppd_ll_CC: lang->language=\"%s\", ll_CC=\"%s\"...",
772 lang->language, ll_CC));
778 * End of "$Id: localize.c 10270 2012-02-13 17:13:21Z mike $".