Menu

[327ada]: / Source / MultiLangSupport.pas  Maximize  Restore  History

Download this file

418 lines (380 with data), 14.4 kB

  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
414
415
416
{
This file is part of Dev-C++
Copyright (c) 2004 Bloodshed Software
Dev-C++ is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Dev-C++ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Dev-C++; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
unit MultiLangSupport;
interface
uses
Windows, Dialogs, SysUtils, Classes;
{$I LangIDs.inc}
type
TCPData = record
CPID: Integer;
CPName: String;
CPLang: String;
end;
TdevMultiLangSupport = class(TObject)
private
fLangList: TStringList;
fLangListLoaded: Boolean;
fLangFile: String;
fCurLang: String;
fCurCodePage: Integer;
fStrings: TStringList;
fDefaultLangStrings: TStringList;
function GetString(ID: integer): String;
function GetLangName: String;
constructor Create;
public
destructor Destroy; override;
procedure SelectLanguage;
procedure CheckLanguageFiles;
procedure Open(const FileName: String);
procedure SetLang(const Lang: String);
function GetLangList: TStringList;
function FileFromDescription(const Desc: String): String;
property Strings[index: integer]: String read GetString; default;
property CurrentLanguage: String read GetLangName;
property Langs: TStringList read GetLangList;
end;
// https://stackoverflow.com/questions/2574289/codepage-id-to-codepage-name-getencoding-equivalent-in-delphi
const
MaxEncodings = 20;//148;
Encodings: Array[0..MaxEncodings - 1] of TCPData =
(
//(CPID: 37; CPName: 'IBM037'),
//(CPID: 437; CPName: 'IBM437'),
//(CPID: 500; CPName: 'IBM500'),
//(CPID: 708; CPName: 'ASMO-708'),
//(CPID: 720; CPName: 'DOS-720'),
//(CPID: 737; CPName: 'ibm737'),
//(CPID: 775; CPName: 'ibm775'),
//(CPID: 850; CPName: 'ibm850'),
//(CPID: 852; CPName: 'ibm852'),
//(CPID: 855; CPName: 'IBM855'),
//(CPID: 857; CPName: 'ibm857'),
//(CPID: 858; CPName: 'IBM00858'),
//(CPID: 860; CPName: 'IBM860'),
//(CPID: 861; CPName: 'ibm861'),
//(CPID: 862; CPName: 'DOS-862'),
//(CPID: 863; CPName: 'IBM863'),
//(CPID: 864; CPName: 'IBM864'),
//(CPID: 865; CPName: 'IBM865'),
//(CPID: 866; CPName: 'cp866'),
//(CPID: 869; CPName: 'ibm869'),
//(CPID: 870; CPName: 'IBM870'),
//(CPID: 874; CPName: 'windows-874'),
//(CPID: 875; CPName: 'cp875'),
(CPID: 932; CPName: 'shift_jis'; CPLang: 'Japanese';),
(CPID: 936; CPName: 'gb2312'; CPLang: 'Chinese';),
(CPID: 949; CPName: 'ks_c_5601-1987'; CPLang: 'Korean';),
//(CPID: 950; CPName: 'big5'),
//(CPID: 1026; CPName: 'IBM1026'),
//(CPID: 1047; CPName: 'IBM01047'),
//(CPID: 1140; CPName: 'IBM01140'),
//(CPID: 1141; CPName: 'IBM01141'),
//(CPID: 1142; CPName: 'IBM01142'),
//(CPID: 1143; CPName: 'IBM01143'),
//(CPID: 1144; CPName: 'IBM01144'),
//(CPID: 1145; CPName: 'IBM01145'),
//(CPID: 1146; CPName: 'IBM01146'),
//(CPID: 1147; CPName: 'IBM01147'),
//(CPID: 1148; CPName: 'IBM01148'),
//(CPID: 1149; CPName: 'IBM01149'),
//(CPID: 1200; CPName: 'utf-16'),
//(CPID: 1201; CPName: 'unicodeFFFE'),
(CPID: 1250; CPName: 'windows-1250'; CPLang: 'Croatian';),
(CPID: 1250; CPName: 'windows-1250'; CPLang: 'Czech';),
(CPID: 1250; CPName: 'windows-1250'; CPLang: 'Hungarian';),
(CPID: 1250; CPName: 'windows-1250'; CPLang: 'Polish';),
(CPID: 1250; CPName: 'windows-1250'; CPLang: 'Romanian';),
(CPID: 1250; CPName: 'windows-1250'; CPLang: 'Slovak';),
(CPID: 1250; CPName: 'windows-1250'; CPLang: 'Slovenian';),
(CPID: 1251; CPName: 'windows-1251'; CPLang: 'Russian';),
(CPID: 1251; CPName: 'windows-1251'; CPLang: 'Ukrainian';),
(CPID: 1252; CPName: 'Windows-1252'; CPLang: 'English';),
(CPID: 1253; CPName: 'windows-1253'; CPLang: 'Greek';),
(CPID: 1254; CPName: 'windows-1254'; CPLang: 'Turkish';),
(CPID: 1255; CPName: 'windows-1255'; CPLang: 'Hebrew';),
//(CPID: 1256; CPName: 'windows-1256'),
//(CPID: 1257; CPName: 'windows-1257'),
//(CPID: 1258; CPName: 'windows-1258'),
//(CPID: 1361; CPName: 'Johab'),
//(CPID: 10000; CPName: 'macintosh'),
//(CPID: 10001; CPName: 'x-mac-japanese'),
//(CPID: 10002; CPName: 'x-mac-chinesetrad'),
//(CPID: 10003; CPName: 'x-mac-korean'),
//(CPID: 10004; CPName: 'x-mac-arabic'),
//(CPID: 10005; CPName: 'x-mac-hebrew'),
//(CPID: 10006; CPName: 'x-mac-greek'),
//(CPID: 10007; CPName: 'x-mac-cyrillic'),
//(CPID: 10008; CPName: 'x-mac-chinesesimp'),
//(CPID: 10010; CPName: 'x-mac-romanian'),
//(CPID: 10017; CPName: 'x-mac-ukrainian'),
//(CPID: 10021; CPName: 'x-mac-thai'),
//(CPID: 10029; CPName: 'x-mac-ce'),
//(CPID: 10079; CPName: 'x-mac-icelandic'),
//(CPID: 10081; CPName: 'x-mac-turkish'),
//(CPID: 10082; CPName: 'x-mac-croatian'),
//(CPID: 12000; CPName: 'utf-32'),
//(CPID: 12001; CPName: 'utf-32BE'),
//(CPID: 20000; CPName: 'x-Chinese-CNS'),
//(CPID: 20001; CPName: 'x-cp20001'),
//(CPID: 20002; CPName: 'x-Chinese-Eten'),
//(CPID: 20003; CPName: 'x-cp20003'),
//(CPID: 20004; CPName: 'x-cp20004'),
//(CPID: 20005; CPName: 'x-cp20005'),
//(CPID: 20105; CPName: 'x-IA5'),
//(CPID: 20106; CPName: 'x-IA5-German'),
//(CPID: 20107; CPName: 'x-IA5-Swedish'),
//(CPID: 20108; CPName: 'x-IA5-Norwegian'),
//(CPID: 20127; CPName: 'us-ascii'),
//(CPID: 20261; CPName: 'x-cp20261'),
//(CPID: 20269; CPName: 'x-cp20269'),
//(CPID: 20273; CPName: 'IBM273'),
//(CPID: 20277; CPName: 'IBM277'),
//(CPID: 20278; CPName: 'IBM278'),
//(CPID: 20280; CPName: 'IBM280'),
//(CPID: 20284; CPName: 'IBM284'),
//(CPID: 20285; CPName: 'IBM285'),
//(CPID: 20290; CPName: 'IBM290'),
//(CPID: 20297; CPName: 'IBM297'),
//(CPID: 20420; CPName: 'IBM420'),
//(CPID: 20423; CPName: 'IBM423'),
//(CPID: 20424; CPName: 'IBM424'),
//(CPID: 20833; CPName: 'x-EBCDIC-KoreanExtended'),
//(CPID: 20838; CPName: 'IBM-Thai'),
//(CPID: 20866; CPName: 'koi8-r'),
//(CPID: 20871; CPName: 'IBM871'),
//(CPID: 20880; CPName: 'IBM880'),
//(CPID: 20905; CPName: 'IBM905'),
//(CPID: 20924; CPName: 'IBM00924'),
//(CPID: 20932; CPName: 'EUC-JP'),
//(CPID: 20936; CPName: 'x-cp20936'),
//(CPID: 20949; CPName: 'x-cp20949'),
(CPID: 21025; CPName: 'cp1025'; CPLang: 'Bulgarian';),
//(CPID: 21866; CPName: 'koi8-u'),
//(CPID: 28591; CPName: 'iso-8859-1'),
//(CPID: 28592; CPName: 'iso-8859-2'),
//(CPID: 28593; CPName: 'iso-8859-3'),
(CPID: 28594; CPName: 'iso-8859-4'; CPLang: 'Estonian';),
(CPID: 28594; CPName: 'iso-8859-4'; CPLang: 'Latvian';),
//(CPID: 28595; CPName: 'iso-8859-5'),
//(CPID: 28596; CPName: 'iso-8859-6'),
//(CPID: 28597; CPName: 'iso-8859-7'),
//(CPID: 28598; CPName: 'iso-8859-8'),
//(CPID: 28599; CPName: 'iso-8859-9'),
//(CPID: 28603; CPName: 'iso-8859-13'),
//(CPID: 28605; CPName: 'iso-8859-15'),
//(CPID: 29001; CPName: 'x-Europa'),
//(CPID: 38598; CPName: 'iso-8859-8-i'),
//(CPID: 50220; CPName: 'iso-2022-jp'),
//(CPID: 50221; CPName: 'csISO2022JP'),
//(CPID: 50222; CPName: 'iso-2022-jp'),
//(CPID: 50225; CPName: 'iso-2022-kr'),
//(CPID: 50227; CPName: 'x-cp50227'),
//(CPID: 51932; CPName: 'euc-jp'),
//(CPID: 51936; CPName: 'EUC-CN'),
//(CPID: 51949; CPName: 'euc-kr'),
//(CPID: 52936; CPName: 'hz-gb-2312'),
//(CPID: 54936; CPName: 'GB18030'),
//(CPID: 57002; CPName: 'x-iscii-de'),
//(CPID: 57003; CPName: 'x-iscii-be'),
//(CPID: 57004; CPName: 'x-iscii-ta'),
//(CPID: 57005; CPName: 'x-iscii-te'),
//(CPID: 57006; CPName: 'x-iscii-as'),
//(CPID: 57007; CPName: 'x-iscii-or'),
//(CPID: 57008; CPName: 'x-iscii-ka'),
//(CPID: 57009; CPName: 'x-iscii-ma'),
//(CPID: 57010; CPName: 'x-iscii-gu'),
//(CPID: 57011; CPName: 'x-iscii-pa'),
//(CPID: 65000; CPName: 'utf-7'),
(CPID: 65001; CPName: 'utf-8')
);
function Lang: TdevMultiLangSupport;
implementation
uses
System.WideStrUtils, System.UITypes, LangFrm, Forms, Utils, Version, Controls, devCFG;
var
fLangSingleton: TdevMultiLangSupport = nil;
function GetEncoding(CPID: Integer): String;
var
I: Integer;
begin
Result := 'iso-8859-2'; //put the default encoding here
for I := 0 to MaxEncodings - 1 do
if Encodings[I].CPID = CPID then
begin
Result := Encodings[I].CPName;
break;
end;
end;
function GetCodePage(const ALanguage: String): Integer;
var
I: Integer;
begin
Result := 1252; //put the default encoding here
for I := 0 to MaxEncodings - 1 do
if ALanguage.IndexOf(Encodings[I].CPLang)>-1 then
begin
Result := Encodings[I].CPID;
break;
end;
end;
function Lang: TdevMultiLangSupport;
begin
if not Assigned(fLangSingleton) and not Application.Terminated then
fLangSingleton := TdevMultiLangSupport.Create;
Result := fLangSingleton;
end;
constructor TdevMultiLangSupport.Create;
var
DefaultLangFile: String;
begin
inherited;
fLangList := TStringList.Create;
fLangListLoaded := False; // only load when needed
fStrings := TStringList.Create;
fDefaultLangStrings := TStringList.Create;
fCurCodePage := 1252;
// Load default english file from languages directory
DefaultLangFile := ValidateFile('English.lng', devDirs.Lang);
if DefaultLangFile = '' then begin
MessageDlg('Could not open language file English.lng', mtError, [mbOK], 0);
end else begin
fDefaultLangStrings.LoadFromFile(DefaultLangFile);
end;
end;
destructor TdevMultiLangSupport.Destroy;
begin
FreeAndNil(fLangList);
FreeAndNil(fStrings);
FreeAndNil(fDefaultLangStrings);
fLangSingleton := nil;
inherited;
end;
procedure TdevMultiLangSupport.Open(const Filename: String);
var
LangFile: String;
begin
// Load file from languages directory
LangFile := ValidateFile(Filename, devDirs.Lang);
if LangFile = '' then begin
MessageDlg('Could not open language file ' + Filename, mtError, [mbOK], 0);
Exit;
end;
// Load file into fStrings
fStrings.LoadFromFile(LangFile,TEncoding.ANSI);
fLangFile := LangFile;
// Get languange
fCurLang := fStrings.Values['Lang'];
if fCurLang = '' then
fCurLang := ChangeFileExt(ExtractFileName(LangFile), '');
fCurCodePage := GetCodePage(fCurLang);
devData.Language := ExtractFileName(LangFile);
end;
procedure TdevMultiLangSupport.CheckLanguageFiles;
var
I: integer;
s: String;
sl: TStringList;
begin
try
fLangList.Clear;
if devDirs.Lang = '' then
Exit;
// Load list of all files
FilesFromWildcard(devDirs.Lang, '*.lng', fLangList, False, False, True);
if fLangList.Count > 0 then begin
fLangList.Sort;
sl := TStringList.Create;
try
for I := 0 to fLangList.Count - 1 do begin
sl.LoadFromFile(fLangList[I]);
s := sl.Values['Lang'];
if SameText(ExtractFileName(fLangList[I]), 'English.lng') and (devData.Language = '') then
fCurLang := s;
if s = '' then
fLangList[I] := format('%s=%s', [fLangList[I], ChangeFileExt(ExtractFileName(fLangList[I]), '')])
else
fLangList[I] := format('%s=%s', [fLangList[I], s]);
end;
finally
sl.Free;
end;
end;
if fCurLang = '' then
fCurLang := devData.Language;
finally
fLangListLoaded := True;
end;
end;
function TdevMultiLangSupport.GetString(ID: integer): String;
begin
result := AnsiToUtf8Ex(fStrings.Values[IntToStr(ID)],fCurCodePage);
if Result = '' then
Result := fDefaultLangStrings.Values[IntToStr(ID)];
if result = '' then
result := format('<ID %d translation missing>', [ID])
else
result := StringReplace(result, '<CR>', #13#10, [rfReplaceAll]);
end;
function TdevMultiLangSupport.GetLangName: String;
begin
result := fCurLang;
end;
procedure TdevMultiLangSupport.SelectLanguage;
begin
with TLangForm.Create(nil) do try
ShowModal;
finally
Free;
end;
end;
procedure TdevMultiLangSupport.SetLang(const Lang: String);
var
I: integer;
begin
if SameText(Lang, fCurLang) then
exit;
if not fLangListLoaded then
CheckLanguageFiles; // can be slow, so load on demand
for I := 0 to fLangList.Count - 1 do
if SameText(ExtractFileName(fLangList.Names[I]), Lang) then begin
Open(fLangList.Names[I]);
break;
end;
end;
function TdevMultiLangSupport.FileFromDescription(const Desc: String): String;
var
i: integer;
begin
// returns the filename of the lang file described as Desc
// for example with Desc="English (Original)", returns "English.lng"
Result := Desc;
if not fLangListLoaded then
CheckLanguageFiles; // can be slow, so load on demand
for i := 0 to fLangList.Count - 1 do
if SameText(fLangList.ValueFromIndex[i], Desc) then begin
Result := ExtractFilename(fLangList.Names[i]);
Break;
end;
end;
function TdevMultiLangSupport.GetLangList: TStringList;
begin
if not fLangListLoaded then
CheckLanguageFiles; // can be slow, so load on demand
Result := fLangList;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.