]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/localize.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / localize.c
1 /*
2 * "$Id: localize.c 6602 2007-06-22 22:13:13Z mike $"
3 *
4 * PPD custom option routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * PostScript is a trademark of Adobe Systems, Inc.
25 *
26 * This code and any derivative of it may be used and distributed
27 * freely under the terms of the GNU General Public License when
28 * used with GNU Ghostscript or its derivatives. Use of the code
29 * (or any derivative of it) with software other than GNU
30 * GhostScript (or its derivatives) is governed by the CUPS license
31 * agreement.
32 *
33 * This file is subject to the Apple OS-Developed Software exception.
34 *
35 * Contents:
36 *
37 * ppdLocalize() - Localize the PPD file to the current locale.
38 */
39
40 /*
41 * Include necessary headers.
42 */
43
44 #include "globals.h"
45 #include "debug.h"
46
47
48 /*
49 * Local functions...
50 */
51
52 static const char *ppd_text(ppd_file_t *ppd, const char *keyword,
53 const char *spec, const char *ll_CC,
54 const char *ll);
55
56
57 /*
58 * 'ppdLocalize()' - Localize the PPD file to the current locale.
59 *
60 * @since CUPS 1.2@
61 */
62
63 int /* O - 0 on success, -1 on error */
64 ppdLocalize(ppd_file_t *ppd) /* I - PPD file */
65 {
66 int i, j, k; /* Looping vars */
67 ppd_group_t *group; /* Current group */
68 ppd_option_t *option; /* Current option */
69 ppd_choice_t *choice; /* Current choice */
70 ppd_coption_t *coption; /* Current custom option */
71 ppd_cparam_t *cparam; /* Current custom parameter */
72 ppd_attr_t *attr; /* Current attribute */
73 cups_lang_t *lang; /* Current language */
74 char ckeyword[PPD_MAX_NAME], /* Custom keyword */
75 ll_CC[6], /* Language + country locale */
76 ll[3]; /* Language locale */
77 const char *text; /* Localized text */
78
79
80 /*
81 * Range check input...
82 */
83
84 DEBUG_printf(("ppdLocalize(ppd=%p)\n", ppd));
85
86 if (!ppd)
87 return (-1);
88
89 /*
90 * Get the default language...
91 */
92
93 if ((lang = cupsLangDefault()) == NULL)
94 return (-1);
95
96 strlcpy(ll_CC, lang->language, sizeof(ll_CC));
97 strlcpy(ll, lang->language, sizeof(ll));
98
99 if (strlen(ll_CC) == 2)
100 {
101 /*
102 * Map "ll" to primary/origin country locales to have the best
103 * chance of finding a match...
104 */
105
106 if (!strcmp(ll_CC, "cs"))
107 strcpy(ll_CC, "cs_CZ");
108 else if (!strcmp(ll_CC, "en"))
109 strcpy(ll_CC, "en_US");
110 else if (!strcmp(ll_CC, "ja"))
111 strcpy(ll_CC, "ja_JP");
112 else if (!strcmp(ll_CC, "sv"))
113 strcpy(ll_CC, "sv_SE");
114 else if (!strcmp(ll_CC, "zh"))
115 strcpy(ll_CC, "zh_CN"); /* Simplified Chinese */
116 else
117 {
118 ll_CC[2] = '_';
119 ll_CC[3] = toupper(ll_CC[0] & 255);
120 ll_CC[4] = toupper(ll_CC[1] & 255);
121 ll_CC[5] = '\0';
122 }
123 }
124
125 DEBUG_printf((" lang->language=\"%s\", ll=\"%s\", ll_CC=\"%s\"...\n",
126 lang->language, ll, ll_CC));
127
128 /*
129 * Now lookup all of the groups, options, choices, etc.
130 */
131
132 for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++)
133 {
134 if ((text = ppd_text(ppd, "Translation", group->name, ll_CC, ll)) != NULL)
135 strlcpy(group->text, text, sizeof(group->text));
136
137 for (j = group->num_options, option = group->options; j > 0; j --, option ++)
138 {
139 if ((text = ppd_text(ppd, "Translation", option->keyword, ll_CC,
140 ll)) != NULL)
141 strlcpy(option->text, text, sizeof(option->text));
142
143 for (k = option->num_choices, choice = option->choices;
144 k > 0;
145 k --, choice ++)
146 {
147 if (strcmp(choice->choice, "Custom"))
148 text = ppd_text(ppd, option->keyword, choice->choice, ll_CC, ll);
149 else
150 {
151 snprintf(ckeyword, sizeof(ckeyword), "Custom%s", option->keyword);
152
153 text = ppd_text(ppd, ckeyword, "True", ll_CC, ll);
154 }
155
156 if (text)
157 strlcpy(choice->text, text, sizeof(choice->text));
158 }
159 }
160 }
161
162 /*
163 * Translate any custom parameters...
164 */
165
166 for (coption = (ppd_coption_t *)cupsArrayFirst(ppd->coptions);
167 coption;
168 coption = (ppd_coption_t *)cupsArrayNext(ppd->coptions))
169 {
170 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
171 cparam;
172 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
173 {
174 snprintf(ckeyword, sizeof(ckeyword), "ParamCustom%s", coption->keyword);
175
176 if ((text = ppd_text(ppd, ckeyword, cparam->name, ll_CC, ll)) != NULL)
177 strlcpy(cparam->text, text, sizeof(cparam->text));
178 }
179 }
180
181 /*
182 * Translate ICC profile names...
183 */
184
185 if ((attr = ppdFindAttr(ppd, "APCustomColorMatchingName", NULL)) != NULL)
186 {
187 if ((text = ppd_text(ppd, "APCustomColorMatchingName", attr->spec,
188 ll_CC, ll)) != NULL)
189 strlcpy(attr->text, text, sizeof(attr->text));
190 }
191
192 for (attr = ppdFindAttr(ppd, "cupsICCProfile", NULL);
193 attr;
194 attr = ppdFindNextAttr(ppd, "cupsICCProfile", NULL))
195 {
196 cupsArraySave(ppd->sorted_attrs);
197
198 if ((text = ppd_text(ppd, "cupsICCProfile", attr->spec, ll_CC, ll)) != NULL)
199 strlcpy(attr->text, text, sizeof(attr->text));
200
201 cupsArrayRestore(ppd->sorted_attrs);
202 }
203
204 /*
205 * Translate printer presets...
206 */
207
208 for (attr = ppdFindAttr(ppd, "APPrinterPreset", NULL);
209 attr;
210 attr = ppdFindNextAttr(ppd, "APPrinterPreset", NULL))
211 {
212 cupsArraySave(ppd->sorted_attrs);
213
214 if ((text = ppd_text(ppd, "APPrinterPreset", attr->spec, ll_CC, ll)) != NULL)
215 strlcpy(attr->text, text, sizeof(attr->text));
216
217 cupsArrayRestore(ppd->sorted_attrs);
218 }
219
220 return (0);
221 }
222
223
224 /*
225 * 'ppd_text()' - Find the localized text as needed...
226 */
227
228 static const char * /* O - Localized text or NULL */
229 ppd_text(ppd_file_t *ppd, /* I - PPD file */
230 const char *keyword, /* I - Main keyword */
231 const char *spec, /* I - Option keyword */
232 const char *ll_CC, /* I - Language + country locale */
233 const char *ll) /* I - Language locale */
234 {
235 char lkeyword[PPD_MAX_NAME]; /* Localization keyword */
236 ppd_attr_t *attr; /* Current attribute */
237
238
239 DEBUG_printf(("ppd_text(ppd=%p, keyword=\"%s\", spec=\"%s\", "
240 "ll_CC=\"%s\", ll=\"%s\")\n",
241 ppd, keyword, spec, ll_CC, ll));
242
243 /*
244 * Look for Keyword.ll_CC, then Keyword.ll...
245 */
246
247 snprintf(lkeyword, sizeof(lkeyword), "%s.%s", ll_CC, keyword);
248 if ((attr = ppdFindAttr(ppd, lkeyword, spec)) == NULL)
249 {
250 snprintf(lkeyword, sizeof(lkeyword), "%s.%s", ll, keyword);
251 attr = ppdFindAttr(ppd, lkeyword, spec);
252
253 if (!attr && !strcmp(ll, "ja"))
254 {
255 /*
256 * Due to a bug in the CUPS DDK 1.1.0 ppdmerge program, Japanese
257 * PPD files were incorrectly assigned "jp" as the locale name
258 * instead of "ja". Support both the old (incorrect) and new
259 * locale names for Japanese...
260 */
261
262 snprintf(lkeyword, sizeof(lkeyword), "jp.%s", keyword);
263 attr = ppdFindAttr(ppd, lkeyword, spec);
264 }
265 }
266
267 #ifdef DEBUG
268 if (attr)
269 printf(" *%s %s/%s: \"%s\"\n", attr->name, attr->spec, attr->text,
270 attr->value ? attr->value : "");
271 else
272 puts(" NOT FOUND");
273 #endif /* DEBUG */
274
275 /*
276 * Return text if we find it...
277 */
278
279 return (attr ? attr->text : NULL);
280 }
281
282
283 /*
284 * End of "$Id: localize.c 6602 2007-06-22 22:13:13Z mike $".
285 */