1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/** BEGIN COPYRIGHT BLOCK
* This Program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; version 2 of the License.
*
* This Program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA.
*
*
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
* END COPYRIGHT BLOCK **/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "libadminutil/resource.h"
#include "dsgwi18n.h"
#include "unicode/utypes.h"
#include "unicode/udat.h"
#include "unicode/ucal.h"
#include "unicode/unum.h"
#include "unicode/ures.h"
static char *database_name;
static Resource *i18nResource;
/*********************************************************************
strReplace replaces the first instance of from in target with to.
from can be "": to is inserted at start of target.
to can be "": from is removed from target.
if from is not found, 0 is returned; else 1 is returned.
*********************************************************************/
static int
strReplace(char* target,char* from,char* to)
{
/* replace /from/to/ in target */
char* pFrom;
char* pOldTail;
int lenTo;
pFrom = strstr(target,from);
if (pFrom) {
pOldTail = pFrom+strlen(from);
lenTo = strlen(to);
memmove(pFrom+lenTo,pOldTail,strlen(pOldTail)+1);
memcpy(pFrom,to,lenTo);
return 1;
}
return 0;
}
/*********************************************************************
statFileDir is a wrapper to stat() that strips trailing slashes
because stat() on Windows seems to return -1 otherwise.
*********************************************************************/
static int
statFileDir(const char *path, struct stat *info) {
int ret, pathlen;
char *newpath = strdup(path);
if(newpath == NULL)
return -1;
for (pathlen = (strlen(newpath) - 1); pathlen >= 0; pathlen--) {
if (newpath[pathlen] == '/' || newpath[pathlen] == '\\') {
newpath[pathlen] = '\0';
} else {
break;
}
}
ret = stat(newpath, info);
if (newpath)
free(newpath);
return ret;
}
/*********************************************************************
GetLanguage is reserved for future use. These APIs are not belong
to this file. It needs to be moved to somewhere which knows what's
the current language setting.
*********************************************************************/
static char *emptyString = "";
static char *client_language;
static char *admin_language;
static char *default_language;
PR_IMPLEMENT( void )
SetLanguage(int type, char *language)
{
switch(type) {
case CLIENT_LANGUAGE:
if (language)
client_language = PL_strdup(language);
break;
case ADMIN_LANGUAGE:
if (language)
admin_language = PL_strdup(language);
break;
case DEFAULT_LANGUAGE:
if (language)
default_language = PL_strdup(language);
break;
}
return ;
}
PR_IMPLEMENT( char* )
GetClientLanguage(void)
{
if (client_language)
return client_language;
else
return emptyString;
}
PR_IMPLEMENT( char* )
GetAdminLanguage(void)
{
if (admin_language)
return admin_language;
else
return emptyString;
}
PR_IMPLEMENT( char* )
GetDefaultLanguage(void)
{
if (default_language)
return default_language;
else
return "en";
}
/*********************************************************************
GetFileForLanguage looks for a file in the appropriate language.
*********************************************************************/
PR_IMPLEMENT( int )
GetFileForLanguage(char* filePath,char* language,char* existingFilePath)
{
/* Input: filePath,language
* filePath is of the form "/xxx/xxx/$$LANGDIR/xxx/xxx/filename"
* or of the form "/xxx/xxx/xxx/xxx/filename".
* filename may or may not have an extension.
* language is an Accept-Language list; each language-range will be
* tried as a subdirectory name and possibly as a filename modifier.
* "*" is ignored - default always provided if needed.
* "-" is replaced by "_".
* $$LANGDIR is a special string replaced by language. It is optional.
* For the default case, $$LANGDIR/ is replaced by nothing
* (so // is not created).
*
* Returned: existingPath
* existingFilePath is the path of a satisfactory, existing file.
* if no file is found, an empty string "" is returned.
*
* int returned: -1 if no file found (existingFilePath = "")
* 0 if default file is returned
* 1 if language file is returned (any in list)
*
* Example:
* filePath = "/path/$$LANGDIR/filename.ext"
* language = "language"
* GetDefaultLanguage() --> "default"
* LANG_DELIMIT = "_"
*
* 1. Try: "/path/language/filename.ext"
* 2. Try: "/path/filename_language.ext"
* 3. Try: "/path/default/filename.ext"
* 4. Try: "/path/filename_default.ext"
* 5. Try: "/path/filename.ext"
* else: ""
*
* Example:
* language = "en-us;q=0.6,ja;q=0.8,en-ca"
*
* 1. Try: "/path/en-ca/filename.ext"
* 2. Try: "/path/filename_en_ca.ext"
* 3. Try: "/path/ja/filename.ext"
* 4. Try: "/path/filename_ja.ext"
* 5. Try: "/path/en_us/filename.ext"
* 6. Try: "/path/filename_en_us.ext"
* 7. Try: "/path/default/filename.ext"
* 8. Try: "/path/filename_default.ext"
* 9. Try: "/path/filename.ext"
* else: ""
*
*/
#define LANG_DELIMIT '_'
int pattern;
char* pDot;
char* pSlash;
/* PRFileInfo info; */
struct stat info;
char lang_modifier[MAX_ACCEPT_LENGTH+1];
ACCEPT_LANGUAGE_LIST acceptLanguageList;
int numLang;
int iLang;
int iCase;
/* escape in case XP_InitStringDatabase has not been called */
if (filePath==NULL) {
*existingFilePath = '\0';
return -1;
}
pattern = (strstr(filePath,"$$LANGDIR/")!=NULL);
for ( iCase=1 ; iCase>=0 ; iCase-- ) {
if (iCase==1) { /* iCase=1 tries requested language */
numLang = XP_AccLangList(language,acceptLanguageList);
} else { /* iCase=0 tries default language */
numLang = XP_AccLangList(GetDefaultLanguage(),acceptLanguageList);
}
for ( iLang=0 ; iLang<numLang ; iLang++ ) {
/* Try: /path/language/filename.ext */
if (pattern) {
strcpy(existingFilePath,filePath);
strReplace(existingFilePath,"$$LANGDIR",acceptLanguageList[iLang]);
if (statFileDir(existingFilePath,&info)==0) {
return iCase;
}
/*
if (PR_GetFileInfo(existingFilePath,&info)==PR_SUCCESS) {
return iCase;
}
*/
}
/* Try: /path/filename_language.ext */
{
strcpy(existingFilePath,filePath);
strReplace(existingFilePath,"$$LANGDIR/",emptyString);
pDot = strrchr(existingFilePath,'.');
pSlash = strrchr(existingFilePath,'/');
if (pSlash>=pDot) {
pDot = strchr(existingFilePath,'\0');
}
sprintf(lang_modifier,"%c%s",LANG_DELIMIT,acceptLanguageList[iLang]);
strReplace(pDot,emptyString,lang_modifier);
if (statFileDir(existingFilePath,&info)==0) {
return iCase;
}
/*
if (PR_GetFileInfo(existingFilePath,&info)==PR_SUCCESS) {
return iCase;
}
*/
}
}
}
/* Try: /path/filename.ext */
{
strcpy(existingFilePath,filePath);
strReplace(existingFilePath,"$$LANGDIR/",emptyString);
if (statFileDir(existingFilePath,&info)==0) {
return 0;
}
/*
if (PR_GetFileInfo(existingFilePath,&info)==PR_SUCCESS) {
return 0;
}
*/
}
/* Else: */
*existingFilePath = '\0';
return -1;
}
/*
Note: This function returns allocated memory. Most of the callers in the
dsgw do not free this memory - they prefer to use exit() for free() - which
is usually fine for short lived CGI programs - so if you use valgrind you
will see a lot of memory leakage around this function
*/
PR_IMPLEMENT( char * )
XP_GetClientStr(int key)
{
int rc = 0;
char keybuf[256];
char *lang = GetClientLanguage();
char *resstring = NULL;
PR_snprintf(keybuf, sizeof(keybuf), "%s%d", database_name, key);
resstring = res_getstring(i18nResource, keybuf, lang,
NULL, 0, &rc);
if (rc) {
fprintf(stderr, "The message keyword id [%d] was not found\n", key);
}
return resstring;
}
PR_IMPLEMENT( void )
XP_InitStringDatabase(const char *path, const char *dbname)
{
database_name = strdup(dbname);
i18nResource = res_init_resource(path, NULL);
/* set default languages for string database */
SetLanguage(CLIENT_LANGUAGE, "");
SetLanguage(ADMIN_LANGUAGE, "");
SetLanguage(DEFAULT_LANGUAGE, "");
}
/*
This function will return the appropriate locale to use
for ICU functions based on the HTTP_ACCEPT_LANGUAGE
*/
char *
dsgw_get_locale_from_accept_language()
{
UErrorCode err = U_ZERO_ERROR;
UEnumeration *available = ures_openAvailableLocales(NULL, &err);
UAcceptResult outResult;
char *returnlocale = NULL;
int32_t needlen = 0;
if (U_FAILURE(err)) {
fprintf(stderr, "Error: ures_openAvailableLocales(): %d:%s\n", err, u_errorName(err));
return NULL;
}
needlen = 20;
returnlocale = (char *)malloc(sizeof(char) * needlen);
needlen = uloc_acceptLanguageFromHTTP(returnlocale, needlen, &outResult, GetClientLanguage(),
available, &err);
if(err == U_BUFFER_OVERFLOW_ERROR) {
err = U_ZERO_ERROR;
returnlocale = (char *)realloc(returnlocale, sizeof(char) * (needlen + 1));
needlen = uloc_acceptLanguageFromHTTP(returnlocale, needlen, &outResult, GetClientLanguage(),
available, &err);
}
if (U_FAILURE(err)) {
free(returnlocale);
returnlocale = NULL;
fprintf(stderr, "Error: uloc_acceptLanguageFromHTTP(%s): %d:%s\n", GetClientLanguage(), err, u_errorName(err));
return NULL;
}
return returnlocale;
}
|