YoungMaster
YoungMaster

Reputation: 405

Getting locale format for Windows

how can I get correct locale's format for Windows in Delphi ?

I trying to do next

LCID := 2048;    
FormatSettings := TFormatSettings.Create(LCID);

but this doesn't work fine if set shortdate format as example '07-13\2012'. and variable will be equal

FormatSettings = 'MM/dd\yyyy' ?????

Upvotes: 1

Views: 10838

Answers (4)

Andrei Krasutski
Andrei Krasutski

Reputation: 5257

The correct format of the locale for countries

var
  formatSettings : TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, formatSettings);
  ShowMessage('LOCALE_SYSTEM_DEFAULT = ' + DateTimeToStr(now, formatSettings));

  GetLocaleFormatSettings(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), formatSettings);
  ShowMessage('LANG_ENGLISH = ' + DateTimeToStr(now, formatSettings));

  GetLocaleFormatSettings(MAKELANGID(LANG_RUSSIAN, SUBLANG_DEFAULT), formatSettings);
  ShowMessage('LANG_RUSSIAN = ' + DateTimeToStr(now, formatSettings));
end;

Upvotes: 1

Zam
Zam

Reputation: 2940

in fact you should consider date as:

TShortDateFormatParts = (sdfpPrefix, sdfpDatePart1, sdfpSplitter1, sdfpDatePart2, sdfpSplitter2, sdfpDatePart3, sdfpSuffix);

in your code you should:

  1. Find and get everything before leading "d", or "M" or "Y" (prefix).

  2. Find and get text before first splitter.

  3. Find and get end of first splitter.

  4. Find and get text before second splitter.

  5. Find and get end of second splitter.

  6. Find and get everything before final text (suffix).

  7. Get what we have now is final part

after:

Get position of DAY, MONTH and YEAR in current format string

Upvotes: 1

Lawrence Barsanti
Lawrence Barsanti

Reputation: 33232

The first lines of TFormatSettings.Create(Locale) are:

  if not IsValidLocale(Locale, LCID_INSTALLED) then
    Locale := GetThreadLocale;

When I pass LOCALE_SYSTEM_DEFAULT (2048) as my locale, IsValidLocale returns false and GetThreadLocale returns 4105 (Canadian-English). You may want to investigate this further. Are you getting the locale that you expecting?

Upvotes: 0

Gerald Versluis
Gerald Versluis

Reputation: 34013

You could use this?

var
   formatSettings : TFormatSettings;

 begin
  // Furnish the locale format settings record
   GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, formatSettings);

  // And use it in the thread safe form of CurrToStrF
   ShowMessage('1234.56 formats as = '+
               CurrToStrF(1234.56, ffCurrency, 4, formatSettings));
 end;

http://www.delphibasics.co.uk/RTL.asp?Name=GetLocaleFormatSettings

Upvotes: 1

Related Questions