Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Locale-independent

Q1: "atof" is locale-dependent function, in "atof_l" locale could be managed but it needs "xlocale.h" file. What platform do i need in order to have "xlocale.h"?

Q2: In MSDN there is "_atof_l" function, but there are only 2 results in google about it. In MSDN it is said that it needs "stdlib.h" and "math.h", but there is no declaration of that function in these files. Does any one knows what it takes to compile the code with "_atof_l" within it?

Q3: What is the general method to create locale-independent software? Writing setlocale("English") in the main scope? Use of pragmas?

Q3.1: In our project we have lots of DLL-s which are unaffected by the "main" setlocale function call. Does any know a better way to change the loaded DLL-s locale, without changing every DLL code?

Please assist. Thanks.
[841 byte] By [mkholod] at [2007-11-11 8:16:32]
# 1 Re: Locale-independent
OK, it's a bit tricky indeed: all the locale-dependent functions you mentioned aren't standard. They are Microsoft proprietary extensions to the Standard Library. Recentky Microsoft submitted a proposal to include these functions in ANSI C and C++ but that doesn't mean they are already standardized. So it all boils down to which OS and compiler you're using. If your standard headers (math.h, stdlib.h etc.) don't have these functions, then don't sweat it -- it's not just a matter of getting the header file bt also the binary lib files.
C++ defines the <locale> library for locale-sensitive apps and wchar_t functions and classes. C has its own <locale.h> header.
Danny at 2007-11-11 21:01:35 >
# 2 Re: Locale-independent
1. I guess that if i intend to write it in a cross-platformed way (for Linux, MAC and Windows) i can't use the microsoft _atof_l.
So what do you suggest to do if i have loads of atof/atol etc. in the code and i wish to make them locale-independent?

2. What is the general method to make your software locale-independent?

Thanks
mkholod at 2007-11-11 21:02:30 >
# 3 Re: Locale-independent
To ensure localizability, stick to wchar_t and the standard wchar_t functions. Almost every ANSI function that takes char * has a wchar_t equivalent: wstrstr, wstrcpy etc. These functions *are* standard so you can expect them to be supported on most platforms. What's not in the Standard yet is the _l functions so you should avoid them.
Other than that, localization is a vast topic. To do it right, you need to plan in advance what yoru app is supposed to do and how much localization you want to offer: is it just the error messages being displayed in different languages? Or do you also intend to use different temperatures (Fahrenheit vs. Celsius), different decimal points (. or , ), date formats etc. The standard C++ <locale> library supports all of these but using it isn't exactly a trivial task.
Danny at 2007-11-11 21:03:34 >
# 4 Re: Locale-independent
So is there an easy way to force the locale of the loaded DLL to be as the locale of the application that loads it? Suppose i'm changing the locale of the loading application.
mkholod at 2007-11-11 21:04:31 >