]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/language.c
2 * "$Id: language.c,v 1.10 1999/07/12 16:09:37 mike Exp $"
4 * I18N/language support for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-1999 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsLangEncoding() - Return the character encoding (us-ascii, etc.)
27 * for the given language.
28 * cupsLangFlush() - Flush all language data out of the cache.
29 * cupsLangFree() - Free language data.
30 * cupsLangGet() - Get a language.
34 * Include necessary headers...
48 static cups_lang_t
*lang_cache
= NULL
; /* Language string cache */
49 static char *lang_blank
= ""; /* Blank constant string */
50 static char *lang_encodings
[] = /* Encoding strings */
65 static char *lang_default
[] = /* Default POSIX locale */
73 * 'cupsLangEncoding()' - Return the character encoding (us-ascii, etc.)
74 * for the given language.
77 char * /* O - Character encoding */
78 cupsLangEncoding(cups_lang_t
*lang
) /* I - Language data */
81 return (lang_encodings
[0]);
83 return (lang_encodings
[lang
->encoding
]);
88 * 'cupsLangFlush()' - Flush all language data out of the cache.
94 int i
; /* Looping var */
95 cups_lang_t
*lang
, /* Current language */
96 *next
; /* Next language */
99 for (lang
= lang_cache
; lang
!= NULL
; lang
= next
)
101 for (i
= 0; i
< CUPS_MSG_MAX
; i
++)
102 if (lang
->messages
[i
] != NULL
&& lang
->messages
[i
] != lang_blank
)
103 free(lang
->messages
[i
]);
112 * 'cupsLangFree()' - Free language data.
114 * This does not actually free anything; use cupsLangFlush() for that.
118 cupsLangFree(cups_lang_t
*lang
) /* I - Language to free */
120 if (lang
!= NULL
&& lang
->used
> 0)
126 * 'cupsLangGet()' - Get a language.
129 cups_lang_t
* /* O - Language data */
130 cupsLangGet(const char *language
) /* I - Language or locale */
132 int i
, count
; /* Looping vars */
133 char langname
[16], /* Requested language name */
134 real
[16], /* Real language name */
135 filename
[1024], /* Filename for language locale file */
136 *localedir
; /* Directory for locale files */
137 FILE *fp
; /* Language locale file pointer */
138 char line
[1024]; /* Line from file */
139 cups_msg_t msg
; /* Message number */
140 char *text
; /* Message text */
141 cups_lang_t
*lang
; /* Current language... */
145 * Convert the language string passed in to a locale string. "C" is the
146 * standard POSIX locale and is copied unchanged. Otherwise the
147 * language string is converted from ll-cc (language-country) to ll_CC
148 * to match the file naming convention used by all POSIX-compliant
152 if (language
== NULL
|| language
[0] == '\0')
153 strcpy(langname
, "C");
155 strcpy(langname
, language
);
157 if (strlen(langname
) < 2)
161 real
[0] = tolower(langname
[0]);
162 real
[1] = tolower(langname
[1]);
164 if (langname
[2] == '_' || langname
[2] == '-')
167 real
[3] = toupper(langname
[3]);
168 real
[4] = toupper(langname
[4]);
180 * Next try to open a locale file; we will try the country-localized file
181 * first, and then look for generic language file. If all else fails we
182 * will use the POSIX locale.
185 if ((localedir
= getenv("LOCALEDIR")) == NULL
)
186 localedir
= CUPS_LOCALEDIR
;
188 sprintf(filename
, "%s/%s/cups_%s", localedir
, real
, real
);
190 if ((fp
= fopen(filename
, "r")) == NULL
)
191 if (strlen(real
) > 2)
194 * Nope, see if we can open a generic language file...
198 sprintf(filename
, "%s/%s/cups_%s", localedir
, real
, real
);
199 fp
= fopen(filename
, "r");
203 * Then see if we already have this language loaded...
206 for (lang
= lang_cache
; lang
!= NULL
; lang
= lang
->next
)
207 if (strcmp(lang
->language
, langname
) == 0)
218 * OK, we have an open messages file; the first line will contain the
219 * language encoding (us-ascii, iso-8859-1, etc.), and the rest will
220 * be messages consisting of:
222 * #### SP message text
228 * If the line starts with a number, then message processing picks up
229 * where the number indicates. Otherwise the last message number is
232 * All leading whitespace is deleted.
236 strcpy(line
, lang_default
[0]);
237 else if (fgets(line
, sizeof(line
), fp
) == NULL
)
240 * Can't read encoding!
247 i
= strlen(line
) - 1;
249 line
[i
] = '\0'; /* Strip LF */
252 * See if there is a free language available; if so, use that
256 for (lang
= lang_cache
; lang
!= NULL
; lang
= lang
->next
)
263 * Allocate memory for the language and add it to the cache.
266 if ((lang
= calloc(sizeof(cups_lang_t
), 1)) == NULL
)
272 lang
->next
= lang_cache
;
278 * Free all old strings as needed...
281 for (i
= 0; i
< CUPS_MSG_MAX
; i
++)
283 if (lang
->messages
[i
] != NULL
&& lang
->messages
[i
] != lang_blank
)
284 free(lang
->messages
[i
]);
286 lang
->messages
[i
] = lang_blank
;
290 * Then assign the language and encoding fields...
294 strcpy(lang
->language
, langname
);
296 for (i
= 0; i
< (sizeof(lang_encodings
) / sizeof(lang_encodings
[0])); i
++)
297 if (strcmp(lang_encodings
[i
], line
) == 0)
299 lang
->encoding
= (cups_encoding_t
)i
;
304 * Read the strings from the file...
307 msg
= (cups_msg_t
)-1;
313 * Read a line from memory or from a file...
318 if (lang_default
[count
] == NULL
)
321 strcpy(line
, lang_default
[count
]);
323 else if (fgets(line
, sizeof(line
), fp
) == NULL
)
329 * Ignore blank lines...
332 i
= strlen(line
) - 1;
334 line
[i
] = '\0'; /* Strip LF */
340 * Grab the message number and text...
343 if (isdigit(line
[0]))
344 msg
= (cups_msg_t
)atoi(line
);
348 if (msg
< 0 || msg
>= CUPS_MSG_MAX
)
352 while (isdigit(*text
))
354 while (isspace(*text
))
357 lang
->messages
[msg
] = strdup(text
);
361 * Close the file and return...
372 * End of "$Id: language.c,v 1.10 1999/07/12 16:09:37 mike Exp $".