]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/localize.c
2 * "$Id: localize.c 6883 2007-08-29 21:05:17Z mike $"
4 * PPD custom option routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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 * ppd_ll_CC() - Get the current locale names.
32 * ppd_localized_attr() - Find a localized attribute.
36 * Include necessary headers.
47 static void ppd_ll_CC(char *ll_CC
, int ll_CC_size
,
48 char *ll
, int ll_size
);
49 static ppd_attr_t
*ppd_localized_attr(ppd_file_t
*ppd
,
51 const char *spec
, const char *ll_CC
,
56 * 'ppdLocalize()' - Localize the PPD file to the current locale.
58 * All groups, options, and choices are localized, as are ICC profile
59 * descriptions, printer presets, and custom option parameters. Each
60 * localized string uses the UTF-8 character encoding.
65 int /* O - 0 on success, -1 on error */
66 ppdLocalize(ppd_file_t
*ppd
) /* I - PPD file */
68 int i
, j
, k
; /* Looping vars */
69 ppd_group_t
*group
; /* Current group */
70 ppd_option_t
*option
; /* Current option */
71 ppd_choice_t
*choice
; /* Current choice */
72 ppd_coption_t
*coption
; /* Current custom option */
73 ppd_cparam_t
*cparam
; /* Current custom parameter */
74 ppd_attr_t
*attr
, /* Current attribute */
75 *locattr
; /* Localized attribute */
76 char ckeyword
[PPD_MAX_NAME
], /* Custom keyword */
77 ll_CC
[6], /* Language + country locale */
78 ll
[3]; /* Language locale */
82 * Range check input...
85 DEBUG_printf(("ppdLocalize(ppd=%p)\n", ppd
));
91 * Get the default language...
94 ppd_ll_CC(ll_CC
, sizeof(ll_CC
), ll
, sizeof(ll
));
97 * Now lookup all of the groups, options, choices, etc.
100 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
102 if ((locattr
= ppd_localized_attr(ppd
, "Translation", group
->name
,
104 strlcpy(group
->text
, locattr
->text
, sizeof(group
->text
));
106 for (j
= group
->num_options
, option
= group
->options
; j
> 0; j
--, option
++)
108 if ((locattr
= ppd_localized_attr(ppd
, "Translation", option
->keyword
,
110 strlcpy(option
->text
, locattr
->text
, sizeof(option
->text
));
112 for (k
= option
->num_choices
, choice
= option
->choices
;
116 if (strcmp(choice
->choice
, "Custom"))
117 locattr
= ppd_localized_attr(ppd
, option
->keyword
, choice
->choice
,
121 snprintf(ckeyword
, sizeof(ckeyword
), "Custom%s", option
->keyword
);
123 locattr
= ppd_localized_attr(ppd
, ckeyword
, "True", ll_CC
, ll
);
127 strlcpy(choice
->text
, locattr
->text
, sizeof(choice
->text
));
133 * Translate any custom parameters...
136 for (coption
= (ppd_coption_t
*)cupsArrayFirst(ppd
->coptions
);
138 coption
= (ppd_coption_t
*)cupsArrayNext(ppd
->coptions
))
140 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
142 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
144 snprintf(ckeyword
, sizeof(ckeyword
), "ParamCustom%s", coption
->keyword
);
146 if ((locattr
= ppd_localized_attr(ppd
, ckeyword
, cparam
->name
,
148 strlcpy(cparam
->text
, locattr
->text
, sizeof(cparam
->text
));
153 * Translate ICC profile names...
156 if ((attr
= ppdFindAttr(ppd
, "APCustomColorMatchingName", NULL
)) != NULL
)
158 if ((locattr
= ppd_localized_attr(ppd
, "APCustomColorMatchingName",
159 attr
->spec
, ll_CC
, ll
)) != NULL
)
160 strlcpy(attr
->text
, locattr
->text
, sizeof(attr
->text
));
163 for (attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
165 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
167 cupsArraySave(ppd
->sorted_attrs
);
169 if ((locattr
= ppd_localized_attr(ppd
, "cupsICCProfile", attr
->spec
,
171 strlcpy(attr
->text
, locattr
->text
, sizeof(attr
->text
));
173 cupsArrayRestore(ppd
->sorted_attrs
);
177 * Translate printer presets...
180 for (attr
= ppdFindAttr(ppd
, "APPrinterPreset", NULL
);
182 attr
= ppdFindNextAttr(ppd
, "APPrinterPreset", NULL
))
184 cupsArraySave(ppd
->sorted_attrs
);
186 if ((locattr
= ppd_localized_attr(ppd
, "APPrinterPreset", attr
->spec
,
188 strlcpy(attr
->text
, locattr
->text
, sizeof(attr
->text
));
190 cupsArrayRestore(ppd
->sorted_attrs
);
198 * 'ppdLocalizeIPPReason()' - Get the localized version of a cupsIPPReason
201 * This function uses the current locale to find the corresponding reason
202 * text or URI from the attribute value. If "scheme" is NULL or "text",
203 * the returned value contains human-readable (UTF-8) text from the translation
204 * string or attribute value. Otherwise the corresponding URI is returned.
206 * If no value of the requested scheme can be found, NULL is returned.
211 const char * /* O - Value or NULL if not found */
212 ppdLocalizeIPPReason(
213 ppd_file_t
*ppd
, /* I - PPD file */
214 const char *reason
, /* I - IPP reason keyword to look up */
215 const char *scheme
, /* I - URI scheme or NULL for text */
216 char *buffer
, /* I - Value buffer */
217 size_t bufsize
) /* I - Size of value buffer */
219 ppd_attr_t
*locattr
; /* Localized attribute */
220 char ll_CC
[6], /* Language + country locale */
221 ll
[3], /* Language 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
), ll
, sizeof(ll
));
247 * Find the localized attribute...
250 if ((locattr
= ppd_localized_attr(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 * 'ppd_ll_CC()' - Get the current locale names.
394 ppd_ll_CC(char *ll_CC
, /* O - Country-specific locale name */
395 int ll_CC_size
, /* I - Size of country-specific name */
396 char *ll
, /* O - Generic locale name */
397 int ll_size
) /* I - Size of generic name */
399 cups_lang_t
*lang
; /* Current language */
403 * Get the current locale...
406 if ((lang
= cupsLangDefault()) == NULL
)
408 strlcpy(ll_CC
, "en_US", ll_CC_size
);
409 strlcpy(ll
, "en", ll_size
);
414 * Copy the locale name...
417 strlcpy(ll_CC
, lang
->language
, ll_CC_size
);
418 strlcpy(ll
, lang
->language
, ll_size
);
420 DEBUG_printf(("ll_CC=\"%s\", ll=\"%s\"\n", ll_CC
, ll
));
422 if (strlen(ll_CC
) == 2)
425 * Map "ll" to primary/origin country locales to have the best
426 * chance of finding a match...
429 if (!strcmp(ll_CC
, "cs"))
430 strlcpy(ll_CC
, "cs_CZ", ll_CC_size
);
431 else if (!strcmp(ll_CC
, "en"))
432 strlcpy(ll_CC
, "en_US", ll_CC_size
);
433 else if (!strcmp(ll_CC
, "ja"))
434 strlcpy(ll_CC
, "ja_JP", ll_CC_size
);
435 else if (!strcmp(ll_CC
, "sv"))
436 strlcpy(ll_CC
, "sv_SE", ll_CC_size
);
437 else if (!strcmp(ll_CC
, "zh")) /* Simplified Chinese */
438 strlcpy(ll_CC
, "zh_CN", ll_CC_size
);
439 else if (ll_CC_size
>= 6)
442 ll_CC
[3] = toupper(ll_CC
[0] & 255);
443 ll_CC
[4] = toupper(ll_CC
[1] & 255);
448 DEBUG_printf(("ppd_ll_CC: lang->language=\"%s\", ll=\"%s\", ll_CC=\"%s\"...\n",
449 lang
->language
, ll
, ll_CC
));
454 * 'ppd_localized_attr()' - Find a localized attribute.
457 static ppd_attr_t
* /* O - Localized attribute or NULL */
458 ppd_localized_attr(ppd_file_t
*ppd
, /* I - PPD file */
459 const char *keyword
, /* I - Main keyword */
460 const char *spec
, /* I - Option keyword */
461 const char *ll_CC
, /* I - Language + country locale */
462 const char *ll
) /* I - Language locale */
464 char lkeyword
[PPD_MAX_NAME
]; /* Localization keyword */
465 ppd_attr_t
*attr
; /* Current attribute */
468 DEBUG_printf(("ppd_text(ppd=%p, keyword=\"%s\", spec=\"%s\", "
469 "ll_CC=\"%s\", ll=\"%s\")\n",
470 ppd
, keyword
, spec
, ll_CC
, ll
));
473 * Look for Keyword.ll_CC, then Keyword.ll...
476 snprintf(lkeyword
, sizeof(lkeyword
), "%s.%s", ll_CC
, keyword
);
477 if ((attr
= ppdFindAttr(ppd
, lkeyword
, spec
)) == NULL
)
479 snprintf(lkeyword
, sizeof(lkeyword
), "%s.%s", ll
, keyword
);
480 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
484 if (!strcmp(ll
, "ja"))
487 * Due to a bug in the CUPS DDK 1.1.0 ppdmerge program, Japanese
488 * PPD files were incorrectly assigned "jp" as the locale name
489 * instead of "ja". Support both the old (incorrect) and new
490 * locale names for Japanese...
493 snprintf(lkeyword
, sizeof(lkeyword
), "jp.%s", keyword
);
494 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
496 else if (!strcmp(ll
, "no"))
499 * Norway has two languages, "Bokmal" (the primary one)
500 * and "Nynorsk" (new Norwegian); we map "no" to "nb" here as
501 * recommended by the locale folks...
504 snprintf(lkeyword
, sizeof(lkeyword
), "nb.%s", keyword
);
505 attr
= ppdFindAttr(ppd
, lkeyword
, spec
);
512 printf(" *%s %s/%s: \"%s\"\n", attr
->name
, attr
->spec
, attr
->text
,
513 attr
->value
? attr
->value
: "");
523 * End of "$Id: localize.c 6883 2007-08-29 21:05:17Z mike $".