#include "globals.h"
#include "debug.h"
static void ppd_ll_CC(char *ll_CC, int ll_CC_size,
char *ll, int ll_size);
static ppd_attr_t *ppd_localized_attr(ppd_file_t *ppd,
const char *keyword,
const char *spec, const char *ll_CC,
const char *ll);
int
ppdLocalize(ppd_file_t *ppd)
{
int i, j, k;
ppd_group_t *group;
ppd_option_t *option;
ppd_choice_t *choice;
ppd_coption_t *coption;
ppd_cparam_t *cparam;
ppd_attr_t *attr,
*locattr;
char ckeyword[PPD_MAX_NAME],
ll_CC[6],
ll[3];
DEBUG_printf(("ppdLocalize(ppd=%p)\n", ppd));
if (!ppd)
return (-1);
ppd_ll_CC(ll_CC, sizeof(ll_CC), ll, sizeof(ll));
for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++)
{
if ((locattr = ppd_localized_attr(ppd, "Translation", group->name,
ll_CC, ll)) != NULL)
strlcpy(group->text, locattr->text, sizeof(group->text));
for (j = group->num_options, option = group->options; j > 0; j --, option ++)
{
if ((locattr = ppd_localized_attr(ppd, "Translation", option->keyword,
ll_CC, ll)) != NULL)
strlcpy(option->text, locattr->text, sizeof(option->text));
for (k = option->num_choices, choice = option->choices;
k > 0;
k --, choice ++)
{
if (strcmp(choice->choice, "Custom"))
locattr = ppd_localized_attr(ppd, option->keyword, choice->choice,
ll_CC, ll);
else
{
snprintf(ckeyword, sizeof(ckeyword), "Custom%s", option->keyword);
locattr = ppd_localized_attr(ppd, ckeyword, "True", ll_CC, ll);
}
if (locattr)
strlcpy(choice->text, locattr->text, sizeof(choice->text));
}
}
}
for (coption = (ppd_coption_t *)cupsArrayFirst(ppd->coptions);
coption;
coption = (ppd_coption_t *)cupsArrayNext(ppd->coptions))
{
for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
cparam;
cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
{
snprintf(ckeyword, sizeof(ckeyword), "ParamCustom%s", coption->keyword);
if ((locattr = ppd_localized_attr(ppd, ckeyword, cparam->name,
ll_CC, ll)) != NULL)
strlcpy(cparam->text, locattr->text, sizeof(cparam->text));
}
}
if ((attr = ppdFindAttr(ppd, "APCustomColorMatchingName", NULL)) != NULL)
{
if ((locattr = ppd_localized_attr(ppd, "APCustomColorMatchingName",
attr->spec, ll_CC, ll)) != NULL)
strlcpy(attr->text, locattr->text, sizeof(attr->text));
}
for (attr = ppdFindAttr(ppd, "cupsICCProfile", NULL);
attr;
attr = ppdFindNextAttr(ppd, "cupsICCProfile", NULL))
{
cupsArraySave(ppd->sorted_attrs);
if ((locattr = ppd_localized_attr(ppd, "cupsICCProfile", attr->spec,
ll_CC, ll)) != NULL)
strlcpy(attr->text, locattr->text, sizeof(attr->text));
cupsArrayRestore(ppd->sorted_attrs);
}
for (attr = ppdFindAttr(ppd, "APPrinterPreset", NULL);
attr;
attr = ppdFindNextAttr(ppd, "APPrinterPreset", NULL))
{
cupsArraySave(ppd->sorted_attrs);
if ((locattr = ppd_localized_attr(ppd, "APPrinterPreset", attr->spec,
ll_CC, ll)) != NULL)
strlcpy(attr->text, locattr->text, sizeof(attr->text));
cupsArrayRestore(ppd->sorted_attrs);
}
return (0);
}
const char *
ppdLocalizeIPPReason(
ppd_file_t *ppd,
const char *reason,
const char *scheme,
char *buffer,
size_t bufsize)
{
ppd_attr_t *locattr;
char ll_CC[6],
ll[3],
*bufptr,
*bufend,
*valptr;
int ch,
schemelen;
if (buffer)
*buffer = '\0';
if (!ppd || !reason || (scheme && !*scheme) ||
!buffer || bufsize < PPD_MAX_TEXT)
return (NULL);
ppd_ll_CC(ll_CC, sizeof(ll_CC), ll, sizeof(ll));
if ((locattr = ppd_localized_attr(ppd, "cupsIPPReason", reason,
ll_CC, ll)) == NULL)
locattr = ppdFindAttr(ppd, "cupsIPPReason", reason);
if (!locattr)
return (NULL);
bufend = buffer + bufsize - 1;
if (!scheme || !strcmp(scheme, "text"))
{
strlcpy(buffer, locattr->text, bufsize);
for (valptr = locattr->value, bufptr = buffer; *valptr && bufptr < bufend;)
{
if (!strncmp(valptr, "text:", 5))
{
if (bufptr > buffer)
*bufptr++ = ' ';
valptr += 5;
while (*valptr && !isspace(*valptr & 255) && bufptr < bufend)
{
if (*valptr == '%' && isxdigit(valptr[1] & 255) &&
isxdigit(valptr[2] & 255))
{
valptr ++;
if (isdigit(*valptr & 255))
ch = (*valptr - '0') << 4;
else
ch = (tolower(*valptr) - 'a' + 10) << 4;
valptr ++;
if (isdigit(*valptr & 255))
*bufptr++ = ch | (*valptr - '0');
else
*bufptr++ = ch | (tolower(*valptr) - 'a' + 10);
valptr ++;
}
else if (*valptr == '+')
{
*bufptr++ = ' ';
valptr ++;
}
else
*bufptr++ = *valptr++;
}
}
else
{
while (*valptr && !isspace(*valptr & 255))
valptr++;
}
while (isspace(*valptr & 255))
valptr ++;
}
if (bufptr > buffer)
*bufptr = '\0';
return (buffer);
}
else
{
schemelen = strlen(scheme);
if (scheme[schemelen - 1] == ':')
schemelen --;
for (valptr = locattr->value, bufptr = buffer; *valptr && bufptr < bufend;)
{
if ((!strncmp(valptr, scheme, schemelen) && valptr[schemelen] == ':') ||
(*valptr == '/' && !strcmp(scheme, "file")))
{
while (*valptr && !isspace(*valptr & 255) && bufptr < bufend)
*bufptr++ = *valptr++;
*bufptr = '\0';
return (buffer);
}
else
{
while (*valptr && !isspace(*valptr & 255))
valptr++;
}
while (isspace(*valptr & 255))
valptr ++;
}
return (NULL);
}
}
static void
ppd_ll_CC(char *ll_CC,
int ll_CC_size,
char *ll,
int ll_size)
{
cups_lang_t *lang;
if ((lang = cupsLangDefault()) == NULL)
{
strlcpy(ll_CC, "en_US", ll_CC_size);
strlcpy(ll, "en", ll_size);
return;
}
strlcpy(ll_CC, lang->language, ll_CC_size);
strlcpy(ll, lang->language, ll_size);
DEBUG_printf(("ll_CC=\"%s\", ll=\"%s\"\n", ll_CC, ll));
if (strlen(ll_CC) == 2)
{
if (!strcmp(ll_CC, "cs"))
strlcpy(ll_CC, "cs_CZ", ll_CC_size);
else if (!strcmp(ll_CC, "en"))
strlcpy(ll_CC, "en_US", ll_CC_size);
else if (!strcmp(ll_CC, "ja"))
strlcpy(ll_CC, "ja_JP", ll_CC_size);
else if (!strcmp(ll_CC, "sv"))
strlcpy(ll_CC, "sv_SE", ll_CC_size);
else if (!strcmp(ll_CC, "zh"))
strlcpy(ll_CC, "zh_CN", ll_CC_size);
}
DEBUG_printf(("ppd_ll_CC: lang->language=\"%s\", ll=\"%s\", ll_CC=\"%s\"...\n",
lang->language, ll, ll_CC));
}
static ppd_attr_t *
ppd_localized_attr(ppd_file_t *ppd,
const char *keyword,
const char *spec,
const char *ll_CC,
const char *ll)
{
char lkeyword[PPD_MAX_NAME];
ppd_attr_t *attr;
DEBUG_printf(("ppd_text(ppd=%p, keyword=\"%s\", spec=\"%s\", "
"ll_CC=\"%s\", ll=\"%s\")\n",
ppd, keyword, spec, ll_CC, ll));
snprintf(lkeyword, sizeof(lkeyword), "%s.%s", ll_CC, keyword);
if ((attr = ppdFindAttr(ppd, lkeyword, spec)) == NULL)
{
snprintf(lkeyword, sizeof(lkeyword), "%s.%s", ll, keyword);
attr = ppdFindAttr(ppd, lkeyword, spec);
if (!attr)
{
if (!strcmp(ll, "ja"))
{
snprintf(lkeyword, sizeof(lkeyword), "jp.%s", keyword);
attr = ppdFindAttr(ppd, lkeyword, spec);
}
else if (!strcmp(ll, "no"))
{
snprintf(lkeyword, sizeof(lkeyword), "nb.%s", keyword);
attr = ppdFindAttr(ppd, lkeyword, spec);
}
}
}
#ifdef DEBUG
if (attr)
printf(" *%s %s/%s: \"%s\"\n", attr->name, attr->spec, attr->text,
attr->value ? attr->value : "");
else
puts(" NOT FOUND");
#endif
return (attr);
}