]>
git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/loclibrary.c
1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * ----------------------------------------------------------------------
20 * Source for localization library
21 * Originally created by jsantamaria: 3 may 2004
22 * ----------------------------------------------------------------------
25 #include "DebugServices.h"
29 #include "loclibrary.h"
31 #include <sys/types.h>
41 #define swprintf _snwprintf
42 #define snprintf _snprintf
47 #define DEFAULT_LANG_CODE "en"
49 // gets the user language
50 static LANGID
_getUserLanguage( void ) {
52 return GetUserDefaultUILanguage();
57 // gets the ISO mapping
58 static int _getISOCode(LANGID wLangID
, char *isoLangCode
, int codeLen
) {
60 unsigned short langCode
;
62 for (i
= 0; i
< NUM_ISOCODES
; i
++) {
63 int startIndex
= i
* MODULO_ISOCODES
;
65 langCode
= (ISOCODES
[startIndex
] << 8);
66 langCode
= langCode
+ ( (unsigned short) (ISOCODES
[startIndex
+ 1]) );
68 if (langCode
== wLangID
) {
69 char *langStr
= (char *)&(ISOCODES
[startIndex
+2]);
70 strncpy(isoLangCode
, langStr
, codeLen
);
77 static char isoLangCode
[LANG_CODE_LEN
+ 1] = "";
78 static LANGID wLangID
= (LANGID
) -1;
80 static void _setLanguageIfNeeded(void) {
82 // get the language code if we don't have it cached
83 if (!strncmp(isoLangCode
,"",LANG_CODE_LEN
+ 1)) {
85 // if we haven't cached the language id, do the lookup
86 if (wLangID
== (LANGID
) -1) {
87 wLangID
= _getUserLanguage();
90 // if no ISOCode, set it to DEFAULT_LANG_CODE
91 if (_getISOCode(wLangID
, isoLangCode
, LANG_CODE_LEN
+ 1)) {
92 strncpy(isoLangCode
, DEFAULT_LANG_CODE
, LANG_CODE_LEN
+1);
100 // Gets the PathForResource for handle 0 for the current process
103 static char appPathNameA
[MAX_PATH
] = "";
105 int PathForResourceA ( HMODULE
module, const char *name
, char *locFile
, int locFileLen
)
109 if ( !strcmp( appPathNameA
, "" ) )
111 char folder
[MAX_PATH
];
115 GetModuleFileNameA( module, folder
, MAX_PATH
);
119 app
= strrchr( folder
, '\\' );
120 require_action( app
, exit
, ret
= 0 );
123 // Strip the extension
125 if ( ( ( ext
= strstr( app
, ".exe" ) ) != NULL
) || ( ( ext
= strstr( app
, ".dll" ) ) != NULL
) )
130 snprintf( appPathNameA
, MAX_PATH
, "%s\\%s", folder
, app
);
133 ret
= PathForResourceWithPathA (appPathNameA
, name
, locFile
, locFileLen
);
140 static wchar_t appPathNameW
[MAX_PATH
] = L
"";
142 int PathForResourceW ( HMODULE
module, const wchar_t *name
, wchar_t *locFile
, int locFileLen
)
146 if ( !wcscmp( appPathNameW
, L
"" ) )
148 wchar_t folder
[MAX_PATH
];
152 GetModuleFileNameW( module, folder
, MAX_PATH
);
156 app
= wcsrchr( folder
, '\\' );
157 require_action( app
, exit
, ret
= 0 );
160 // Strip the extension
162 if ( ( ( ext
= wcsstr( app
, L
".exe" ) ) != NULL
) || ( ( ext
= wcsstr( app
, L
".dll" ) ) != NULL
) )
167 swprintf( appPathNameW
, MAX_PATH
, L
"%ls\\%ls", folder
, app
);
170 ret
= PathForResourceWithPathW (appPathNameW
, name
, locFile
, locFileLen
);
178 //// PathForResourceWithPath
180 #define TMP_BUF_SIZE MAX_PATH
182 int PathForResourceWithPathA (const char *path
, const char *nm
,
183 char *locFile
, int locFileLen
) {
184 char tmpBuffer
[TMP_BUF_SIZE
];
186 // build the path to the executable in the generic
187 // resources folder, check there first
188 snprintf(tmpBuffer
, MAX_PATH
, "%s.Resources\\%s", path
, nm
);
190 if (!PathFileExistsA(tmpBuffer
)) {
192 // didn't hit generic resource folder, so need to get language codes
193 _setLanguageIfNeeded();
195 // test to see if localized directory exists,
196 // if so, we don't fall back if we don't find the file.
197 snprintf(tmpBuffer
, TMP_BUF_SIZE
,
198 "%s.Resources\\%s.lproj", path
, isoLangCode
);
200 if (PathFileExistsA(tmpBuffer
)) {
201 snprintf(tmpBuffer
, TMP_BUF_SIZE
, "%s\\%s", tmpBuffer
, nm
);
203 if (!PathFileExistsA(tmpBuffer
)) return 0;
205 strncpy(locFile
, tmpBuffer
, locFileLen
);
206 return (int) strlen(locFile
);
209 // fall back on DEFAULT_LANG_CODE if still no good
210 snprintf(tmpBuffer
, TMP_BUF_SIZE
, "%s.Resources\\%s.lproj\\%s",
211 path
, DEFAULT_LANG_CODE
, nm
);
213 // we can't find the resource, so return 0
214 if (!PathFileExistsA(tmpBuffer
)) return 0;
217 strncpy(locFile
, tmpBuffer
, locFileLen
);
218 return (int) strlen(locFile
);
223 int PathForResourceWithPathW (const wchar_t *path
, const wchar_t *nm
,
224 wchar_t *locFile
, int locFileLen
) {
226 wchar_t tmpBuffer
[TMP_BUF_SIZE
];
228 // build the path to the executable in the generic
229 // resources folder, check there first
230 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls.Resources\\%ls", path
, nm
);
232 if (!PathFileExistsW(tmpBuffer
)) {
233 // didn't hit generic resource folder, so need to get language codes
234 _setLanguageIfNeeded();
236 // test to see if localized directory exists,
237 // if so, we don't fall back if we don't find the file.
238 swprintf(tmpBuffer
, TMP_BUF_SIZE
,
239 L
"%ls.Resources\\%S.lproj", path
, isoLangCode
);
241 if (PathFileExistsW(tmpBuffer
)) {
242 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls\\%ls", tmpBuffer
, nm
);
244 if (!PathFileExistsW(tmpBuffer
)) return 0;
246 wcsncpy(locFile
, tmpBuffer
, locFileLen
);
247 return (int) wcslen(locFile
);
250 // fall back on DEFAULT_LANG_CODE if still no good
251 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls.Resources\\%S.lproj\\%ls",
252 path
, DEFAULT_LANG_CODE
, nm
);
254 // we can't find the resource, so return 0
255 if (!PathFileExistsW(tmpBuffer
)) return 0;
258 wcsncpy(locFile
, tmpBuffer
, locFileLen
);
259 return (int) wcslen(locFile
);