summaryrefslogtreecommitdiffstats
path: root/lib/libsi18n/getlang.c
blob: 0ccf2225ca19b93ce524e2518da7f28759552c13 (plain)
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
/** BEGIN COPYRIGHT BLOCK
 * 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 "libadmin/libadmin.h"


#include "i18n.h"

/*********************************************************************
  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.
*********************************************************************/

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[128] = "en";
static char admin_language[128] = "en";
static char default_language[128] = "en";

void
SetLanguage(int type, char *language)
{
	switch(type) {
	case CLIENT_LANGUAGE:
		if (language)
			strcpy(client_language, language);
		break;
	case ADMIN_LANGUAGE:
		if (language)
			strcpy(admin_language, language);
		break;
	case DEFAULT_LANGUAGE:
		if (language)
			strcpy(default_language, language);
		break;
	}
	return ;
}



char*
GetClientLanguage(void)
{
  if (client_language)
	return client_language;
  else
	return emptyString;
}
 
char*
GetAdminLanguage(void)
{
  if (admin_language)
	return admin_language;
  else
	return emptyString;
}

char*
GetDefaultLanguage(void)
{
  if (default_language)
	return default_language;
  else
	return "en";
}

/*********************************************************************
  GetFileForLanguage looks for a file in the appropriate language.
 *********************************************************************/

NSAPI_PUBLIC
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;
}