Board index » delphi » SetThreadLocale

SetThreadLocale


2003-12-06 02:30:18 AM
delphi188
Hi all,
I want to set the local parametres like local desimal separator, or thousand
separator for my application. But the changes must effect only for my
application.
For that problem the function SetThreadLocale must be used but I couldn't do
it properly. If you help me how do to do it, I will be grateful.
Thanks for helps.
 
 

Re:SetThreadLocale

In article <XXXX@XXXXX.COM>, Makbolat writes:
Quote
I want to set the local parametres like local desimal separator, or thousand
separator for my application. But the changes must effect only for my
application.
For that problem the function SetThreadLocale must be used but I couldn't do
it properly. If you help me how do to do it, I will be grateful.
A simple way is to do it this way:
Application.UpdateFormatSettings := false;
DecimalSeparator := '.';
ThousandSeparator := ',';
ShortDateFormat := 'yyyy-mm-dd';
...
The Sysutils unit contains all the locale settings as read at app startup.
These variables are used by Format and the other data conversion routines.
However, there is still code that will go to the Windows settings (e.g. in
database drivers, Variant conversions).
Function MakeLCID( lgID: Word; srtid: Word ): DWORD;
Begin
Result := MakeLong( lgid, srtid );
End;
Function MakeLangID( p, s: Word ): Word;
Begin
Result := (s shl 10) or p
End;
procedure TForm1.Button1Click(Sender: TObject);
var
V: OleVariant;
S: string;
begin
Win32Check( SetThreadLocale(
MakeLCID(
MakeLangID( LANG_ENGLISH, SUBLANG_ENGLISH_US),
0 )));
GetFormatSettings;
Application.UpdateFormatSettings := false;
V := 3.456; // VType is varCurrency here!
S := VarAsType( V, varDouble );
label1.Caption := '3.456 ->'+S;
end;
This sets the threads locale and updates the Sysutils settings. Unfortunately
this will still not fix all the problems with Variant type conversions. For
some reason the Variants unit (D6 and D7) will call OS conversion routines
for some variant type (varCurrency, for example), and it uses
LOCALE_USER_DEFAULT instead of GetThreadLocale to specify the locale for the
conversion. In the test case above this results in S := V (without the
VarAsType conversion) to still display the number with a decimal comma (i use
a german locale).
Peter Below (TeamB)
Use the newsgroup archives :
www.mers.com/searchsite.html
www.tamaracka.com/search.htm
groups.google.com
www.prolix.be