]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/localize.c
2 * "$Id: localize.c 7363 2008-03-03 22:19:24Z 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 * ppdLocalizeIPPReason() - Get the localized version of a cupsIPPReason
31 * ppdLocalizeMarkerName() - Get the localized version of a marker-names
33 * _ppdFreeLanguages() - Free an array of languages from _ppdGetLanguages.
34 * _ppdGetLanguages() - Get an array of languages from a PPD file.
35 * _ppdHashName() - Generate a hash value for a device or profile
37 * _ppdLocalizedAttr() - Find a localized attribute.
38 * ppd_ll_CC() - Get the current locale names.
42 * Include necessary headers.
46 #include "ppd-private.h"
54 static void 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.
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)\n", 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 locattr
= _ppdLocalizedAttr(ppd
, option
->keyword
, choice
->choice
,
122 snprintf(ckeyword
, sizeof(ckeyword
), "Custom%s", option
->keyword
);
124 locattr
= _ppdLocalizedAttr(ppd
, ckeyword
, "True", ll_CC
);
128 strlcpy(choice
->text
, locattr
->text
, sizeof(choice
->text
));
134 * Translate any custom parameters...
137 for (coption
= (ppd_coption_t
*)cupsArrayFirst(ppd
->coptions
);
139 coption
= (ppd_coption_t
*)cupsArrayNext(ppd
->coptions
))
141 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
143 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
145 snprintf(ckeyword
, sizeof(ckeyword
), "ParamCustom%s", coption
->keyword
);
147 if ((locattr
= _ppdLocalizedAttr(ppd
, ckeyword
, cparam
->name
,
149 strlcpy(cparam
->text
, locattr
->text
, sizeof(cparam
->text
));
154 * Translate ICC profile names...
157 if ((attr
= ppdFindAttr(ppd
, "APCustomColorMatchingName", NULL
)) != NULL
)
159 if ((locattr
= _ppdLocalizedAttr(ppd
, "APCustomColorMatchingName",
160 attr
->spec
, ll_CC
)) != NULL
)
161 strlcpy(attr
->text
, locattr
->text
, sizeof(attr
->text
));
164 for (attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
166 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
168 cupsArraySave(ppd
->sorted_attrs
);
170 if ((locattr
= _ppdLocalizedAttr(ppd
, "cupsICCProfile", attr
->spec
,
172 strlcpy(attr
->text
, locattr
->text
, sizeof(attr
->text
));
174 cupsArrayRestore(ppd
->sorted_attrs
);
178 * Translate printer presets...
181 for (attr
= ppdFindAttr(ppd
, "APPrinterPreset", NULL
);
183 attr
= ppdFindNextAttr(ppd
, "APPrinterPreset", NULL
))
185 cupsArraySave(ppd
->sorted_attrs
);
187 if ((locattr
= _ppdLocalizedAttr(ppd
, "APPrinterPreset", attr
->spec
,
189 strlcpy(attr
->text
, locattr
->text
, sizeof(attr
->text
));
191 cupsArrayRestore(ppd
->sorted_attrs
);
199 * 'ppdLocalizeIPPReason()' - Get the localized version of a cupsIPPReason
202 * This function uses the current locale to find the corresponding reason
203 * text or URI from the attribute value. If "scheme" is NULL or "text",
204 * the returned value contains human-readable (UTF-8) text from the translation
205 * string or attribute value. Otherwise the corresponding URI is returned.
207 * If no value of the requested scheme can be found, NULL is returned.
212 const char * /* O - Value or NULL if not found */
213 ppdLocalizeIPPReason(
214 ppd_file_t
*ppd
, /* I - PPD file */
215 const char *reason
, /* I - IPP reason keyword to look up */
216 const char *scheme
, /* I - URI scheme or NULL for text */
217 char *buffer
, /* I - Value buffer */
218 size_t bufsize
) /* I - Size of value buffer */
220 ppd_attr_t
*locattr
; /* Localized attribute */
221 char ll_CC
[6], /* Language + country locale */
222 *bufptr
, /* Pointer into buffer */
223 *bufend
, /* Pointer to end of buffer */
224 *valptr
; /* Pointer into value */
225 int ch
, /* Hex-encoded character */
226 schemelen
; /* Length of scheme name */
230 * Range check input...
236 if (!ppd
|| !reason
|| (scheme
&& !*scheme
) ||
237 !buffer
|| bufsize
< PPD_MAX_TEXT
)
241 * Get the default language...
244 ppd_ll_CC(ll_CC
, sizeof(ll_CC
));
247 * Find the localized attribute...
250 if ((locattr
= _ppdLocalizedAttr(ppd
, "cupsIPPReason", reason
,
252 locattr
= ppdFindAttr(ppd
, "cupsIPPReason", reason
);
258 * Now find the value we need...
261 bufend
= buffer
+ bufsize
- 1;
263 if (!scheme
|| !strcmp(scheme
, "text"))
266 * Copy a text value (either the translation text or text:... URIs from
270 strlcpy(buffer
, locattr
->text
, bufsize
);
272 for (valptr
= locattr
->value
, bufptr
= buffer
; *valptr
&& bufptr
< bufend
;)
274 if (!strncmp(valptr
, "text:", 5))
277 * Decode text: URI and add to the buffer...
281 *bufptr
++ = ' '; /* Add leading whitespace */
285 while (*valptr
&& !isspace(*valptr
& 255) && bufptr
< bufend
)
287 if (*valptr
== '%' && isxdigit(valptr
[1] & 255) &&
288 isxdigit(valptr
[2] & 255))
291 * Pull a hex-encoded character from the URI...
296 if (isdigit(*valptr
& 255))
297 ch
= (*valptr
- '0') << 4;
299 ch
= (tolower(*valptr
) - 'a' + 10) << 4;
302 if (isdigit(*valptr
& 255))
303 *bufptr
++ = ch
| (*valptr
- '0');
305 *bufptr
++ = ch
| (tolower(*valptr
) - 'a' + 10);
308 else if (*valptr
== '+')
314 *bufptr
++ = *valptr
++;
323 while (*valptr
&& !isspace(*valptr
& 255))
331 while (isspace(*valptr
& 255))
346 schemelen
= strlen(scheme
);
347 if (scheme
[schemelen
- 1] == ':') /* Force scheme to be just the name */
350 for (valptr
= locattr
->value
, bufptr
= buffer
; *valptr
&& bufptr
< bufend
;)
352 if ((!strncmp(valptr
, scheme
, schemelen
) && valptr
[schemelen
] == ':') ||
353 (*valptr
== '/' && !strcmp(scheme
, "file")))
359 while (*valptr
&& !isspace(*valptr
& 255) && bufptr
< bufend
)
360 *bufptr
++ = *valptr
++;
372 while (*valptr
&& !isspace(*valptr
& 255))
380 while (isspace(*valptr
& 255))
390 * 'ppdLocalizeMarkerName()' - Get the localized version of a marker-names
393 * This function uses the current locale to find the corresponding name
394 * text from the attribute value. If no localized text for the requested
395 * name can be found, @code NULL@ is returned.
400 const char * /* O - Value or @code NULL@ if not found */
401 ppdLocalizeMarkerName(
402 ppd_file_t
*ppd
, /* I - PPD file */
403 const char *name
) /* I - Marker name to look up */
405 ppd_attr_t
*locattr
; /* Localized attribute */
406 char ll_CC
[6]; /* Language + country locale */
410 * Range check input...
417 * Get the default language...
420 ppd_ll_CC(ll_CC
, sizeof(ll_CC
));
423 * Find the localized attribute...
426 if ((locattr
= _ppdLocalizedAttr(ppd
, "cupsMarkerName", name
,
428 locattr
= ppdFindAttr(ppd
, "cupsMarkerName", name
);
430 return (locattr
? locattr
->text
: NULL
);
435 * '_ppdFreeLanguages()' - Free an array of languages from _ppdGetLanguages.
440 cups_array_t
*languages
) /* I - Languages array */
442 char *language
; /* Current language */
445 for (language
= (char *)cupsArrayFirst(languages
);
447 language
= (char *)cupsArrayNext(languages
))
450 cupsArrayDelete(languages
);
455 * '_ppdGetLanguages()' - Get an array of languages from a PPD file.
458 cups_array_t
* /* O - Languages array */
459 _ppdGetLanguages(ppd_file_t
*ppd
) /* I - PPD file */
461 cups_array_t
*languages
; /* Languages array */
462 ppd_attr_t
*attr
; /* cupsLanguages attribute */
463 char *value
, /* Copy of attribute value */
464 *start
, /* Start of current language */
465 *ptr
; /* Pointer into languages */
469 * See if we have a cupsLanguages attribute...
472 if ((attr
= ppdFindAttr(ppd
, "cupsLanguages", NULL
)) == NULL
|| !attr
->value
)
476 * Yes, load the list...
479 if ((languages
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
)) == NULL
)
482 if ((value
= strdup(attr
->value
)) == NULL
)
484 cupsArrayDelete(languages
);
488 for (ptr
= value
; *ptr
;)
491 * Skip leading whitespace...
494 while (isspace(*ptr
& 255))
501 * Find the end of this language name...
504 for (start
= ptr
; *ptr
&& !isspace(*ptr
& 255); ptr
++);
509 if (!strcmp(start
, "en"))
512 cupsArrayAdd(languages
, strdup(start
));
516 * Free the temporary string and return either an array with one or more
517 * values or a NULL pointer...
522 if (cupsArrayCount(languages
) == 0)
524 cupsArrayDelete(languages
);
533 * '_ppdHashName()' - Generate a hash value for a device or profile name.
535 * This function is primarily used on Mac OS X, but is generally accessible
536 * since cupstestppd needs to check for profile name collisions in PPD files...
539 unsigned /* O - Hash value */
540 _ppdHashName(const char *name
) /* I - Name to hash */
542 int mult
; /* Multiplier */
543 unsigned hash
= 0; /* Hash value */
546 for (mult
= 1; *name
&& mult
<= 128; mult
++, name
++)
547 hash
+= (*name
& 255) * mult
;
554 * '_ppdLocalizedAttr()' - Find a localized attribute.
557 ppd_attr_t
* /* O - Localized attribute or NULL */
558 _ppdLocalizedAttr(ppd_file_t
*ppd
, /* I - PPD file */
559 const char *keyword
, /* I - Main keyword */
560 const char *spec
, /* I - Option keyword */
561 const char *ll_CC
) /* I - Language + country locale */
563 char lkeyword
[PPD_MAX_NAME
]; /* Localization keyword */
564 ppd_attr_t
*attr
; /* Current attribute */
567 DEBUG_printf(("_ppdLocalizedAttr(ppd=%p, keyword=\"%s\", spec=\"%s\", "
568 "ll_CC=\"%s\")\n", ppd
, keyword
, spec
, ll_CC
));
571 * Look for Keyword.ll_CC, then Keyword.ll...
574 snprintf(lkeyword
, sizeof(lkeyword
), "%s.%s", ll_CC
, keyword
);
575 if ((attr
= ppdFindAttr(ppd
, lkeyword
, spec
)) == NULL
)
577 snprintf(lkeyword
, sizeof(lkeyword
), "%2.2s.%s", ll_CC
, keyword
);
578 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
582 if (!strncmp(ll_CC
, "ja", 2))
585 * Due to a bug in the CUPS DDK 1.1.0 ppdmerge program, Japanese
586 * PPD files were incorrectly assigned "jp" as the locale name
587 * instead of "ja". Support both the old (incorrect) and new
588 * locale names for Japanese...
591 snprintf(lkeyword
, sizeof(lkeyword
), "jp.%s", keyword
);
592 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
594 else if (!strncmp(ll_CC
, "no", 2))
597 * Norway has two languages, "Bokmal" (the primary one)
598 * and "Nynorsk" (new Norwegian); we map "no" to "nb" here as
599 * recommended by the locale folks...
602 snprintf(lkeyword
, sizeof(lkeyword
), "nb.%s", keyword
);
603 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
610 DEBUG_printf(("_ppdLocalizedAttr: *%s %s/%s: \"%s\"\n", attr
->name
,
611 attr
->spec
, attr
->text
, attr
->value
? attr
->value
: ""));
613 DEBUG_puts("_ppdLocalizedAttr: NOT FOUND");
621 * 'ppd_ll_CC()' - Get the current locale names.
625 ppd_ll_CC(char *ll_CC
, /* O - Country-specific locale name */
626 int ll_CC_size
) /* I - Size of country-specific name */
628 cups_lang_t
*lang
; /* Current language */
632 * Get the current locale...
635 if ((lang
= cupsLangDefault()) == NULL
)
637 strlcpy(ll_CC
, "en_US", ll_CC_size
);
642 * Copy the locale name...
645 strlcpy(ll_CC
, lang
->language
, ll_CC_size
);
647 if (strlen(ll_CC
) == 2)
650 * Map "ll" to primary/origin country locales to have the best
651 * chance of finding a match...
654 if (!strcmp(ll_CC
, "cs"))
655 strlcpy(ll_CC
, "cs_CZ", ll_CC_size
);
656 else if (!strcmp(ll_CC
, "en"))
657 strlcpy(ll_CC
, "en_US", ll_CC_size
);
658 else if (!strcmp(ll_CC
, "ja"))
659 strlcpy(ll_CC
, "ja_JP", ll_CC_size
);
660 else if (!strcmp(ll_CC
, "sv"))
661 strlcpy(ll_CC
, "sv_SE", ll_CC_size
);
662 else if (!strcmp(ll_CC
, "zh")) /* Simplified Chinese */
663 strlcpy(ll_CC
, "zh_CN", ll_CC_size
);
664 else if (ll_CC_size
>= 6)
667 ll_CC
[3] = toupper(ll_CC
[0] & 255);
668 ll_CC
[4] = toupper(ll_CC
[1] & 255);
673 DEBUG_printf(("ppd_ll_CC: lang->language=\"%s\", ll_CC=\"%s\"...\n",
674 lang
->language
, ll_CC
));
679 * End of "$Id: localize.c 7363 2008-03-03 22:19:24Z mike $".