summaryrefslogtreecommitdiff
path: root/plugins/Chess4Net/LocalizerUnit.pas
blob: bd4644bbc3cf6d8549829d3d0126de4f5befda45 (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
////////////////////////////////////////////////////////////////////////////////
// All code below is exclusively owned by author of Chess4Net - Pavel Perminov
// (packpaul@mail.ru, packpaul1@gmail.com).
// Any changes, modifications, borrowing and adaptation are a subject for
// explicit permition from the owner.

unit LocalizerUnit;

interface

uses
  SysUtils, TntSysUtils, Classes, TntClasses;

type
  // A singletone that is used to localize interface
  ILocalizable = interface
    procedure Localize;
  end;

  ELocalizer = class(Exception);
  TLocalizer = class
  private
    m_IniFileName: TFileName;
    m_iLangugesCount: integer;
    m_wstrarLanguageNames: array of WideString;
    m_wstrlLabels, m_wstrlMessages: TTntStringList;
    m_iActiveLanguage: integer;
    m_Subscribers: TList;

    constructor Create;
    procedure FReadLanguages;
    function FGetLangaugeName(iIndex: integer): WideString;
    procedure FSetActiveLanguage(iIndex: integer);
    function FReadLanguageData(iIndex: integer): boolean;
    procedure FNotifySubscribers;

  public
    destructor Destroy; override;
    class function Instance: TLocalizer;
    class procedure FreeInstance; reintroduce;
    property LanguagesCount: integer read m_iLangugesCount;
    // Returns a language name depending on its index
    function GetLabel(iIndex: integer): WideString;
    function GetMessage(iIndex: integer): WideString; overload;
    function GetMessage(iIndex: integer; wstrarParams: array of const): WideString; overload;
    procedure AddSubscriber(const Subscriber: ILocalizable);
    procedure DeleteSubscriber(const Subscriber: ILocalizable);
    property LanguageName[iIndex: integer]: WideString read FGetLangaugeName;
    property ActiveLanguage: integer read m_iActiveLanguage write FSetActiveLanguage;
  end;

implementation

uses
  StrUtils, Forms, TntIniFiles,
  //
  GlobalsUnit;

const
  LOCALIZER_INI_FILE = 'Lang.ini';

  // Labels are used to name interface controls s.a. TLabel and the like
  DEFAULT_LABELS: array[0..69] of WideString =
    ('Look & Feel Options', {0}
     'Animate Move:',
     'No',
     'Slowly',
     'Quickly',
     'Highlight Last Move',
     'Flash on incoming move',
     'Show Coordinates',
     'Stay Always on Top',
     'Extra Exit on ESC',
     'GUI Language:', {10}
     '&OK',
     '&Cancel',
     'White   ',
     'White ',
     'W ',
     'Black   ',
     'Black ',
     'B ',
     'Connecting...',
     'Wait until the connection is completed.', {20}
     '&Abort',
     'Press button to continue the game.',
     '&Continue',
     'Game Options',
     'Time Control',
     'Equal time for both players',
     'Your time',
     'Opponent''s time',
     'Unlimited',
     'Minutes per game:', {30}
     'Increment in seconds:',
     'Training Mode',
     'Enabled',
     'External base:',
     'Use user base',
     'Game can be paused',
     'Game can be adjourned',
     'Allow takebacks to your partner',
     'Auto Flag',
     'Yes', {40}
     'No',
     'OK',
     'Cancel',
     'Abort',
     'Retry',
     'Ignore',
     'All',
     'NoToAll',
     'YesToAll',
     'Help', {50}
     'Start Adjourned Game',
     'Start Standart Game',
     'Start PP Random Game',
     'Change Color',
     'Game Options...',
     'Look && Feel Options...',
     'About...',
     'Abort',
     'Draw',
     'Resign', {60}
     'Adjourn',
     'Pause',
     'Takeback',
     'If you liked plying Chess4Net give your credits at',
     '&Close',
     'Don''t show again',
     'Transmit Game',
     'Select Skype contact',
     'Broadcast...'
    );

  // Messages are used in message boxes
  DEFAULT_MESSAGES: array[0..37] of WideString =
    ('White is checkmated. You win.', {0}
     'White is checkmated. You loose.',
     'Black is checkmated. You win.',
     'Black is checkmated. You loose.',
     'It''s stalemate. No one wins.',
     'Your opponent leaves.',
     'Your opponent leaves. The game is aborted.',
     'An error during connection occured.',
     'Your opponent is using an older version of Chess4Net.' + sLineBreak +
       'Most of functionality will be not available.' + sLineBreak +
       'Please, ask him/her to update the client.',
     'The current version of Chess4Net is incompatible with the one of your partner.' + sLineBreak +
       'Please check the versions.',
     'Draw?', {10}
     'Can we abort the game?',
     'I resign. You win this game. Congratulations!',
     'The game is aborted.',
     'Sorry, but we have to finish this game.',
     'The game is drawn.',
     'No draw, sorry.',
     'You forfeited on time.',
     'Your opponent forfeited on time.',
     'Can we pause the game?',
     'No pause, sorry.', {20}
     'May I take back last move?',
     'Can we adjourn this game?',
     'No adjourns, sorry.',
     'Sorry, no takebacks!',
     'Do you really want to resign?',
     'Do you want to exit?',
     'The game is adjourned.',
     'You are currently playing some games. Do you want to start broadcasting?',
     'Black forfeits on time.',
     'White forfeits on time.', {30}
     'White resigns.',
     'Black resigns.',
     'Game transmition is not supported by this client!',
     'Broadcaster leaves. Transmition will be closed.',
     'Stalemate.',
     'White is checkmated.',
     'Black is checkmated.'
    );

var
  LocalizerInstance: TLocalizer = nil;

////////////////////////////////////////////////////////////////////////////////
// TLocalizer

constructor TLocalizer.Create;
begin
  m_IniFileName := Chess4NetPath + LOCALIZER_INI_FILE;

  m_wstrlLabels := TTntStringList.Create;
  m_wstrlMessages := TTntStringList.Create;

  m_Subscribers := TList.Create;

  m_iActiveLanguage := -1;
  FReadLanguages;
  FReadLanguageData(-1);
end;


destructor TLocalizer.Destroy;
begin
  Finalize(m_wstrarLanguageNames);

  m_Subscribers.Free;

  m_wstrlMessages.Free;
  m_wstrlLabels.Free;

  inherited;
end;


class function TLocalizer.Instance: TLocalizer;
begin
  if (not Assigned(LocalizerInstance)) then
    LocalizerInstance := TLocalizer.Create;
  Result := LocalizerInstance;
end;


class procedure TLocalizer.FreeInstance;
begin
  FreeAndNil(LocalizerInstance);
end;


procedure TLocalizer.FReadLanguages;
var
  IniFile: TTntIniFile;
  i: integer;
  wstrSection: WideString;
begin
  if (not FileExists(m_IniFileName)) then
    exit;

  IniFile := TTntIniFile.Create(m_IniFileName);
  try
    // Count available languages
    Finalize(m_wstrarLanguageNames);
    i := 0;
    repeat
      wstrSection := 'Language' + IntToStr(i + 1);
      if (not IniFile.SectionExists(wstrSection)) then
        break;
      SetLength(m_wstrarLanguageNames, length(m_wstrarLanguageNames) + 1);
      m_wstrarLanguageNames[i] := IniFile.ReadString(wstrSection, 'Name', '<unknown>');
      inc(i);
    until FALSE;
    m_iLangugesCount := i;

  finally
    IniFile.Free;
  end;
end;


function TLocalizer.FGetLangaugeName(iIndex: integer): WideString;
begin
  Result := m_wstrarLanguageNames[iIndex];
end;


procedure TLocalizer.FSetActiveLanguage(iIndex: integer);
begin
  if (iIndex <> m_iActiveLanguage) then
  begin
    if (FReadLanguageData(iIndex)) then
    begin
      m_iActiveLanguage := iIndex;
      FNotifySubscribers;
    end
    else
      m_iActiveLanguage := -1;
  end;
end;


function TLocalizer.FReadLanguageData(iIndex: integer): boolean;

  function NInsertLineFeeds(const wstrSource: WideString): WideString;
  var
    iPos, iOffset: integer;
  begin
    Result := '';
    iOffset := 1;
    repeat
      iPos := PosEx('/n', wstrSource, iOffset);
      if (iPos = 0) then
        break;
      if ((iPos = 1) or (wstrSource[iPos - 1] <> '/')) then
        Result := Result + Copy(wstrSource, iOffset, iPos - iOffset) + sLineBreak
      else
        Result := Result + Copy(wstrSource, iOffset, iPos - iOffset) + 'n'; // '//n' -> '/n'
      iOffset := iPos + 2;
    until FALSE;

    Result := Result + Copy(wstrSource, iOffset, MaxInt);
  end;

var
  IniFile: TTntIniFile;
  i: integer;
  wstrSection: WideString;
  wstrlValues: TTntStringList;
  wstrValue: WideString;
begin
  Result := FALSE;

  // Copy default values
  m_wstrlLabels.Clear;
  for i := Low(DEFAULT_LABELS) to High(DEFAULT_LABELS) do
    m_wstrlLabels.Add(DEFAULT_LABELS[i]);

  m_wstrlMessages.Clear;
  for i := Low(DEFAULT_MESSAGES) to High(DEFAULT_MESSAGES) do
    m_wstrlMessages.Add(DEFAULT_MESSAGES[i]);

  if (not FileExists(m_IniFileName)) then
    exit;    

  wstrlValues := nil;
  IniFile := TTntIniFile.Create(m_IniFileName);
  try
    wstrSection := 'Language' + IntToStr(iIndex + 1);
    if (not IniFile.SectionExists(wstrSection)) then
      exit;
      
    wstrlValues := TTntStringList.Create;
    IniFile.ReadSectionValues(wstrSection, wstrlValues);

    for i := 0 to m_wstrlLabels.Count - 1 do
    begin
      wstrValue := wstrlValues.Values['Label' + IntToStr(i)];
      if (wstrValue <> '') then
      begin
        if (wstrValue[length(wstrValue)] = '|') then // labels with a width
          wstrValue := Copy(wstrValue, 1, length(wstrValue) - 1);
        m_wstrlLabels[i] := wstrValue;
      end;
    end;
    for i := 0 to m_wstrlMessages.Count - 1 do
    begin
      wstrValue := wstrlValues.Values['Msg' + IntToStr(i)];
      if (wstrValue <> '') then
        m_wstrlMessages[i] := NInsertLineFeeds(wstrValue);
    end;

  finally
    wstrlValues.Free;
    IniFile.Free;
  end;

  Result := TRUE;
end;


function TLocalizer.GetLabel(iIndex: integer): WideString;
begin
  if (iIndex in [0..m_wstrlLabels.Count - 1]) then
    Result := m_wstrlLabels[iIndex]
  else
    ELocalizer.Create('Wrong label index!');
end;


function TLocalizer.GetMessage(iIndex: integer): WideString;
begin
  if (iIndex in [0..m_wstrlMessages.Count - 1]) then
    Result := m_wstrlMessages[iIndex]
  else
    ELocalizer.Create('Wrong message index!');
end;


function TLocalizer.GetMessage(iIndex: integer; wstrarParams: array of const): WideString;
begin
  Result := WideFormat(GetMessage(iIndex), wstrarParams)
end;


procedure TLocalizer.AddSubscriber(const Subscriber: ILocalizable);
begin
  if (m_Subscribers.IndexOf(Pointer(Subscriber)) < 0) then
    m_Subscribers.Add(Pointer(Subscriber));
end;


procedure TLocalizer.DeleteSubscriber(const Subscriber: ILocalizable);
var
  iIndex: integer;
begin
  iIndex := m_Subscribers.IndexOf(Pointer(Subscriber));
  if (iIndex >= 0) then
    m_Subscribers.Delete(iIndex);
end;


procedure TLocalizer.FNotifySubscribers;
var
  i: integer;
begin
  for i := 0 to m_Subscribers.Count - 1 do
    ILocalizable(m_Subscribers[i]).Localize;
end;

initialization

finalization
  TLocalizer.FreeInstance;

end.