]>
git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/loclibrary.c
78f16a48eb7a568f8f91e03993f063974e08cf71
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 * ----------------------------------------------------------------------
37 #include "loclibrary.h"
39 #include <sys/types.h>
48 #define swprintf _snwprintf
49 #define snprintf _snprintf
54 #define DEFAULT_LANG_CODE "en"
56 // gets the user language
57 static LANGID
_getUserLanguage( void ) {
59 return GetUserDefaultUILanguage();
64 // gets the ISO mapping
65 static int _getISOCode(LANGID wLangID
, char *isoLangCode
, int codeLen
) {
67 unsigned short langCode
;
69 for (i
= 0; i
< NUM_ISOCODES
; i
++) {
70 int startIndex
= i
* MODULO_ISOCODES
;
72 langCode
= (ISOCODES
[startIndex
] << 8);
73 langCode
+= ( (unsigned short) (ISOCODES
[startIndex
+ 1]) );
75 if (langCode
== wLangID
) {
76 char *langStr
= (char *)&(ISOCODES
[startIndex
+2]);
77 strncpy(isoLangCode
, langStr
, codeLen
);
84 static char isoLangCode
[LANG_CODE_LEN
+ 1] = "";
85 static LANGID wLangID
= (LANGID
) -1;
87 static void _setLanguageIfNeeded(void) {
89 // get the language code if we don't have it cached
90 if (!strncmp(isoLangCode
,"",LANG_CODE_LEN
+ 1)) {
92 // if we haven't cached the language id, do the lookup
93 if (wLangID
== (LANGID
) -1) {
94 wLangID
= _getUserLanguage();
97 // if no ISOCode, set it to DEFAULT_LANG_CODE
98 if (_getISOCode(wLangID
, isoLangCode
, LANG_CODE_LEN
+ 1)) {
99 strncpy(isoLangCode
, DEFAULT_LANG_CODE
, LANG_CODE_LEN
+1);
107 // Gets the PathForResource for handle 0 for the current process
110 static char appPathNameA
[MAX_PATH
] = "";
112 int PathForResourceA ( HMODULE
module, const char *name
, char *locFile
, int locFileLen
) {
113 if (!strcmp(appPathNameA
,"")) {
114 GetModuleFileNameA(module, appPathNameA
, MAX_PATH
);
117 return PathForResourceWithPathA (appPathNameA
, name
, locFile
, locFileLen
);
121 static wchar_t appPathNameW
[MAX_PATH
] = L
"";
123 int PathForResourceW ( HMODULE
module, const wchar_t *name
, wchar_t *locFile
, int locFileLen
) {
124 if (!wcscmp(appPathNameW
,L
"")) {
125 GetModuleFileNameW( module, appPathNameW
, MAX_PATH
);
128 OutputDebugString( appPathNameW
);
130 return PathForResourceWithPathW (appPathNameW
, name
, locFile
, locFileLen
);
134 //// PathForResourceWithPath
136 #define TMP_BUF_SIZE MAX_PATH
138 int PathForResourceWithPathA (const char *path
, const char *nm
,
139 char *locFile
, int locFileLen
) {
140 char tmpBuffer
[TMP_BUF_SIZE
];
142 // build the path to the executable in the generic
143 // resources folder, check there first
144 snprintf(tmpBuffer
, MAX_PATH
, "%s.Resources\\%s", path
, nm
);
146 if (!PathFileExistsA(tmpBuffer
)) {
148 // didn't hit generic resource folder, so need to get language codes
149 _setLanguageIfNeeded();
151 // test to see if localized directory exists,
152 // if so, we don't fall back if we don't find the file.
153 snprintf(tmpBuffer
, TMP_BUF_SIZE
,
154 "%s.Resources\\%s.lproj", path
, isoLangCode
);
156 if (PathFileExistsA(tmpBuffer
)) {
157 snprintf(tmpBuffer
, TMP_BUF_SIZE
, "%s\\%s", tmpBuffer
, nm
);
159 if (!PathFileExistsA(tmpBuffer
)) return 0;
161 strncpy(locFile
, tmpBuffer
, locFileLen
);
162 return (int) strlen(locFile
);
165 // fall back on DEFAULT_LANG_CODE if still no good
166 snprintf(tmpBuffer
, TMP_BUF_SIZE
, "%s.Resources\\%s.lproj\\%s",
167 path
, DEFAULT_LANG_CODE
, nm
);
169 // we can't find the resource, so return 0
170 if (!PathFileExistsA(tmpBuffer
)) return 0;
173 strncpy(locFile
, tmpBuffer
, locFileLen
);
174 return (int) strlen(locFile
);
179 int PathForResourceWithPathW (const wchar_t *path
, const wchar_t *nm
,
180 wchar_t *locFile
, int locFileLen
) {
182 wchar_t tmpBuffer
[TMP_BUF_SIZE
];
184 // build the path to the executable in the generic
185 // resources folder, check there first
186 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls.Resources\\%ls", path
, nm
);
188 if (!PathFileExistsW(tmpBuffer
)) {
189 // didn't hit generic resource folder, so need to get language codes
190 _setLanguageIfNeeded();
192 // test to see if localized directory exists,
193 // if so, we don't fall back if we don't find the file.
194 swprintf(tmpBuffer
, TMP_BUF_SIZE
,
195 L
"%ls.Resources\\%S.lproj", path
, isoLangCode
);
197 if (PathFileExistsW(tmpBuffer
)) {
198 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls\\%ls", tmpBuffer
, nm
);
200 if (!PathFileExistsW(tmpBuffer
)) return 0;
202 wcsncpy(locFile
, tmpBuffer
, locFileLen
);
203 return (int) wcslen(locFile
);
206 // fall back on DEFAULT_LANG_CODE if still no good
207 swprintf(tmpBuffer
, TMP_BUF_SIZE
, L
"%ls.Resources\\%s.lproj\\%ls",
208 path
, DEFAULT_LANG_CODE
, nm
);
210 // we can't find the resource, so return 0
211 if (!PathFileExistsW(tmpBuffer
)) return 0;
214 wcsncpy(locFile
, tmpBuffer
, locFileLen
);
215 return (int) wcslen(locFile
);