]>
git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/loclibrary.c
2 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 Change History (most recent first):
28 * ----------------------------------------------------------------------
29 * Source for localization library
30 * Originally created by jsantamaria: 3 may 2004
31 * ----------------------------------------------------------------------
34 #include "DebugServices.h"
38 #include "loclibrary.h"
40 #include <sys/types.h>
50 #define swprintf _snwprintf
51 #define snprintf _snprintf
56 #define DEFAULT_LANG_CODE "en"
58 // gets the user language
59 static LANGID
_getUserLanguage( void ) {
61 return GetUserDefaultUILanguage();
66 // gets the ISO mapping
67 static int _getISOCode(LANGID wLangID
, char *isoLangCode
, int codeLen
) {
69 unsigned short langCode
;
71 for (i
= 0; i
< NUM_ISOCODES
; i
++) {
72 int startIndex
= i
* MODULO_ISOCODES
;
74 langCode
= (ISOCODES
[startIndex
] << 8);
75 langCode
= langCode
+ ( (unsigned short) (ISOCODES
[startIndex
+ 1]) );
77 if (langCode
== wLangID
) {
78 char *langStr
= (char *)&(ISOCODES
[startIndex
+2]);
79 strncpy(isoLangCode
, langStr
, codeLen
);
86 static char isoLangCode
[LANG_CODE_LEN
+ 1] = "";
87 static LANGID wLangID
= (LANGID
) -1;
89 static void _setLanguageIfNeeded(void) {
91 // get the language code if we don't have it cached
92 if (!strncmp(isoLangCode
,"",LANG_CODE_LEN
+ 1)) {
94 // if we haven't cached the language id, do the lookup
95 if (wLangID
== (LANGID
) -1) {
96 wLangID
= _getUserLanguage();
99 // if no ISOCode, set it to DEFAULT_LANG_CODE
100 if (_getISOCode(wLangID
, isoLangCode
, LANG_CODE_LEN
+ 1)) {
101 strncpy(isoLangCode
, DEFAULT_LANG_CODE
, LANG_CODE_LEN
+1);
109 // Gets the PathForResource for handle 0 for the current process
112 static char appPathNameA
[MAX_PATH
] = "";
114 int PathForResourceA ( HMODULE
module, const char *name
, char *locFile
, int locFileLen
)
118 if ( !strcmp( appPathNameA
, "" ) )
120 char folder
[MAX_PATH
];
124 GetModuleFileNameA( module, folder
, MAX_PATH
);
128 app
= strrchr( folder
, '\\' );
129 require_action( app
, exit
, ret
= 0 );
132 // Strip the extension
134 if ( ( ( ext
= strstr( app
, ".exe" ) ) != NULL
) || ( ( ext
= strstr( app
, ".dll" ) ) != NULL
) )
139 snprintf( appPathNameA
, MAX_PATH
, "%s\\%s", folder
, app
);
142 ret
= PathForResourceWithPathA (appPathNameA
, name
, locFile
, locFileLen
);
149 static wchar_t appPathNameW
[MAX_PATH
] = L
"";
151 int PathForResourceW ( HMODULE
module, const wchar_t *name
, wchar_t *locFile
, int locFileLen
)
155 if ( !wcscmp( appPathNameW
, L
"" ) )
157 wchar_t folder
[MAX_PATH
];
161 GetModuleFileNameW( module, folder
, MAX_PATH
);
165 app
= wcsrchr( folder
, '\\' );
166 require_action( app
, exit
, ret
= 0 );
169 // Strip the extension
171 if ( ( ( ext
= wcsstr( app
, L
".exe" ) ) != NULL
) || ( ( ext
= wcsstr( app
, L
".dll" ) ) != NULL
) )
176 swprintf( appPathNameW
, MAX_PATH
, L
"%ls\\%ls", folder
, app
);
179 ret
= PathForResourceWithPathW (appPathNameW
, name
, locFile
, locFileLen
);
187 //// PathForResourceWithPath
189 #define TMP_BUF_SIZE MAX_PATH
191 int PathForResourceWithPathA (const char *path
, const char *nm
,
192 char *locFile
, int locFileLen
) {
193 char tmpBuffer
[TMP_BUF_SIZE
];
195 // build the path to the executable in the generic
196 // resources folder, check there first
197 snprintf(tmpBuffer
, MAX_PATH
, "%s.Resources\\%s", path
, nm
);
199 if (!PathFileExistsA(tmpBuffer
)) {
201 // didn't hit generic resource folder, so need to get language codes
202 _setLanguageIfNeeded();
204 // test to see if localized directory exists,
205 // if so, we don't fall back if we don't find the file.
206 snprintf(tmpBuffer
, TMP_BUF_SIZE
,
207 "%s.Resources\\%s.lproj", path
, isoLangCode
);
209 if (PathFileExistsA(tmpBuffer
)) {
210 snprintf(tmpBuffer
, TMP_BUF_SIZE
, "%s\\%s", tmpBuffer
, nm
);
212 if (!PathFileExistsA(tmpBuffer
)) return 0;
214 strncpy(locFile
, tmpBuffer
, locFileLen
);
215 return (int) strlen(locFile
);
218 // fall back on DEFAULT_LANG_CODE if still no good
219 snprintf(tmpBuffer
, TMP_BUF_SIZE
, "%s.Resources\\%s.lproj\\%s",
220 path
, DEFAULT_LANG_CODE
, nm
);
222 // we can't find the resource, so return 0
223 if (!PathFileExistsA(tmpBuffer
)) return 0;
226 strncpy(locFile
, tmpBuffer
, locFileLen
);
227 return (int) strlen(locFile
);
232 int PathForResourceWithPathW (const wchar_t *path
, const wchar_t *nm
,
233 wchar_t *locFile
, int locFileLen
) {
235 wchar_t tmpBuffer
[TMP_BUF_SIZE
];
237 // build the path to the executable in the generic
238 // resources folder, check there first
239 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls.Resources\\%ls", path
, nm
);
241 if (!PathFileExistsW(tmpBuffer
)) {
242 // didn't hit generic resource folder, so need to get language codes
243 _setLanguageIfNeeded();
245 // test to see if localized directory exists,
246 // if so, we don't fall back if we don't find the file.
247 swprintf(tmpBuffer
, TMP_BUF_SIZE
,
248 L
"%ls.Resources\\%S.lproj", path
, isoLangCode
);
250 if (PathFileExistsW(tmpBuffer
)) {
251 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls\\%ls", tmpBuffer
, nm
);
253 if (!PathFileExistsW(tmpBuffer
)) return 0;
255 wcsncpy(locFile
, tmpBuffer
, locFileLen
);
256 return (int) wcslen(locFile
);
259 // fall back on DEFAULT_LANG_CODE if still no good
260 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls.Resources\\%S.lproj\\%ls",
261 path
, DEFAULT_LANG_CODE
, nm
);
263 // we can't find the resource, so return 0
264 if (!PathFileExistsW(tmpBuffer
)) return 0;
267 wcsncpy(locFile
, tmpBuffer
, locFileLen
);
268 return (int) wcslen(locFile
);