How to get localised day names in Delphi?
Asked Answered
P

3

8

I'm using standard Delphi constants DayMonday, etc and I want to convert them to localized strings (eg "Lundi"). Is there a simple RTL or VCL call for this?

Pedicular answered 15/1, 2009 at 12:7 Comment(1)
Nice question by the way, I had to lookup the details but it is possible ;-).Brusque
B
7

You can get different locale settings by:

var
  fs : TFormatSettings;
  x  : string;
begin
  GetLocaleFormatSettings(GetThreadlocale, fs);
  x:= FormatDateTime('%mmmm', Now, fs);
  // etc..
end;

GetThreadLocale gives the current LCID but you can use another number yourself.

TFormatSettings record:

TFormatSettings = record
  CurrencyFormat: Byte;
  NegCurrFormat: Byte;
  ThousandSeparator: Char;
  DecimalSeparator: Char;
  CurrencyDecimals: Byte;
  DateSeparator: Char;
  TimeSeparator: Char;
  ListSeparator: Char;
  CurrencyString: string;
  ShortDateFormat: string;
  LongDateFormat: string;
  TimeAMString: string;
  TimePMString: string;
  ShortTimeFormat: string;
  LongTimeFormat: string;
  ShortMonthNames: array[1..12] of string;
  LongMonthNames: array[1..12] of string;
  ShortDayNames: array[1..7] of string;
  LongDayNames: array[1..7] of string;
  TwoDigitYearCenturyWindow: Word;
end;

See also http://www.microsoft.com/globaldev/reference/lcid-all.mspx for a complete list.

You can even change the formatsettings yourself to create really fancy results.

Brusque answered 15/1, 2009 at 12:25 Comment(0)
P
3

I thought I had found a simple way for the "current locale".

There are global arrays LongDayNames[] and ShortDayNames[] defined in system.pas

So..

  Label.Text = LongDayName[DayMonday];

should work, for example. Except it returns "Sunday". This is because Delphi internally supports two day numbering schemes, and DayMonday is an the ISO8601 constant 1, while the LongDayName array expects sunday as the first day of the week. C++Builder confuses things further because the string array then starts at zero, not one.

Pedicular answered 15/1, 2009 at 12:31 Comment(1)
Yes, they are initialized using GetFormatSettings ;-). There are always different roads to the goal, but in this case they have the same source.Brusque
G
1

You can do somthing like :

var d1:string;

// in french :

 case dayofweek(cxScheduler1.SelStart) of
  1:d1:='Dimanche';
  2:d1:='Lundi';
  3:d1:='Mardi';
  4:d1:='Mercredi';
  5:d1:='Jeudi';
  6:d1:='vendredi';
  7:d1:='Samedi';
 end;
Guarantee answered 26/12, 2020 at 19:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.