]>
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
+= ( (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
];
123 GetModuleFileNameA( module, folder
, MAX_PATH
);
127 app
= strrchr( folder
, '\\' );
128 require_action( app
, exit
, ret
= 0 );
132 snprintf( appPathNameA
, MAX_PATH
, "%s\\Resources\\%s", folder
, app
);
135 ret
= PathForResourceWithPathA (appPathNameA
, name
, locFile
, locFileLen
);
142 static wchar_t appPathNameW
[MAX_PATH
] = L
"";
144 int PathForResourceW ( HMODULE
module, const wchar_t *name
, wchar_t *locFile
, int locFileLen
)
148 if ( !wcscmp( appPathNameW
, L
"" ) )
150 wchar_t folder
[MAX_PATH
];
153 GetModuleFileNameW( module, folder
, MAX_PATH
);
157 app
= wcsrchr( folder
, '\\' );
158 require_action( app
, exit
, ret
= 0 );
162 swprintf( appPathNameW
, MAX_PATH
, L
"%ls\\Resources\\%ls", folder
, app
);
165 ret
= PathForResourceWithPathW (appPathNameW
, name
, locFile
, locFileLen
);
173 //// PathForResourceWithPath
175 #define TMP_BUF_SIZE MAX_PATH
177 int PathForResourceWithPathA (const char *path
, const char *nm
,
178 char *locFile
, int locFileLen
) {
179 char tmpBuffer
[TMP_BUF_SIZE
];
181 // build the path to the executable in the generic
182 // resources folder, check there first
183 snprintf(tmpBuffer
, MAX_PATH
, "%s.Resources\\%s", path
, nm
);
185 if (!PathFileExistsA(tmpBuffer
)) {
187 // didn't hit generic resource folder, so need to get language codes
188 _setLanguageIfNeeded();
190 // test to see if localized directory exists,
191 // if so, we don't fall back if we don't find the file.
192 snprintf(tmpBuffer
, TMP_BUF_SIZE
,
193 "%s.Resources\\%s.lproj", path
, isoLangCode
);
195 if (PathFileExistsA(tmpBuffer
)) {
196 snprintf(tmpBuffer
, TMP_BUF_SIZE
, "%s\\%s", tmpBuffer
, nm
);
198 if (!PathFileExistsA(tmpBuffer
)) return 0;
200 strncpy(locFile
, tmpBuffer
, locFileLen
);
201 return (int) strlen(locFile
);
204 // fall back on DEFAULT_LANG_CODE if still no good
205 snprintf(tmpBuffer
, TMP_BUF_SIZE
, "%s.Resources\\%s.lproj\\%s",
206 path
, DEFAULT_LANG_CODE
, nm
);
208 // we can't find the resource, so return 0
209 if (!PathFileExistsA(tmpBuffer
)) return 0;
212 strncpy(locFile
, tmpBuffer
, locFileLen
);
213 return (int) strlen(locFile
);
218 int PathForResourceWithPathW (const wchar_t *path
, const wchar_t *nm
,
219 wchar_t *locFile
, int locFileLen
) {
221 wchar_t tmpBuffer
[TMP_BUF_SIZE
];
223 // build the path to the executable in the generic
224 // resources folder, check there first
225 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls.Resources\\%ls", path
, nm
);
227 if (!PathFileExistsW(tmpBuffer
)) {
228 // didn't hit generic resource folder, so need to get language codes
229 _setLanguageIfNeeded();
231 // test to see if localized directory exists,
232 // if so, we don't fall back if we don't find the file.
233 swprintf(tmpBuffer
, TMP_BUF_SIZE
,
234 L
"%ls.Resources\\%S.lproj", path
, isoLangCode
);
236 if (PathFileExistsW(tmpBuffer
)) {
237 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls\\%ls", tmpBuffer
, nm
);
239 if (!PathFileExistsW(tmpBuffer
)) return 0;
241 wcsncpy(locFile
, tmpBuffer
, locFileLen
);
242 return (int) wcslen(locFile
);
245 // fall back on DEFAULT_LANG_CODE if still no good
246 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls.Resources\\%S.lproj\\%ls",
247 path
, DEFAULT_LANG_CODE
, nm
);
249 // we can't find the resource, so return 0
250 if (!PathFileExistsW(tmpBuffer
)) return 0;
253 wcsncpy(locFile
, tmpBuffer
, locFileLen
);
254 return (int) wcslen(locFile
);