File: unit2.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (129 lines) | stat: -rw-r--r-- 3,819 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
unit Unit2;

{$mode objfpc}{$H+}

interface

uses
  SysUtils, StdCtrls, ExtCtrls, LocalizedForms;

type

  { TForm2 }

  TForm2 = class(TLocalizedForm)
    CheckGroup: TCheckGroup;
    GroupBox1: TGroupBox;
    LblSum: TLabel;
    LblMoney: TLabel;
    LblTodayIs: TLabel;
    RadioGroup: TRadioGroup;
    procedure CheckGroupItemClick(Sender: TObject; Index: integer);
    procedure FormCreate(Sender: TObject);
    procedure RadioGroupSelectionChanged(Sender: TObject);
  private
    procedure CalculateSum;
  protected
    procedure UpdateTranslation(ALang: String); override;
  public
  end;

var
  Form2: TForm2;

implementation

{$R *.lfm}

uses
  LConvEncoding, // for ConvertEncoding - see "UpdateTranslation"
  StringsUnit;

procedure TForm2.FormCreate(Sender: TObject);
begin
  UpdateTranslation(CurrentLang);
end;

{ This procedure shows
  - how to combine translated resource strings by means of the format statement
  - how to use arrays of resource strings: use pointers to the resource strings! }
procedure TForm2.CalculateSum;
const
  numbers: array[1..9] of PString = (@rsOne, @rsTwo, @rsThree, @rsFour, @rsFive,
    @rsSix, @rsSeven, @rsEight, @rsNine);
var
  sum: Integer;
begin
  sum := RadioGroup.ItemIndex + 1;
  if CheckGroup.Checked[0] then sum := sum + 1;
  if CheckGroup.Checked[1] then sum := sum + 2;
  if CheckGroup.Checked[2] then sum := sum + 3;
  LblSum.Caption := Format(rsSumOfSelectedNumbers, [numbers[sum]^]);
end;

procedure TForm2.CheckGroupItemClick(Sender: TObject; Index: integer);
begin
  CalculateSum;
end;

procedure TForm2.RadioGroupSelectionChanged(Sender: TObject);
begin
  CalculateSum;
end;

procedure TForm2.UpdateTranslation(ALang: String);
var
  s: String;
begin
  inherited;

  { DefaultTranslator cannot execute code, i.e. strings combined by means of
    the Format statement are not translated automatically. Such a string is
    created for the caption of LblSum in "CalculateSum" - we have to call this
    method here to get that label translated. }
  CalculateSum;

  { There is a complication for the labels LblTodayIs which displays the current
    date, and with LblMoney which displays some amount of money with the currency
    sign.
    Formatting for these data is extracted from the DefaultFormatSettings.
    The resulting strings are encoded in ansi and do not display locale-specific
    characters. To get this right they have to be converted to UTF8.
    Usually, it is sufficient to call SysToUTF8 for this purpose. Our example,
    however, allows for Hebrew characters which are usually not contained in the
    typical code pages. Therefore, we use'll a more general procedure based on
    ConvertEncoding which allows to specify the source code page which had been
    determined when UpdateFormatSettings had been called in the LocalizedForms
    unit. }
  s := ConvertEncoding(
    FormatDateTime(DefaultFormatSettings.LongDateFormat, date),  // string to convert
    CodePage,      // source encoding as defined by "CodePage"
    EncodingUTF8   // destination encoding - UTF8
  );
  LblTodayIs.Caption := Format(rsTodayIs, [s]);
  { Note: "ConvertEncoding" requires the unit LConvEncoding in the uses clause. }

  { Now the same with LblMoney... }
  LblMoney.Caption := ConvertEncoding(
    Format('%.*n %s', [
      DefaultFormatSettings.CurrencyDecimals,
      10e6,
      DefaultFormatSettings.CurrencyString
    ]),
    CodePage, EncodingUTF8
  );

  { The items of TRadiogroup are not translated automatically. }
  with RadioGroup do begin
    Items[0] := rsOne;
    Items[1] := rsTwo;
    Items[2] := rsThree;
  end;

  { We should translate CheckGroup here also. But we don't do this here
    to demonstrate the effect when the items of the CheckGroup are not
    translated explicitly as we did with the RadioGroup. }
end;

end.