]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/localize.c
2 * "$Id: localize.c 6882 2007-08-29 21:05:10Z 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 * ppd_ll_CC() - Get the current locale names.
34 * ppd_localized_attr() - Find a localized attribute.
38 * Include necessary headers.
49 static void ppd_ll_CC(char *ll_CC
, int ll_CC_size
,
50 char *ll
, int ll_size
);
51 static ppd_attr_t
*ppd_localized_attr(ppd_file_t
*ppd
,
53 const char *spec
, const char *ll_CC
,
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 */
80 ll
[3]; /* Language 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
), ll
, sizeof(ll
));
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
= ppd_localized_attr(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
= ppd_localized_attr(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
= ppd_localized_attr(ppd
, option
->keyword
, choice
->choice
,
123 snprintf(ckeyword
, sizeof(ckeyword
), "Custom%s", option
->keyword
);
125 locattr
= ppd_localized_attr(ppd
, ckeyword
, "True", ll_CC
, ll
);
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
= ppd_localized_attr(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
= ppd_localized_attr(ppd
, "APCustomColorMatchingName",
161 attr
->spec
, ll_CC
, ll
)) != 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
= ppd_localized_attr(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
= ppd_localized_attr(ppd
, "APPrinterPreset", attr
->spec
,
190 strlcpy(attr
->text
, locattr
->text
, sizeof(attr
->text
));
192 cupsArrayRestore(ppd
->sorted_attrs
);
200 * 'ppdLocalizeIPPReason()' - Get the localized version of a cupsIPPReason
203 * This function uses the current locale to find the corresponding reason
204 * text or URI from the attribute value. If "scheme" is NULL or "text",
205 * the returned value contains human-readable (UTF-8) text from the translation
206 * string or attribute value. Otherwise the corresponding URI is returned.
208 * If no value of the requested scheme can be found, NULL is returned.
213 const char * /* O - Value or NULL if not found */
214 ppdLocalizeIPPReason(
215 ppd_file_t
*ppd
, /* I - PPD file */
216 const char *reason
, /* I - IPP reason keyword to look up */
217 const char *scheme
, /* I - URI scheme or NULL for text */
218 char *buffer
, /* I - Value buffer */
219 size_t bufsize
) /* I - Size of value buffer */
221 ppd_attr_t
*locattr
; /* Localized attribute */
222 char ll_CC
[6], /* Language + country locale */
223 ll
[3], /* Language locale */
224 *bufptr
, /* Pointer into buffer */
225 *bufend
, /* Pointer to end of buffer */
226 *valptr
; /* Pointer into value */
227 int ch
, /* Hex-encoded character */
228 schemelen
; /* Length of scheme name */
232 * Range check input...
238 if (!ppd
|| !reason
|| (scheme
&& !*scheme
) ||
239 !buffer
|| bufsize
< PPD_MAX_TEXT
)
243 * Get the default language...
246 ppd_ll_CC(ll_CC
, sizeof(ll_CC
), ll
, sizeof(ll
));
249 * Find the localized attribute...
252 if ((locattr
= ppd_localized_attr(ppd
, "cupsIPPReason", reason
,
254 locattr
= ppdFindAttr(ppd
, "cupsIPPReason", reason
);
260 * Now find the value we need...
263 bufend
= buffer
+ bufsize
- 1;
265 if (!scheme
|| !strcmp(scheme
, "text"))
268 * Copy a text value (either the translation text or text:... URIs from
272 strlcpy(buffer
, locattr
->text
, bufsize
);
274 for (valptr
= locattr
->value
, bufptr
= buffer
; *valptr
&& bufptr
< bufend
;)
276 if (!strncmp(valptr
, "text:", 5))
279 * Decode text: URI and add to the buffer...
283 *bufptr
++ = ' '; /* Add leading whitespace */
287 while (*valptr
&& !isspace(*valptr
& 255) && bufptr
< bufend
)
289 if (*valptr
== '%' && isxdigit(valptr
[1] & 255) &&
290 isxdigit(valptr
[2] & 255))
293 * Pull a hex-encoded character from the URI...
298 if (isdigit(*valptr
& 255))
299 ch
= (*valptr
- '0') << 4;
301 ch
= (tolower(*valptr
) - 'a' + 10) << 4;
304 if (isdigit(*valptr
& 255))
305 *bufptr
++ = ch
| (*valptr
- '0');
307 *bufptr
++ = ch
| (tolower(*valptr
) - 'a' + 10);
310 else if (*valptr
== '+')
316 *bufptr
++ = *valptr
++;
325 while (*valptr
&& !isspace(*valptr
& 255))
333 while (isspace(*valptr
& 255))
348 schemelen
= strlen(scheme
);
349 if (scheme
[schemelen
- 1] == ':') /* Force scheme to be just the name */
352 for (valptr
= locattr
->value
, bufptr
= buffer
; *valptr
&& bufptr
< bufend
;)
354 if ((!strncmp(valptr
, scheme
, schemelen
) && valptr
[schemelen
] == ':') ||
355 (*valptr
== '/' && !strcmp(scheme
, "file")))
361 while (*valptr
&& !isspace(*valptr
& 255) && bufptr
< bufend
)
362 *bufptr
++ = *valptr
++;
374 while (*valptr
&& !isspace(*valptr
& 255))
382 while (isspace(*valptr
& 255))
392 * 'ppdLocalizeMarkerName()' - Get the localized version of a marker-names
395 * This function uses the current locale to find the corresponding name
396 * text from the attribute value. If no localized text for the requested
397 * name can be found, @code NULL@ is returned.
402 const char * /* O - Value or @code NULL@ if not found */
403 ppdLocalizeMarkerName(
404 ppd_file_t
*ppd
, /* I - PPD file */
405 const char *name
) /* I - Marker name to look up */
407 ppd_attr_t
*locattr
; /* Localized attribute */
408 char ll_CC
[6], /* Language + country locale */
409 ll
[3]; /* Language locale */
413 * Range check input...
420 * Get the default language...
423 ppd_ll_CC(ll_CC
, sizeof(ll_CC
), ll
, sizeof(ll
));
426 * Find the localized attribute...
429 if ((locattr
= ppd_localized_attr(ppd
, "cupsMarkerName", name
,
431 locattr
= ppdFindAttr(ppd
, "cupsMarkerName", name
);
433 return (locattr
? locattr
->text
: NULL
);
438 * 'ppd_ll_CC()' - Get the current locale names.
442 ppd_ll_CC(char *ll_CC
, /* O - Country-specific locale name */
443 int ll_CC_size
, /* I - Size of country-specific name */
444 char *ll
, /* O - Generic locale name */
445 int ll_size
) /* I - Size of generic name */
447 cups_lang_t
*lang
; /* Current language */
451 * Get the current locale...
454 if ((lang
= cupsLangDefault()) == NULL
)
456 strlcpy(ll_CC
, "en_US", ll_CC_size
);
457 strlcpy(ll
, "en", ll_size
);
462 * Copy the locale name...
465 strlcpy(ll_CC
, lang
->language
, ll_CC_size
);
466 strlcpy(ll
, lang
->language
, ll_size
);
468 DEBUG_printf(("ll_CC=\"%s\", ll=\"%s\"\n", ll_CC
, ll
));
470 if (strlen(ll_CC
) == 2)
473 * Map "ll" to primary/origin country locales to have the best
474 * chance of finding a match...
477 if (!strcmp(ll_CC
, "cs"))
478 strlcpy(ll_CC
, "cs_CZ", ll_CC_size
);
479 else if (!strcmp(ll_CC
, "en"))
480 strlcpy(ll_CC
, "en_US", ll_CC_size
);
481 else if (!strcmp(ll_CC
, "ja"))
482 strlcpy(ll_CC
, "ja_JP", ll_CC_size
);
483 else if (!strcmp(ll_CC
, "sv"))
484 strlcpy(ll_CC
, "sv_SE", ll_CC_size
);
485 else if (!strcmp(ll_CC
, "zh")) /* Simplified Chinese */
486 strlcpy(ll_CC
, "zh_CN", ll_CC_size
);
487 else if (ll_CC_size
>= 6)
490 ll_CC
[3] = toupper(ll_CC
[0] & 255);
491 ll_CC
[4] = toupper(ll_CC
[1] & 255);
496 DEBUG_printf(("ppd_ll_CC: lang->language=\"%s\", ll=\"%s\", ll_CC=\"%s\"...\n",
497 lang
->language
, ll
, ll_CC
));
502 * 'ppd_localized_attr()' - Find a localized attribute.
505 static ppd_attr_t
* /* O - Localized attribute or NULL */
506 ppd_localized_attr(ppd_file_t
*ppd
, /* I - PPD file */
507 const char *keyword
, /* I - Main keyword */
508 const char *spec
, /* I - Option keyword */
509 const char *ll_CC
, /* I - Language + country locale */
510 const char *ll
) /* I - Language locale */
512 char lkeyword
[PPD_MAX_NAME
]; /* Localization keyword */
513 ppd_attr_t
*attr
; /* Current attribute */
516 DEBUG_printf(("ppd_text(ppd=%p, keyword=\"%s\", spec=\"%s\", "
517 "ll_CC=\"%s\", ll=\"%s\")\n",
518 ppd
, keyword
, spec
, ll_CC
, ll
));
521 * Look for Keyword.ll_CC, then Keyword.ll...
524 snprintf(lkeyword
, sizeof(lkeyword
), "%s.%s", ll_CC
, keyword
);
525 if ((attr
= ppdFindAttr(ppd
, lkeyword
, spec
)) == NULL
)
527 snprintf(lkeyword
, sizeof(lkeyword
), "%s.%s", ll
, keyword
);
528 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
532 if (!strcmp(ll
, "ja"))
535 * Due to a bug in the CUPS DDK 1.1.0 ppdmerge program, Japanese
536 * PPD files were incorrectly assigned "jp" as the locale name
537 * instead of "ja". Support both the old (incorrect) and new
538 * locale names for Japanese...
541 snprintf(lkeyword
, sizeof(lkeyword
), "jp.%s", keyword
);
542 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
544 else if (!strcmp(ll
, "no"))
547 * Norway has two languages, "Bokmal" (the primary one)
548 * and "Nynorsk" (new Norwegian); we map "no" to "nb" here as
549 * recommended by the locale folks...
552 snprintf(lkeyword
, sizeof(lkeyword
), "nb.%s", keyword
);
553 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
560 printf(" *%s %s/%s: \"%s\"\n", attr
->name
, attr
->spec
, attr
->text
,
561 attr
->value
? attr
->value
: "");
571 * End of "$Id: localize.c 6882 2007-08-29 21:05:10Z mike $".