过载 TFormatSettings 和不兼容类型

3

我有这个过程:

procedure Initialize(out FormatSettings: TFormatSettings);
const
  LongDayNamesEx : array [1..7] of string = ('Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato', 'Domenica');
  LongMonthNamesEx : array [1..12] of string = ('Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre');
begin
  FormatSettings := TFormatSettings.Create;
  with FormatSettings do
  begin
    LongDayNames := LongDayNamesEx;
    LongMonthNames := LongMonthNamesEx;
  end;
end;

我遇到了不兼容类型的错误(E2008)。我应该如何解决这个问题?我不想使用类似以下代码的东西:

LongDayNames[1] := 'Lunedì';
LongDayNames[2] := 'Martedì';
...
LongDayNames[7] := 'Domenica';
LongMonthNames[1] := 'Gennaio';
LongMonthNames[2] := 'Febbraio';
...
LongMonthNames[12] := 'Dicembre';

如果不是必要的话,请勿使用。

谢谢帮助。
3个回答

5
您可以像这样操作:
type
  TDayNameArray = array[1..7] of string;
const
  LongDayNamesEx: TDayNameArray = ('Måndag', 'Tisdag', 'Onsdag', 'Torsdag',
    'Fredag', 'Lördag', 'Söndag');
var
  fs: TFormatSettings;
begin
  TDayNameArray(fs.LongDayNames) := LongDayNamesEx;

3

Andreas给出了你所提出的直接问题的好答案。

采用另一种方法,我认为你可以通过在初始化对象时传递当地语言环境来更轻松地解决问题。例如:

FormatSettings := TFormatSettings.Create('it-IT');

对于意大利语,请使用该系统填写特定的设置,例如日期名称、月份名称等。

或者也可以使用接受区域设置 ID 的重载函数。不管怎样,您肯定会理解。


谢谢,问题已解决。关于指定语言,没有帮助我太多;是的,它设定了语言,但我需要每种格式的字符串输出。例如,当我需要显示“Sabato”时,我会显示“sabato”。 - Marcello Impastato

0
直接回答你的问题,显而易见的解决方案是使用for循环。结合记录助手和开放数组参数使其更容易调用:
type
  TTFormatSettingsHelper = record helper for TFormatSettings
    procedure SetLongDayNames(const Values: array of string);
  end;

procedure TTFormatSettingsHelper.SetLongDayNames(const Values: array of string);
var
  Index: Integer;
  Value: string;
begin
  Assert(high(Values)-low(Values)
    = high(Self.LongDayNames)-low(Self.LongDayNames));

  Index := low(Self.LongDayNames);
  for Value in Values do
  begin
    Self.LongDayNames[Index] := Value;
    inc(Index);
  end;
end;

然后,要调用这个函数,你只需要写:

FormatSettings.SetLongDayNames(['Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 
  'Venerdì', 'Sabato', 'Domenica']);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接