]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/localize.c
4 * PPD localization routines for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers.
31 #include "cups-private.h"
32 #include "ppd-private.h"
39 static cups_lang_t
*ppd_ll_CC(char *ll_CC
, size_t ll_CC_size
);
43 * 'ppdLocalize()' - Localize the PPD file to the current locale.
45 * All groups, options, and choices are localized, as are ICC profile
46 * descriptions, printer presets, and custom option parameters. Each
47 * localized string uses the UTF-8 character encoding.
49 * @since CUPS 1.2/OS X 10.5@
52 int /* O - 0 on success, -1 on error */
53 ppdLocalize(ppd_file_t
*ppd
) /* I - PPD file */
55 int i
, j
, k
; /* Looping vars */
56 ppd_group_t
*group
; /* Current group */
57 ppd_option_t
*option
; /* Current option */
58 ppd_choice_t
*choice
; /* Current choice */
59 ppd_coption_t
*coption
; /* Current custom option */
60 ppd_cparam_t
*cparam
; /* Current custom parameter */
61 ppd_attr_t
*attr
, /* Current attribute */
62 *locattr
; /* Localized attribute */
63 char ckeyword
[PPD_MAX_NAME
], /* Custom keyword */
64 ll_CC
[6]; /* Language + country locale */
68 * Range check input...
71 DEBUG_printf(("ppdLocalize(ppd=%p)", ppd
));
77 * Get the default language...
80 ppd_ll_CC(ll_CC
, sizeof(ll_CC
));
83 * Now lookup all of the groups, options, choices, etc.
86 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
88 if ((locattr
= _ppdLocalizedAttr(ppd
, "Translation", group
->name
,
90 strlcpy(group
->text
, locattr
->text
, sizeof(group
->text
));
92 for (j
= group
->num_options
, option
= group
->options
; j
> 0; j
--, option
++)
94 if ((locattr
= _ppdLocalizedAttr(ppd
, "Translation", option
->keyword
,
96 strlcpy(option
->text
, locattr
->text
, sizeof(option
->text
));
98 for (k
= option
->num_choices
, choice
= option
->choices
;
102 if (strcmp(choice
->choice
, "Custom") ||
103 !ppdFindCustomOption(ppd
, option
->keyword
))
104 locattr
= _ppdLocalizedAttr(ppd
, option
->keyword
, choice
->choice
,
108 snprintf(ckeyword
, sizeof(ckeyword
), "Custom%s", option
->keyword
);
110 locattr
= _ppdLocalizedAttr(ppd
, ckeyword
, "True", ll_CC
);
114 strlcpy(choice
->text
, locattr
->text
, sizeof(choice
->text
));
120 * Translate any custom parameters...
123 for (coption
= (ppd_coption_t
*)cupsArrayFirst(ppd
->coptions
);
125 coption
= (ppd_coption_t
*)cupsArrayNext(ppd
->coptions
))
127 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
129 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
131 snprintf(ckeyword
, sizeof(ckeyword
), "ParamCustom%s", coption
->keyword
);
133 if ((locattr
= _ppdLocalizedAttr(ppd
, ckeyword
, cparam
->name
,
135 strlcpy(cparam
->text
, locattr
->text
, sizeof(cparam
->text
));
140 * Translate ICC profile names...
143 if ((attr
= ppdFindAttr(ppd
, "APCustomColorMatchingName", NULL
)) != NULL
)
145 if ((locattr
= _ppdLocalizedAttr(ppd
, "APCustomColorMatchingName",
146 attr
->spec
, ll_CC
)) != NULL
)
147 strlcpy(attr
->text
, locattr
->text
, sizeof(attr
->text
));
150 for (attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
152 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
154 cupsArraySave(ppd
->sorted_attrs
);
156 if ((locattr
= _ppdLocalizedAttr(ppd
, "cupsICCProfile", attr
->spec
,
158 strlcpy(attr
->text
, locattr
->text
, sizeof(attr
->text
));
160 cupsArrayRestore(ppd
->sorted_attrs
);
164 * Translate printer presets...
167 for (attr
= ppdFindAttr(ppd
, "APPrinterPreset", NULL
);
169 attr
= ppdFindNextAttr(ppd
, "APPrinterPreset", NULL
))
171 cupsArraySave(ppd
->sorted_attrs
);
173 if ((locattr
= _ppdLocalizedAttr(ppd
, "APPrinterPreset", attr
->spec
,
175 strlcpy(attr
->text
, locattr
->text
, sizeof(attr
->text
));
177 cupsArrayRestore(ppd
->sorted_attrs
);
185 * 'ppdLocalizeAttr()' - Localize an attribute.
187 * This function uses the current locale to find the localized attribute for
188 * the given main and option keywords. If no localized version of the
189 * attribute exists for the current locale, the unlocalized version is returned.
192 ppd_attr_t
* /* O - Localized attribute or @code NULL@ if none exists */
193 ppdLocalizeAttr(ppd_file_t
*ppd
, /* I - PPD file */
194 const char *keyword
, /* I - Main keyword */
195 const char *spec
) /* I - Option keyword or @code NULL@ for none */
197 ppd_attr_t
*locattr
; /* Localized attribute */
198 char ll_CC
[6]; /* Language + country locale */
202 * Get the default language...
205 ppd_ll_CC(ll_CC
, sizeof(ll_CC
));
208 * Find the localized attribute...
212 locattr
= _ppdLocalizedAttr(ppd
, keyword
, spec
, ll_CC
);
214 locattr
= _ppdLocalizedAttr(ppd
, "Translation", keyword
, ll_CC
);
217 locattr
= ppdFindAttr(ppd
, keyword
, spec
);
224 * 'ppdLocalizeIPPReason()' - Get the localized version of a cupsIPPReason
227 * This function uses the current locale to find the corresponding reason
228 * text or URI from the attribute value. If "scheme" is NULL or "text",
229 * the returned value contains human-readable (UTF-8) text from the translation
230 * string or attribute value. Otherwise the corresponding URI is returned.
232 * If no value of the requested scheme can be found, NULL is returned.
234 * @since CUPS 1.3/OS X 10.5@
237 const char * /* O - Value or NULL if not found */
238 ppdLocalizeIPPReason(
239 ppd_file_t
*ppd
, /* I - PPD file */
240 const char *reason
, /* I - IPP reason keyword to look up */
241 const char *scheme
, /* I - URI scheme or NULL for text */
242 char *buffer
, /* I - Value buffer */
243 size_t bufsize
) /* I - Size of value buffer */
245 cups_lang_t
*lang
; /* Current language */
246 ppd_attr_t
*locattr
; /* Localized attribute */
247 char ll_CC
[6], /* Language + country locale */
248 *bufptr
, /* Pointer into buffer */
249 *bufend
, /* Pointer to end of buffer */
250 *valptr
; /* Pointer into value */
251 int ch
; /* Hex-encoded character */
252 size_t schemelen
; /* Length of scheme name */
256 * Range check input...
262 if (!ppd
|| !reason
|| (scheme
&& !*scheme
) ||
263 !buffer
|| bufsize
< PPD_MAX_TEXT
)
267 * Get the default language...
270 lang
= ppd_ll_CC(ll_CC
, sizeof(ll_CC
));
273 * Find the localized attribute...
276 if ((locattr
= _ppdLocalizedAttr(ppd
, "cupsIPPReason", reason
,
278 locattr
= ppdFindAttr(ppd
, "cupsIPPReason", reason
);
282 if (lang
&& (!scheme
|| !strcmp(scheme
, "text")))
285 * Try to localize a standard printer-state-reason keyword...
288 const char *message
= NULL
; /* Localized message */
290 if (!strncmp(reason
, "media-needed", 12))
291 message
= _("Load paper.");
292 else if (!strncmp(reason
, "media-jam", 9))
293 message
= _("Paper jam.");
294 else if (!strncmp(reason
, "offline", 7) ||
295 !strncmp(reason
, "shutdown", 8))
296 message
= _("The printer is not connected.");
297 else if (!strncmp(reason
, "toner-low", 9))
298 message
= _("The printer is low on toner.");
299 else if (!strncmp(reason
, "toner-empty", 11))
300 message
= _("The printer may be out of toner.");
301 else if (!strncmp(reason
, "cover-open", 10))
302 message
= _("The printer's cover is open.");
303 else if (!strncmp(reason
, "interlock-open", 14))
304 message
= _("The printer's interlock is open.");
305 else if (!strncmp(reason
, "door-open", 9))
306 message
= _("The printer's door is open.");
307 else if (!strncmp(reason
, "input-tray-missing", 18))
308 message
= _("Paper tray is missing.");
309 else if (!strncmp(reason
, "media-low", 9))
310 message
= _("Paper tray is almost empty.");
311 else if (!strncmp(reason
, "media-empty", 11))
312 message
= _("Paper tray is empty.");
313 else if (!strncmp(reason
, "output-tray-missing", 19))
314 message
= _("Output bin is missing.");
315 else if (!strncmp(reason
, "output-area-almost-full", 23))
316 message
= _("Output bin is almost full.");
317 else if (!strncmp(reason
, "output-area-full", 16))
318 message
= _("Output bin is full.");
319 else if (!strncmp(reason
, "marker-supply-low", 17))
320 message
= _("The printer is low on ink.");
321 else if (!strncmp(reason
, "marker-supply-empty", 19))
322 message
= _("The printer may be out of ink.");
323 else if (!strncmp(reason
, "marker-waste-almost-full", 24))
324 message
= _("The printer's waste bin is almost full.");
325 else if (!strncmp(reason
, "marker-waste-full", 17))
326 message
= _("The printer's waste bin is full.");
327 else if (!strncmp(reason
, "fuser-over-temp", 15))
328 message
= _("The fuser's temperature is high.");
329 else if (!strncmp(reason
, "fuser-under-temp", 16))
330 message
= _("The fuser's temperature is low.");
331 else if (!strncmp(reason
, "opc-near-eol", 12))
332 message
= _("The optical photoconductor will need to be replaced soon.");
333 else if (!strncmp(reason
, "opc-life-over", 13))
334 message
= _("The optical photoconductor needs to be replaced.");
335 else if (!strncmp(reason
, "developer-low", 13))
336 message
= _("The developer unit will need to be replaced soon.");
337 else if (!strncmp(reason
, "developer-empty", 15))
338 message
= _("The developer unit needs to be replaced.");
342 strlcpy(buffer
, _cupsLangString(lang
, message
), bufsize
);
351 * Now find the value we need...
354 bufend
= buffer
+ bufsize
- 1;
356 if (!scheme
|| !strcmp(scheme
, "text"))
359 * Copy a text value (either the translation text or text:... URIs from
363 strlcpy(buffer
, locattr
->text
, bufsize
);
365 for (valptr
= locattr
->value
, bufptr
= buffer
; *valptr
&& bufptr
< bufend
;)
367 if (!strncmp(valptr
, "text:", 5))
370 * Decode text: URI and add to the buffer...
375 while (*valptr
&& !_cups_isspace(*valptr
) && bufptr
< bufend
)
377 if (*valptr
== '%' && isxdigit(valptr
[1] & 255) &&
378 isxdigit(valptr
[2] & 255))
381 * Pull a hex-encoded character from the URI...
386 if (isdigit(*valptr
& 255))
387 ch
= (*valptr
- '0') << 4;
389 ch
= (tolower(*valptr
) - 'a' + 10) << 4;
392 if (isdigit(*valptr
& 255))
393 *bufptr
++ = (char)(ch
| (*valptr
- '0'));
395 *bufptr
++ = (char)(ch
| (tolower(*valptr
) - 'a' + 10));
398 else if (*valptr
== '+')
404 *bufptr
++ = *valptr
++;
413 while (*valptr
&& !_cups_isspace(*valptr
))
421 while (_cups_isspace(*valptr
))
436 schemelen
= strlen(scheme
);
437 if (scheme
[schemelen
- 1] == ':') /* Force scheme to be just the name */
440 for (valptr
= locattr
->value
, bufptr
= buffer
; *valptr
&& bufptr
< bufend
;)
442 if ((!strncmp(valptr
, scheme
, schemelen
) && valptr
[schemelen
] == ':') ||
443 (*valptr
== '/' && !strcmp(scheme
, "file")))
449 while (*valptr
&& !_cups_isspace(*valptr
) && bufptr
< bufend
)
450 *bufptr
++ = *valptr
++;
462 while (*valptr
&& !_cups_isspace(*valptr
))
470 while (_cups_isspace(*valptr
))
480 * 'ppdLocalizeMarkerName()' - Get the localized version of a marker-names
483 * This function uses the current locale to find the corresponding name
484 * text from the attribute value. If no localized text for the requested
485 * name can be found, @code NULL@ is returned.
487 * @since CUPS 1.4/OS X 10.6@
490 const char * /* O - Value or @code NULL@ if not found */
491 ppdLocalizeMarkerName(
492 ppd_file_t
*ppd
, /* I - PPD file */
493 const char *name
) /* I - Marker name to look up */
495 ppd_attr_t
*locattr
; /* Localized attribute */
496 char ll_CC
[6]; /* Language + country locale */
500 * Range check input...
507 * Get the default language...
510 ppd_ll_CC(ll_CC
, sizeof(ll_CC
));
513 * Find the localized attribute...
516 if ((locattr
= _ppdLocalizedAttr(ppd
, "cupsMarkerName", name
,
518 locattr
= ppdFindAttr(ppd
, "cupsMarkerName", name
);
520 return (locattr
? locattr
->text
: NULL
);
525 * '_ppdFreeLanguages()' - Free an array of languages from _ppdGetLanguages.
530 cups_array_t
*languages
) /* I - Languages array */
532 char *language
; /* Current language */
535 for (language
= (char *)cupsArrayFirst(languages
);
537 language
= (char *)cupsArrayNext(languages
))
540 cupsArrayDelete(languages
);
545 * '_ppdGetLanguages()' - Get an array of languages from a PPD file.
548 cups_array_t
* /* O - Languages array */
549 _ppdGetLanguages(ppd_file_t
*ppd
) /* I - PPD file */
551 cups_array_t
*languages
; /* Languages array */
552 ppd_attr_t
*attr
; /* cupsLanguages attribute */
553 char *value
, /* Copy of attribute value */
554 *start
, /* Start of current language */
555 *ptr
; /* Pointer into languages */
559 * See if we have a cupsLanguages attribute...
562 if ((attr
= ppdFindAttr(ppd
, "cupsLanguages", NULL
)) == NULL
|| !attr
->value
)
566 * Yes, load the list...
569 if ((languages
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
)) == NULL
)
572 if ((value
= strdup(attr
->value
)) == NULL
)
574 cupsArrayDelete(languages
);
578 for (ptr
= value
; *ptr
;)
581 * Skip leading whitespace...
584 while (_cups_isspace(*ptr
))
591 * Find the end of this language name...
594 for (start
= ptr
; *ptr
&& !_cups_isspace(*ptr
); ptr
++);
599 if (!strcmp(start
, "en"))
602 cupsArrayAdd(languages
, strdup(start
));
606 * Free the temporary string and return either an array with one or more
607 * values or a NULL pointer...
612 if (cupsArrayCount(languages
) == 0)
614 cupsArrayDelete(languages
);
623 * '_ppdHashName()' - Generate a hash value for a device or profile name.
625 * This function is primarily used on OS X, but is generally accessible
626 * since cupstestppd needs to check for profile name collisions in PPD files...
629 unsigned /* O - Hash value */
630 _ppdHashName(const char *name
) /* I - Name to hash */
632 unsigned mult
, /* Multiplier */
633 hash
= 0; /* Hash value */
636 for (mult
= 1; *name
&& mult
<= 128; mult
++, name
++)
637 hash
+= (*name
& 255) * mult
;
644 * '_ppdLocalizedAttr()' - Find a localized attribute.
647 ppd_attr_t
* /* O - Localized attribute or NULL */
648 _ppdLocalizedAttr(ppd_file_t
*ppd
, /* I - PPD file */
649 const char *keyword
, /* I - Main keyword */
650 const char *spec
, /* I - Option keyword */
651 const char *ll_CC
) /* I - Language + country locale */
653 char lkeyword
[PPD_MAX_NAME
]; /* Localization keyword */
654 ppd_attr_t
*attr
; /* Current attribute */
657 DEBUG_printf(("4_ppdLocalizedAttr(ppd=%p, keyword=\"%s\", spec=\"%s\", "
658 "ll_CC=\"%s\")", ppd
, keyword
, spec
, ll_CC
));
661 * Look for Keyword.ll_CC, then Keyword.ll...
664 snprintf(lkeyword
, sizeof(lkeyword
), "%s.%s", ll_CC
, keyword
);
665 if ((attr
= ppdFindAttr(ppd
, lkeyword
, spec
)) == NULL
)
667 snprintf(lkeyword
, sizeof(lkeyword
), "%2.2s.%s", ll_CC
, keyword
);
668 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
672 if (!strncmp(ll_CC
, "ja", 2))
675 * Due to a bug in the CUPS DDK 1.1.0 ppdmerge program, Japanese
676 * PPD files were incorrectly assigned "jp" as the locale name
677 * instead of "ja". Support both the old (incorrect) and new
678 * locale names for Japanese...
681 snprintf(lkeyword
, sizeof(lkeyword
), "jp.%s", keyword
);
682 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
684 else if (!strncmp(ll_CC
, "no", 2))
687 * Norway has two languages, "Bokmal" (the primary one)
688 * and "Nynorsk" (new Norwegian); we map "no" to "nb" here as
689 * recommended by the locale folks...
692 snprintf(lkeyword
, sizeof(lkeyword
), "nb.%s", keyword
);
693 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
700 DEBUG_printf(("5_ppdLocalizedAttr: *%s %s/%s: \"%s\"\n", attr
->name
,
701 attr
->spec
, attr
->text
, attr
->value
? attr
->value
: ""));
703 DEBUG_puts("5_ppdLocalizedAttr: NOT FOUND");
711 * 'ppd_ll_CC()' - Get the current locale names.
714 static cups_lang_t
* /* O - Current language */
715 ppd_ll_CC(char *ll_CC
, /* O - Country-specific locale name */
716 size_t ll_CC_size
) /* I - Size of country-specific name */
718 cups_lang_t
*lang
; /* Current language */
722 * Get the current locale...
725 if ((lang
= cupsLangDefault()) == NULL
)
727 strlcpy(ll_CC
, "en_US", ll_CC_size
);
732 * Copy the locale name...
735 strlcpy(ll_CC
, lang
->language
, ll_CC_size
);
737 if (strlen(ll_CC
) == 2)
740 * Map "ll" to primary/origin country locales to have the best
741 * chance of finding a match...
744 if (!strcmp(ll_CC
, "cs"))
745 strlcpy(ll_CC
, "cs_CZ", ll_CC_size
);
746 else if (!strcmp(ll_CC
, "en"))
747 strlcpy(ll_CC
, "en_US", ll_CC_size
);
748 else if (!strcmp(ll_CC
, "ja"))
749 strlcpy(ll_CC
, "ja_JP", ll_CC_size
);
750 else if (!strcmp(ll_CC
, "sv"))
751 strlcpy(ll_CC
, "sv_SE", ll_CC_size
);
752 else if (!strcmp(ll_CC
, "zh")) /* Simplified Chinese */
753 strlcpy(ll_CC
, "zh_CN", ll_CC_size
);
756 DEBUG_printf(("8ppd_ll_CC: lang->language=\"%s\", ll_CC=\"%s\"...",
757 lang
->language
, ll_CC
));