]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/localize.c
2 * "$Id: localize.c 7679 2008-06-19 23:37:45Z mike $"
4 * PPD localization routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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.
47 #include "ppd-private.h"
55 static void ppd_ll_CC(char *ll_CC
, int ll_CC_size
);
59 * 'ppdLocalize()' - Localize the PPD file to the current locale.
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.
65 * @since CUPS 1.2/Mac OS X 10.5@
68 int /* O - 0 on success, -1 on error */
69 ppdLocalize(ppd_file_t
*ppd
) /* I - PPD file */
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 */
77 ppd_attr_t
*attr
, /* Current attribute */
78 *locattr
; /* Localized attribute */
79 char ckeyword
[PPD_MAX_NAME
], /* Custom keyword */
80 ll_CC
[6]; /* Language + country locale */
84 * Range check input...
87 DEBUG_printf(("ppdLocalize(ppd=%p)\n", ppd
));
93 * Get the default language...
96 ppd_ll_CC(ll_CC
, sizeof(ll_CC
));
99 * Now lookup all of the groups, options, choices, etc.
102 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
104 if ((locattr
= _ppdLocalizedAttr(ppd
, "Translation", group
->name
,
106 strlcpy(group
->text
, locattr
->text
, sizeof(group
->text
));
108 for (j
= group
->num_options
, option
= group
->options
; j
> 0; j
--, option
++)
110 if ((locattr
= _ppdLocalizedAttr(ppd
, "Translation", option
->keyword
,
112 strlcpy(option
->text
, locattr
->text
, sizeof(option
->text
));
114 for (k
= option
->num_choices
, choice
= option
->choices
;
118 if (strcmp(choice
->choice
, "Custom"))
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 ppd_attr_t
*locattr
; /* Localized attribute */
261 char ll_CC
[6], /* Language + country locale */
262 *bufptr
, /* Pointer into buffer */
263 *bufend
, /* Pointer to end of buffer */
264 *valptr
; /* Pointer into value */
265 int ch
, /* Hex-encoded character */
266 schemelen
; /* Length of scheme name */
270 * Range check input...
276 if (!ppd
|| !reason
|| (scheme
&& !*scheme
) ||
277 !buffer
|| bufsize
< PPD_MAX_TEXT
)
281 * Get the default language...
284 ppd_ll_CC(ll_CC
, sizeof(ll_CC
));
287 * Find the localized attribute...
290 if ((locattr
= _ppdLocalizedAttr(ppd
, "cupsIPPReason", reason
,
292 locattr
= ppdFindAttr(ppd
, "cupsIPPReason", reason
);
298 * Now find the value we need...
301 bufend
= buffer
+ bufsize
- 1;
303 if (!scheme
|| !strcmp(scheme
, "text"))
306 * Copy a text value (either the translation text or text:... URIs from
310 strlcpy(buffer
, locattr
->text
, bufsize
);
312 for (valptr
= locattr
->value
, bufptr
= buffer
; *valptr
&& bufptr
< bufend
;)
314 if (!strncmp(valptr
, "text:", 5))
317 * Decode text: URI and add to the buffer...
322 while (*valptr
&& !isspace(*valptr
& 255) && bufptr
< bufend
)
324 if (*valptr
== '%' && isxdigit(valptr
[1] & 255) &&
325 isxdigit(valptr
[2] & 255))
328 * Pull a hex-encoded character from the URI...
333 if (isdigit(*valptr
& 255))
334 ch
= (*valptr
- '0') << 4;
336 ch
= (tolower(*valptr
) - 'a' + 10) << 4;
339 if (isdigit(*valptr
& 255))
340 *bufptr
++ = ch
| (*valptr
- '0');
342 *bufptr
++ = ch
| (tolower(*valptr
) - 'a' + 10);
345 else if (*valptr
== '+')
351 *bufptr
++ = *valptr
++;
360 while (*valptr
&& !isspace(*valptr
& 255))
368 while (isspace(*valptr
& 255))
383 schemelen
= strlen(scheme
);
384 if (scheme
[schemelen
- 1] == ':') /* Force scheme to be just the name */
387 for (valptr
= locattr
->value
, bufptr
= buffer
; *valptr
&& bufptr
< bufend
;)
389 if ((!strncmp(valptr
, scheme
, schemelen
) && valptr
[schemelen
] == ':') ||
390 (*valptr
== '/' && !strcmp(scheme
, "file")))
396 while (*valptr
&& !isspace(*valptr
& 255) && bufptr
< bufend
)
397 *bufptr
++ = *valptr
++;
409 while (*valptr
&& !isspace(*valptr
& 255))
417 while (isspace(*valptr
& 255))
427 * 'ppdLocalizeMarkerName()' - Get the localized version of a marker-names
430 * This function uses the current locale to find the corresponding name
431 * text from the attribute value. If no localized text for the requested
432 * name can be found, @code NULL@ is returned.
437 const char * /* O - Value or @code NULL@ if not found */
438 ppdLocalizeMarkerName(
439 ppd_file_t
*ppd
, /* I - PPD file */
440 const char *name
) /* I - Marker name to look up */
442 ppd_attr_t
*locattr
; /* Localized attribute */
443 char ll_CC
[6]; /* Language + country locale */
447 * Range check input...
454 * Get the default language...
457 ppd_ll_CC(ll_CC
, sizeof(ll_CC
));
460 * Find the localized attribute...
463 if ((locattr
= _ppdLocalizedAttr(ppd
, "cupsMarkerName", name
,
465 locattr
= ppdFindAttr(ppd
, "cupsMarkerName", name
);
467 return (locattr
? locattr
->text
: NULL
);
472 * '_ppdFreeLanguages()' - Free an array of languages from _ppdGetLanguages.
477 cups_array_t
*languages
) /* I - Languages array */
479 char *language
; /* Current language */
482 for (language
= (char *)cupsArrayFirst(languages
);
484 language
= (char *)cupsArrayNext(languages
))
487 cupsArrayDelete(languages
);
492 * '_ppdGetLanguages()' - Get an array of languages from a PPD file.
495 cups_array_t
* /* O - Languages array */
496 _ppdGetLanguages(ppd_file_t
*ppd
) /* I - PPD file */
498 cups_array_t
*languages
; /* Languages array */
499 ppd_attr_t
*attr
; /* cupsLanguages attribute */
500 char *value
, /* Copy of attribute value */
501 *start
, /* Start of current language */
502 *ptr
; /* Pointer into languages */
506 * See if we have a cupsLanguages attribute...
509 if ((attr
= ppdFindAttr(ppd
, "cupsLanguages", NULL
)) == NULL
|| !attr
->value
)
513 * Yes, load the list...
516 if ((languages
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
)) == NULL
)
519 if ((value
= strdup(attr
->value
)) == NULL
)
521 cupsArrayDelete(languages
);
525 for (ptr
= value
; *ptr
;)
528 * Skip leading whitespace...
531 while (isspace(*ptr
& 255))
538 * Find the end of this language name...
541 for (start
= ptr
; *ptr
&& !isspace(*ptr
& 255); ptr
++);
546 if (!strcmp(start
, "en"))
549 cupsArrayAdd(languages
, strdup(start
));
553 * Free the temporary string and return either an array with one or more
554 * values or a NULL pointer...
559 if (cupsArrayCount(languages
) == 0)
561 cupsArrayDelete(languages
);
570 * '_ppdHashName()' - Generate a hash value for a device or profile name.
572 * This function is primarily used on Mac OS X, but is generally accessible
573 * since cupstestppd needs to check for profile name collisions in PPD files...
576 unsigned /* O - Hash value */
577 _ppdHashName(const char *name
) /* I - Name to hash */
579 int mult
; /* Multiplier */
580 unsigned hash
= 0; /* Hash value */
583 for (mult
= 1; *name
&& mult
<= 128; mult
++, name
++)
584 hash
+= (*name
& 255) * mult
;
591 * '_ppdLocalizedAttr()' - Find a localized attribute.
594 ppd_attr_t
* /* O - Localized attribute or NULL */
595 _ppdLocalizedAttr(ppd_file_t
*ppd
, /* I - PPD file */
596 const char *keyword
, /* I - Main keyword */
597 const char *spec
, /* I - Option keyword */
598 const char *ll_CC
) /* I - Language + country locale */
600 char lkeyword
[PPD_MAX_NAME
]; /* Localization keyword */
601 ppd_attr_t
*attr
; /* Current attribute */
604 DEBUG_printf(("_ppdLocalizedAttr(ppd=%p, keyword=\"%s\", spec=\"%s\", "
605 "ll_CC=\"%s\")\n", ppd
, keyword
, spec
, ll_CC
));
608 * Look for Keyword.ll_CC, then Keyword.ll...
611 snprintf(lkeyword
, sizeof(lkeyword
), "%s.%s", ll_CC
, keyword
);
612 if ((attr
= ppdFindAttr(ppd
, lkeyword
, spec
)) == NULL
)
614 snprintf(lkeyword
, sizeof(lkeyword
), "%2.2s.%s", ll_CC
, keyword
);
615 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
619 if (!strncmp(ll_CC
, "ja", 2))
622 * Due to a bug in the CUPS DDK 1.1.0 ppdmerge program, Japanese
623 * PPD files were incorrectly assigned "jp" as the locale name
624 * instead of "ja". Support both the old (incorrect) and new
625 * locale names for Japanese...
628 snprintf(lkeyword
, sizeof(lkeyword
), "jp.%s", keyword
);
629 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
631 else if (!strncmp(ll_CC
, "no", 2))
634 * Norway has two languages, "Bokmal" (the primary one)
635 * and "Nynorsk" (new Norwegian); we map "no" to "nb" here as
636 * recommended by the locale folks...
639 snprintf(lkeyword
, sizeof(lkeyword
), "nb.%s", keyword
);
640 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
647 DEBUG_printf(("_ppdLocalizedAttr: *%s %s/%s: \"%s\"\n", attr
->name
,
648 attr
->spec
, attr
->text
, attr
->value
? attr
->value
: ""));
650 DEBUG_puts("_ppdLocalizedAttr: NOT FOUND");
658 * 'ppd_ll_CC()' - Get the current locale names.
662 ppd_ll_CC(char *ll_CC
, /* O - Country-specific locale name */
663 int ll_CC_size
) /* I - Size of country-specific name */
665 cups_lang_t
*lang
; /* Current language */
669 * Get the current locale...
672 if ((lang
= cupsLangDefault()) == NULL
)
674 strlcpy(ll_CC
, "en_US", ll_CC_size
);
679 * Copy the locale name...
682 strlcpy(ll_CC
, lang
->language
, ll_CC_size
);
684 if (strlen(ll_CC
) == 2)
687 * Map "ll" to primary/origin country locales to have the best
688 * chance of finding a match...
691 if (!strcmp(ll_CC
, "cs"))
692 strlcpy(ll_CC
, "cs_CZ", ll_CC_size
);
693 else if (!strcmp(ll_CC
, "en"))
694 strlcpy(ll_CC
, "en_US", ll_CC_size
);
695 else if (!strcmp(ll_CC
, "ja"))
696 strlcpy(ll_CC
, "ja_JP", ll_CC_size
);
697 else if (!strcmp(ll_CC
, "sv"))
698 strlcpy(ll_CC
, "sv_SE", ll_CC_size
);
699 else if (!strcmp(ll_CC
, "zh")) /* Simplified Chinese */
700 strlcpy(ll_CC
, "zh_CN", ll_CC_size
);
703 DEBUG_printf(("ppd_ll_CC: lang->language=\"%s\", ll_CC=\"%s\"...\n",
704 lang
->language
, ll_CC
));
709 * End of "$Id: localize.c 7679 2008-06-19 23:37:45Z mike $".