【问题标题】:How to get localised day names in Delphi?如何在 Delphi 中获取本地化的日期名称?
【发布时间】:2010-10-01 13:59:08
【问题描述】:

我正在使用标准的 Delphi 常量 DayMonday 等,我想将它们转换为本地化字符串(例如“Lundi”)。是否有一个简单的 RTL 或 VCL 调用?

【问题讨论】:

  • 顺便提个很好的问题,我必须查找详细信息,但这是可能的 ;-)。

标签: delphi dayofweek


【解决方案1】:

您可以通过以下方式获得不同的区域设置:

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

GetThreadLocale 提供当前的 LCID,但您可以自己使用其他数字。

TFormatSettings 记录:

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;

有关完整列表,另请参阅 http://www.microsoft.com/globaldev/reference/lcid-all.mspx。

您甚至可以自己更改格式设置以创建真正精美的结果。

【讨论】:

    【解决方案2】:

    我以为我找到了一种简单的“当前语言环境”方法。

    system.pas 中定义了全局数组 LongDayNames[] 和 ShortDayNames[]

    所以..

      Label.Text = LongDayName[DayMonday];
    

    例如应该可以工作。 除了返回“星期日”。这是因为 Delphi 内部支持两天编号方案,DayMonday 是 ISO8601 常量 1,而 LongDayName 数组预计星期日是一周的第一天。 C++Builder 进一步混淆了事情,因为字符串数组从零开始,而不是一。

    【讨论】:

    • 是的,它们是使用 GetFormatSettings 初始化的;-)。通往目标的道路总是不同的,但在这种情况下,它们有相同的来源。
    【解决方案3】:

    你可以这样做:

    var d1:string;

    // 法语:

     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;
    

    【讨论】:

      猜你喜欢
      • 2016-10-28
      • 2015-11-10
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      相关资源
      最近更新 更多