]>
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.
17 Change History (most recent first):
22 * ----------------------------------------------------------------------
23 * Source for localization library
24 * Originally created by jsantamaria: 3 may 2004
25 * ----------------------------------------------------------------------
28 #include "DebugServices.h"
32 #include "loclibrary.h"
34 #include <sys/types.h>
44 #define swprintf _snwprintf
45 #define snprintf _snprintf
50 #define DEFAULT_LANG_CODE "en"
52 // gets the user language
53 static LANGID
_getUserLanguage( void ) {
55 return GetUserDefaultUILanguage();
60 // gets the ISO mapping
61 static int _getISOCode(LANGID wLangID
, char *isoLangCode
, int codeLen
) {
63 unsigned short langCode
;
65 for (i
= 0; i
< NUM_ISOCODES
; i
++) {
66 int startIndex
= i
* MODULO_ISOCODES
;
68 langCode
= (ISOCODES
[startIndex
] << 8);
69 langCode
= langCode
+ ( (unsigned short) (ISOCODES
[startIndex
+ 1]) );
71 if (langCode
== wLangID
) {
72 char *langStr
= (char *)&(ISOCODES
[startIndex
+2]);
73 strncpy(isoLangCode
, langStr
, codeLen
);
80 static char isoLangCode
[LANG_CODE_LEN
+ 1] = "";
81 static LANGID wLangID
= (LANGID
) -1;
83 static void _setLanguageIfNeeded(void) {
85 // get the language code if we don't have it cached
86 if (!strncmp(isoLangCode
,"",LANG_CODE_LEN
+ 1)) {
88 // if we haven't cached the language id, do the lookup
89 if (wLangID
== (LANGID
) -1) {
90 wLangID
= _getUserLanguage();
93 // if no ISOCode, set it to DEFAULT_LANG_CODE
94 if (_getISOCode(wLangID
, isoLangCode
, LANG_CODE_LEN
+ 1)) {
95 strncpy(isoLangCode
, DEFAULT_LANG_CODE
, LANG_CODE_LEN
+1);
103 // Gets the PathForResource for handle 0 for the current process
106 static char appPathNameA
[MAX_PATH
] = "";
108 int PathForResourceA ( HMODULE
module, const char *name
, char *locFile
, int locFileLen
)
112 if ( !strcmp( appPathNameA
, "" ) )
114 char folder
[MAX_PATH
];
118 GetModuleFileNameA( module, folder
, MAX_PATH
);
122 app
= strrchr( folder
, '\\' );
123 require_action( app
, exit
, ret
= 0 );
126 // Strip the extension
128 if ( ( ( ext
= strstr( app
, ".exe" ) ) != NULL
) || ( ( ext
= strstr( app
, ".dll" ) ) != NULL
) )
133 snprintf( appPathNameA
, MAX_PATH
, "%s\\%s", folder
, app
);
136 ret
= PathForResourceWithPathA (appPathNameA
, name
, locFile
, locFileLen
);
143 static wchar_t appPathNameW
[MAX_PATH
] = L
"";
145 int PathForResourceW ( HMODULE
module, const wchar_t *name
, wchar_t *locFile
, int locFileLen
)
149 if ( !wcscmp( appPathNameW
, L
"" ) )
151 wchar_t folder
[MAX_PATH
];
155 GetModuleFileNameW( module, folder
, MAX_PATH
);
159 app
= wcsrchr( folder
, '\\' );
160 require_action( app
, exit
, ret
= 0 );
163 // Strip the extension
165 if ( ( ( ext
= wcsstr( app
, L
".exe" ) ) != NULL
) || ( ( ext
= wcsstr( app
, L
".dll" ) ) != NULL
) )
170 swprintf( appPathNameW
, MAX_PATH
, L
"%ls\\%ls", folder
, app
);
173 ret
= PathForResourceWithPathW (appPathNameW
, name
, locFile
, locFileLen
);
181 //// PathForResourceWithPath
183 #define TMP_BUF_SIZE MAX_PATH
185 int PathForResourceWithPathA (const char *path
, const char *nm
,
186 char *locFile
, int locFileLen
) {
187 char tmpBuffer
[TMP_BUF_SIZE
];
189 // build the path to the executable in the generic
190 // resources folder, check there first
191 snprintf(tmpBuffer
, MAX_PATH
, "%s.Resources\\%s", path
, nm
);
193 if (!PathFileExistsA(tmpBuffer
)) {
195 // didn't hit generic resource folder, so need to get language codes
196 _setLanguageIfNeeded();
198 // test to see if localized directory exists,
199 // if so, we don't fall back if we don't find the file.
200 snprintf(tmpBuffer
, TMP_BUF_SIZE
,
201 "%s.Resources\\%s.lproj", path
, isoLangCode
);
203 if (PathFileExistsA(tmpBuffer
)) {
204 snprintf(tmpBuffer
, TMP_BUF_SIZE
, "%s\\%s", tmpBuffer
, nm
);
206 if (!PathFileExistsA(tmpBuffer
)) return 0;
208 strncpy(locFile
, tmpBuffer
, locFileLen
);
209 return (int) strlen(locFile
);
212 // fall back on DEFAULT_LANG_CODE if still no good
213 snprintf(tmpBuffer
, TMP_BUF_SIZE
, "%s.Resources\\%s.lproj\\%s",
214 path
, DEFAULT_LANG_CODE
, nm
);
216 // we can't find the resource, so return 0
217 if (!PathFileExistsA(tmpBuffer
)) return 0;
220 strncpy(locFile
, tmpBuffer
, locFileLen
);
221 return (int) strlen(locFile
);
226 int PathForResourceWithPathW (const wchar_t *path
, const wchar_t *nm
,
227 wchar_t *locFile
, int locFileLen
) {
229 wchar_t tmpBuffer
[TMP_BUF_SIZE
];
231 // build the path to the executable in the generic
232 // resources folder, check there first
233 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls.Resources\\%ls", path
, nm
);
235 if (!PathFileExistsW(tmpBuffer
)) {
236 // didn't hit generic resource folder, so need to get language codes
237 _setLanguageIfNeeded();
239 // test to see if localized directory exists,
240 // if so, we don't fall back if we don't find the file.
241 swprintf(tmpBuffer
, TMP_BUF_SIZE
,
242 L
"%ls.Resources\\%S.lproj", path
, isoLangCode
);
244 if (PathFileExistsW(tmpBuffer
)) {
245 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls\\%ls", tmpBuffer
, nm
);
247 if (!PathFileExistsW(tmpBuffer
)) return 0;
249 wcsncpy(locFile
, tmpBuffer
, locFileLen
);
250 return (int) wcslen(locFile
);
253 // fall back on DEFAULT_LANG_CODE if still no good
254 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls.Resources\\%S.lproj\\%ls",
255 path
, DEFAULT_LANG_CODE
, nm
);
257 // we can't find the resource, so return 0
258 if (!PathFileExistsW(tmpBuffer
)) return 0;
261 wcsncpy(locFile
, tmpBuffer
, locFileLen
);
262 return (int) wcslen(locFile
);