-
Notifications
You must be signed in to change notification settings - Fork 1
/
gnugettext.pas
4338 lines (4033 loc) · 142 KB
/
gnugettext.pas
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ *------------------------------------------------------------------------------
GNU gettext translation system for Delphi, Kylix, C++ Builder and others.
All parts of the translation system are kept in this unit.
@author Lars B. Dybdahl and others
@version $LastChangedRevision: 421 $
@see http://dybdahl.dk/dxgettext/
------------------------------------------------------------------------------- }
unit gnugettext;
(* ************************************************************ *)
(* *)
(* (C) Copyright by Lars B. Dybdahl and others *)
(* E-mail: [email protected], phone +45 70201241 *)
(* *)
(* Contributors: Peter Thornqvist, Troy Wolbrink, *)
(* Frank Andreas de Groot, Igor Siticov, *)
(* Jacques Garcia Vazquez, Igor Gitman, *)
(* Arvid Winkelsdorf *)
(* *)
(* See http://dybdahl.dk/dxgettext/ for more information *)
(* *)
(* ************************************************************ *)
// Information about this file:
// $LastChangedDate: 2012-10-31 15:46:19 +0100 (mer. 31 oct. 2012) $
// $LastChangedRevision: 421 $
// $HeadURL: http://svn.berlios.de/svnroot/repos/dxgettext/trunk/dxgettext/sample/gnugettext.pas $
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// The names of any contributor may not be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
interface
// If the conditional define DXGETTEXTDEBUG is defined, debugging log is activated.
// Use DefaultInstance.DebugLogToFile() to write the log to a file.
{ $define DXGETTEXTDEBUG }
{$IFDEF VER140}
// Delphi 6
{$IFDEF MSWINDOWS}
{$DEFINE DELPHI6OROLDER}
{$ENDIF}
{$ENDIF}
{$IFDEF VER150}
// Delphi 7
{$DEFINE DELPHI7OROLDER}
{$ENDIF}
{$IFDEF VER160}
// Delphi 8
{$DEFINE DELPHI8OROLDER}
{$ENDIF}
{$IFDEF VER170}
// Delphi 2005
{$DEFINE DELPHI2005OROLDER}
{$ENDIF}
{$IFDEF VER180}
{$IFNDEF VER185}
// Delphi 2006
{$DEFINE DELPHI2006OROLDER}
{$ELSE}
// Delphi 2007
{$DEFINE DELPHI2007OROLDER}
{$ENDIF}
{$ENDIF}
{$IFDEF VER190}
// Delphi 2007
{$DEFINE DELPHI2007OROLDER}
{$ENDIF}
{$IFDEF VER200}
// Delphi 2009 with Unicode
{$DEFINE DELPHI2009OROLDER}
{$ENDIF}
{$IFDEF VER210}
// Delphi 2010 with Unicode
{$DEFINE DELPHI2010OROLDER}
{$ENDIF}
{$IFDEF VER220}
// Delphi 2011 with Unicode
{$DEFINE DELPHI2011OROLDER}
{$ENDIF}
{$IFDEF VER230}
// Delphi 2012 with Unicode
{$DEFINE DELPHI2012OROLDER}
{$ENDIF}
{$IFDEF VER240}
// Delphi 2013/XE3 with Unicode
{$DEFINE DELPHI2013OROLDER}
{$ENDIF}
{$IFDEF DELPHI2013OROLDER}
{$DEFINE DELPHI2012OROLDER}
{$ENDIF}
{$IFDEF DELPHI2012OROLDER}
{$DEFINE DELPHI2011OROLDER}
{$ENDIF}
{$IFDEF DELPHI2011OROLDER}
{$DEFINE DELPHI2010OROLDER}
{$ENDIF}
{$IFDEF DELPHI2010OROLDER}
{$DEFINE DELPHI2009OROLDER}
{$ENDIF}
{$IFDEF DELPHI2009OROLDER}
{$DEFINE DELPHI2007OROLDER}
{$ENDIF}
{$IFDEF DELPHI2007OROLDER}
{$DEFINE DELPHI7OROLDER}
{$ENDIF}
{$IFDEF DELPHI7OROLDER}
{$DEFINE DELPHI6OROLDER}
{$ENDIF}
uses
{$IFDEF MSWINDOWS}
Windows,
{$ELSE}
Libc,
{$IFDEF FPC}
CWString,
{$ENDIF}
{$ENDIF}
Classes, StrUtils, SysUtils, TypInfo;
(* *************************************************************************** *)
(* *)
(* MAIN API *)
(* *)
(* *************************************************************************** *)
type
{$IFNDEF UNICODE}
UnicodeString = WideString;
RawUtf8String = AnsiString;
RawByteString = AnsiString;
{$ELSE}
RawUtf8String = RawByteString;
{$ENDIF}
DomainString = string;
LanguageString = string;
ComponentNameString = string;
FilenameString = string;
MsgIdString = UnicodeString;
TranslatedUnicodeString = UnicodeString;
// Main GNU gettext functions. See documentation for instructions on how to use them.
function _(const szMsgId: MsgIdString): TranslatedUnicodeString;
function gettext(const szMsgId: MsgIdString): TranslatedUnicodeString;
function gettext_NoExtract(const szMsgId: MsgIdString): TranslatedUnicodeString;
function gettext_NoOp(const szMsgId: MsgIdString): TranslatedUnicodeString;
function dgettext(const szDomain: DomainString; const szMsgId: MsgIdString)
: TranslatedUnicodeString;
function dgettext_NoExtract(const szDomain: DomainString;
const szMsgId: MsgIdString): TranslatedUnicodeString;
function dngettext(const szDomain: DomainString;
const singular, plural: MsgIdString; Number: longint)
: TranslatedUnicodeString;
function ngettext(const singular, plural: MsgIdString; Number: longint)
: TranslatedUnicodeString;
function ngettext_NoExtract(const singular, plural: MsgIdString;
Number: longint): TranslatedUnicodeString;
procedure textdomain(const szDomain: DomainString);
function getcurrenttextdomain: DomainString;
procedure bindtextdomain(const szDomain: DomainString;
const szDirectory: FilenameString);
// Set language to use
procedure UseLanguage(LanguageCode: LanguageString);
function GetCurrentLanguage: LanguageString;
// Translates a component (form, frame etc.) to the currently selected language.
// Put TranslateComponent(self) in the OnCreate event of all your forms.
// See the manual for documentation on these functions
type
TTranslator = procedure(obj: TObject) of object;
procedure TP_Ignore(AnObject: TObject; const name: ComponentNameString);
procedure TP_IgnoreClass(IgnClass: TClass);
procedure TP_IgnoreClassProperty(IgnClass: TClass;
const propertyname: ComponentNameString);
procedure TP_GlobalIgnoreClass(IgnClass: TClass);
function TP_TryGlobalIgnoreClass(IgnClass: TClass): boolean;
procedure TP_GlobalIgnoreClassProperty(IgnClass: TClass;
const propertyname: ComponentNameString);
procedure TP_GlobalHandleClass(HClass: TClass; Handler: TTranslator);
procedure TP_Remember(AnObject: TObject; PropName: ComponentNameString;
OldValue: TranslatedUnicodeString);
procedure TranslateComponent(AnObject: TComponent;
const textdomain: DomainString = '');
procedure RetranslateComponent(AnObject: TComponent;
const textdomain: DomainString = '');
// Add more domains that resourcestrings can be extracted from. If a translation
// is not found in the default domain, this domain will be searched, too.
// This is useful for adding mo files for certain runtime libraries and 3rd
// party component libraries
procedure AddDomainForResourceString(const domain: DomainString);
procedure RemoveDomainForResourceString(const domain: DomainString);
// Add more domains that component strings can be extracted from. If a translation
// is not found in the default domain, this domain will be searched, too.
// This is useful when an application inherits components from a 3rd
// party component libraries
procedure AddDomainForComponent(const domain: DomainString);
procedure RemoveDomainForComponent(const domain: DomainString);
// Unicode-enabled way to get resourcestrings, automatically translated
// Use like this: ws:=LoadResStringW(@NameOfResourceString);
function LoadResString(ResStringRec: PResStringRec): WideString;
function LoadResStringW(ResStringRec: PResStringRec): UnicodeString;
// This returns an empty string if not translated or translator name is not specified.
function GetTranslatorNameAndEmail: TranslatedUnicodeString;
(* *************************************************************************** *)
(* *)
(* ADVANCED FUNCTIONALITY *)
(* *)
(* *************************************************************************** *)
const
DefaultTextDomain = 'default';
var
ExecutableFilename: FilenameString;
// This is set to paramstr(0) or the name of the DLL you are creating.
const
PreferExternal = False;
// Set to true, to prefer external *.mo over embedded translation
UseMemoryMappedFiles = True;
// Set to False, to use the mo-file as independent copy in memory (you can update the file while it is in use)
ReReadMoFileOnSameLanguage = False;
// Set to True, to reread mo-file if the current language is selected again
const
// Subversion source code version control version information
VCSVersion = '$LastChangedRevision: 421 $';
type
EGnuGettext = class(Exception);
EGGProgrammingError = class(EGnuGettext);
EGGComponentError = class(EGnuGettext);
EGGIOError = class(EGnuGettext);
EGGAnsi2WideConvError = class(EGnuGettext);
// This function will turn resourcestring hooks on or off, eventually with BPL file support.
// Please do not activate BPL file support when the package is in design mode.
const
AutoCreateHooks = True;
procedure HookIntoResourceStrings(enabled: boolean = True;
SupportPackages: boolean = False);
(* *************************************************************************** *)
(* *)
(* CLASS based implementation. *)
(* Use TGnuGettextInstance to have more than one language *)
(* in your application at the same time *)
(* *)
(* *************************************************************************** *)
{$IFDEF MSWINDOWS}
{$IFNDEF DELPHI6OROLDER}
{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CODE OFF}
{$WARN UNSAFE_CAST OFF}
{$ENDIF}
{$ENDIF}
type
TOnDebugLine = Procedure(Sender: TObject; const Line: String;
var Discard: boolean) of Object;
// Set Discard to false if output should still go to ordinary debug log
TGetPluralForm = function(Number: longint): Integer;
TDebugLogger = procedure(Line: AnsiString) of object;
{ *------------------------------------------------------------------------------
Handles .mo files, in separate files or inside the exe file.
Don't use this class. It's for internal use.
------------------------------------------------------------------------------- }
TMoFile = class
/// Threadsafe. Only constructor and destructor are writing to memory
private
doswap: boolean;
public
Users: Integer;
/// Reference count. If it reaches zero, this object should be destroyed.
constructor Create(const filename: FilenameString; const Offset: int64;
Size: int64; const xUseMemoryMappedFiles: boolean);
destructor Destroy; override;
function gettext(const msgid: RawUtf8String; var found: boolean)
: RawUtf8String; // uses mo file and utf-8
property isSwappedArchitecture: boolean read doswap;
private
N, O, T: Cardinal;
/// Values defined at http://www.linuxselfhelp.com/gnu/gettext/html_chapter/gettext_6.html
startindex, startstep: Integer;
FUseMemoryMappedFiles: boolean;
mo: THandle;
momapping: THandle;
momemoryHandle: PAnsiChar;
momemory: PAnsiChar;
function autoswap32(i: Cardinal): Cardinal;
function CardinalInMem(baseptr: PAnsiChar; Offset: Cardinal): Cardinal;
end;
{ *------------------------------------------------------------------------------
Handles all issues regarding a specific domain.
Don't use this class. It's for internal use.
------------------------------------------------------------------------------- }
TDomain = class
private
enabled: boolean;
vDirectory: FilenameString;
procedure setDirectory(const dir: FilenameString);
public
DebugLogger: TDebugLogger;
domain: DomainString;
property Directory: FilenameString read vDirectory write setDirectory;
constructor Create;
destructor Destroy; override;
// Set parameters
procedure SetLanguageCode(const langcode: LanguageString);
procedure SetFilename(const filename: FilenameString);
// Bind this domain to a specific file
// Get information
procedure GetListOfLanguages(list: TStrings);
function GetTranslationProperty(propertyname: ComponentNameString)
: TranslatedUnicodeString;
function gettext(const msgid: RawUtf8String): RawUtf8String;
// uses mo file and utf-8
private
mofile: TMoFile;
SpecificFilename: FilenameString;
curlang: LanguageString;
OpenHasFailedBefore: boolean;
procedure OpenMoFile;
procedure CloseMoFile;
end;
{ *------------------------------------------------------------------------------
Helper class for invoking events.
------------------------------------------------------------------------------- }
TExecutable = class
procedure Execute; virtual; abstract;
end;
{ *------------------------------------------------------------------------------
Interface to implement if you want to register as a language change listener
------------------------------------------------------------------------------- }
IGnuGettextInstanceWhenNewLanguageListener = interface
procedure WhenNewLanguage(const LanguageID: LanguageString);
end;
{ *------------------------------------------------------------------------------
The main translation engine.
------------------------------------------------------------------------------- }
TGnuGettextInstance = class
private
fOnDebugLine: TOnDebugLine;
CreatorThread: Cardinal;
/// Only this thread can use LoadResString
public
enabled: boolean;
/// Set this to false to disable translations
DesignTimeCodePage: Integer;
/// See MultiByteToWideChar() in Win32 API for documentation
constructor Create;
destructor Destroy; override;
procedure UseLanguage(LanguageCode: LanguageString);
procedure GetListOfLanguages(const domain: DomainString; list: TStrings);
// Puts list of language codes, for which there are translations in the specified domain, into list
{$IFNDEF UNICODE}
function gettext(const szMsgId: AnsiString): TranslatedUnicodeString;
overload; virtual;
function ngettext(const singular, plural: AnsiString; Number: longint)
: TranslatedUnicodeString; overload; virtual;
{$ENDIF}
function gettext(const szMsgId: MsgIdString): TranslatedUnicodeString;
overload; virtual;
function gettext_NoExtract(const szMsgId: MsgIdString)
: TranslatedUnicodeString;
function gettext_NoOp(const szMsgId: MsgIdString): TranslatedUnicodeString;
function ngettext(const singular, plural: MsgIdString; Number: longint)
: TranslatedUnicodeString; overload; virtual;
function ngettext_NoExtract(const singular, plural: MsgIdString;
Number: longint): TranslatedUnicodeString;
function GetCurrentLanguage: LanguageString;
function GetTranslationProperty(const propertyname: ComponentNameString)
: TranslatedUnicodeString;
function GetTranslatorNameAndEmail: TranslatedUnicodeString;
// Form translation tools, these are not threadsafe. All TP_ procs must be called just before TranslateProperites()
procedure TP_Ignore(AnObject: TObject; const name: ComponentNameString);
procedure TP_IgnoreClass(IgnClass: TClass);
procedure TP_IgnoreClassProperty(IgnClass: TClass;
propertyname: ComponentNameString);
function TP_TryGlobalIgnoreClass(IgnClass: TClass): boolean;
procedure TP_GlobalIgnoreClass(IgnClass: TClass);
procedure TP_GlobalIgnoreClassProperty(IgnClass: TClass;
propertyname: ComponentNameString);
procedure TP_GlobalHandleClass(HClass: TClass; Handler: TTranslator);
procedure TP_Remember(AnObject: TObject; PropName: ComponentNameString;
OldValue: TranslatedUnicodeString);
procedure TranslateProperties(AnObject: TObject;
textdomain: DomainString = '');
procedure TranslateComponent(AnObject: TComponent;
const textdomain: DomainString = '');
procedure RetranslateComponent(AnObject: TComponent;
const textdomain: DomainString = '');
// Multi-domain functions
{$IFNDEF UNICODE}
function dgettext(const szDomain: DomainString; const szMsgId: AnsiString)
: TranslatedUnicodeString; overload; virtual;
function dngettext(const szDomain: DomainString;
const singular, plural: AnsiString; Number: longint)
: TranslatedUnicodeString; overload; virtual;
{$ENDIF}
function dgettext(const szDomain: DomainString; const szMsgId: MsgIdString)
: TranslatedUnicodeString; overload; virtual;
function dgettext_NoExtract(const szDomain: DomainString;
const szMsgId: MsgIdString): TranslatedUnicodeString;
function dngettext(const szDomain: DomainString;
const singular, plural: MsgIdString; Number: longint)
: TranslatedUnicodeString; overload; virtual;
function dngettext_NoExtract(const szDomain: DomainString;
const singular, plural: MsgIdString; Number: longint)
: TranslatedUnicodeString;
procedure textdomain(const szDomain: DomainString);
function getcurrenttextdomain: DomainString;
procedure bindtextdomain(const szDomain: DomainString;
const szDirectory: FilenameString);
procedure bindtextdomainToFile(const szDomain: DomainString;
const filename: FilenameString);
// Also works with files embedded in exe file
// Windows API functions
function LoadResString(ResStringRec: PResStringRec): UnicodeString;
// Output all log info to this file. This may only be called once.
procedure DebugLogToFile(const filename: FilenameString;
append: boolean = False);
procedure DebugLogPause(PauseEnabled: boolean);
property OnDebugLine: TOnDebugLine read fOnDebugLine write fOnDebugLine;
// If set, all debug output goes here
{$IFNDEF UNICODE}
// Conversion according to design-time character set
function ansi2wideDTCP(const s: AnsiString): MsgIdString;
// Convert using Design Time Code Page
{$ENDIF}
procedure RegisterWhenNewLanguageListener
(Listener: IGnuGettextInstanceWhenNewLanguageListener);
procedure UnregisterWhenNewLanguageListener
(Listener: IGnuGettextInstanceWhenNewLanguageListener);
protected
procedure TranslateStrings(sl: TStrings; const textdomain: DomainString);
// Override these three, if you want to inherited from this class
// to create a new class that handles other domain and language dependent
// issues
procedure WhenNewLanguage(const LanguageID: LanguageString); virtual;
// Override to know when language changes
procedure WhenNewDomain(const textdomain: DomainString); virtual;
// Override to know when text domain changes. Directory is purely informational
procedure WhenNewDomainDirectory(const textdomain: DomainString;
const Directory: FilenameString); virtual;
// Override to know when any text domain's directory changes. It won't be called if a domain is fixed to a specific file.
private
curlang: LanguageString;
curGetPluralForm: TGetPluralForm;
curmsgdomain: DomainString;
savefileCS: TMultiReadExclusiveWriteSynchronizer;
savefile: TextFile;
savememory: TStringList;
DefaultDomainDirectory: FilenameString;
domainlist: TStringList;
/// List of domain names. Objects are TDomain.
TP_IgnoreList: TStringList;
/// Temporary list, reset each time TranslateProperties is called
TP_ClassHandling: TList;
/// Items are TClassMode. If a is derived from b, a comes first
TP_GlobalClassHandling: TList;
/// Items are TClassMode. If a is derived from b, a comes first
TP_Retranslator: TExecutable;
/// Cast this to TTP_Retranslator
fWhenNewLanguageListeners: TInterfaceList;
/// List of all registered WhenNewLanguage listeners
{$IFDEF DXGETTEXTDEBUG}
DebugLogCS: TMultiReadExclusiveWriteSynchronizer;
DebugLog: TStream;
DebugLogOutputPaused: boolean;
{$ENDIF}
function TP_CreateRetranslator: TExecutable; // Must be freed by caller!
procedure FreeTP_ClassHandlingItems;
function ClassIsIgnored(AClass: TClass): boolean;
{$IFDEF DXGETTEXTDEBUG}
procedure DebugWriteln(Line: AnsiString);
{$ENDIF}
procedure TranslateProperty(AnObject: TObject; PropInfo: PPropInfo;
TodoList: TStrings; const textdomain: DomainString);
function Getdomain(const domain: DomainString;
const DefaultDomainDirectory: FilenameString;
const curlang: LanguageString): TDomain;
// Translates a single property of an object
end;
const
LOCALE_SISO639LANGNAME = $59; // Used by Lazarus software development tool
LOCALE_SISO3166CTRYNAME = $5A; // Used by Lazarus software development tool
var
DefaultInstance: TGnuGettextInstance;
/// Default instance of the main API for singlethreaded applications.
implementation
{$IFDEF DELPHI2012OROLDER}
uses
Vcl.Themes;
{$ENDIF DELPHI2012OROLDER}
{$IFNDEF MSWINDOWS}
{$IFNDEF LINUX}
'This version of gnugettext.pas is only meant to be compiled with Kylix 3,'
'Delphi 6, Delphi 7 and later versions. If you use other versions, please'
'get the gnugettext.pas version from the Delphi 5 directory.'
{$ENDIF}
{$ENDIF}
(* ************************************************************************ *)
// Some comments on the implementation:
// This unit should be independent of other units where possible.
// It should have a small footprint in any way.
(* ************************************************************************ *)
// TMultiReadExclusiveWriteSynchronizer is used instead of TCriticalSection
// because it makes this unit independent of the SyncObjs unit
(* ************************************************************************ *)
{$B-,R+,I+,Q+}
type TTP_RetranslatorItem = class obj: TObject;
PropName:
ComponentNameString;
OldValue:
TranslatedUnicodeString;
end;
TTP_Retranslator = class(TExecutable)textdomain: DomainString;
Instance:
TGnuGettextInstance;
constructor Create;
destructor Destroy; override;
procedure Remember(obj: TObject; PropName: ComponentNameString;
OldValue: TranslatedUnicodeString);
procedure Execute; override;
private
list: TList;
end;
TEmbeddedFileInfo = class Offset, Size: int64;
end;
TFileLocator = class
// This class finds files even when embedded inside executable
constructor Create;
destructor Destroy;
override;
function FindSignaturePos(const signature: RawByteString;
str: TFileStream): int64;
procedure Analyze; // List files embedded inside executable
function FileExists(filename: FilenameString): boolean;
function GetMoFile(filename: FilenameString;
DebugLogger: TDebugLogger): TMoFile;
procedure ReleaseMoFile(mofile: TMoFile);
private
basedirectory: FilenameString;
filelist: TStringList;
// Objects are TEmbeddedFileInfo. Filenames are relative to .exe file
MoFilesCS: TMultiReadExclusiveWriteSynchronizer;
MoFiles: TStringList;
// Objects are filenames+offset, objects are TMoFile
function ReadInt64(str: TStream): int64;
end;
TGnuGettextComponentMarker = class(TComponent)public LastLanguage
: LanguageString;
Retranslator: TExecutable;
destructor Destroy;
override;
end;
TClassMode = class HClass: TClass;
SpecialHandler: TTranslator;
PropertiesToIgnore: TStringList; // This is ignored if Handler is set
constructor Create;
destructor Destroy;
override;
end;
TRStrinfo = record strlength, stroffset: Cardinal;
end;
TStrInfoArr = array [0 .. 10000000] of TRStrinfo;
PStrInfoArr = ^TStrInfoArr;
TCharArray5 = array [0 .. 4] of ansichar;
THook = // Replaces a runtime library procedure with a custom procedure
class public constructor Create(OldProcedure, NewProcedure: pointer;
FollowJump: boolean = False);
destructor Destroy;
override; // Restores unhooked state
procedure Reset(FollowJump: boolean = False);
// Disables and picks up patch points again
procedure Disable;
procedure Enable;
private
oldproc, newproc: pointer;
Patch: TCharArray5;
Original: TCharArray5;
PatchPosition: PAnsiChar;
procedure Shutdown;
// Same as destroy, except that object is not destroyed
end;
PProxyClassData = ^TProxyClassData;
TProxyClassData = record SelfPtr: TClass;
IntfTable: pointer;
AutoTable: pointer;
InitTable: pointer;
TypeInfo: PTypeInfo;
FieldTable: pointer;
MethodTable: pointer;
DynamicTable: pointer;
ClassName: PShortString;
InstanceSize: Integer;
Parent: ^TClass;
end;
THookedObjects = class(TList)private interceptorClassDatas: TList;
interceptorClassDataParents: array of TClass;
function findInterceptorClassData(AClass: TClass): pointer;
procedure BeforeDestructionHook;
function GetBeforeDestructionHookAddress: pointer;
public
constructor Create;
destructor Destroy;
override;
procedure Proxify(obj: TObject);
procedure Unproxify(obj: TObject);
end;
var
// System information
Win32PlatformIsUnicode: boolean = False;
// Information about files embedded inside .exe file
FileLocator: TFileLocator;
// Hooks into runtime library functions
ResourceStringDomainListCS: TMultiReadExclusiveWriteSynchronizer;
ResourceStringDomainList: TStringList;
ComponentDomainListCS: TMultiReadExclusiveWriteSynchronizer;
ComponentDomainList: TStringList;
HookLoadResString: THook;
HookLoadStr: THook;
HookFmtLoadStr: THook;
HookedObjects: THookedObjects;
KnownRetranslators: TList;
function GGGetEnvironmentVariable(const name: WideString): WideString;
var
Len: Integer;
W: WideString;
begin
Result := '';
SetLength(W, 1);
Len := Windows.GetEnvironmentVariableW(PWideChar(Name),
PWideChar(W), 1);
if Len > 0 then
begin
SetLength(Result, Len - 1);
Windows.GetEnvironmentVariableW(PWideChar(Name),
PWideChar(Result), Len);
end;
end;
function StripCRRawMsgId(s: RawUtf8String): RawUtf8String;
var
i: Integer;
begin
i := 1;
while i <= length(s) do
begin
if s[i] = #13 then
delete(s, i, 1)
else
inc(i);
end;
Result := s;
end;
function EnsureLineBreakInTranslatedString(s: RawUtf8String)
: RawUtf8String;
{$IFDEF MSWINDOWS}
var
i: Integer;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
Assert(sLinebreak = AnsiString(#13#10));
i := 1;
while i <= length(s) do
begin
if (s[i] = #10) and (MidStr(s, i - 1, 1) <> #13) then
begin
insert(#13, s, i);
inc(i, 2);
end
else
inc(i);
end;
{$ENDIF}
Result := s;
end;
function IsWriteProp(Info: PPropInfo): boolean;
begin
Result := Assigned(Info) and (Info^.SetProc <> nil);
end;
function ResourceStringGettext(msgid: MsgIdString)
: TranslatedUnicodeString;
var
i: Integer;
begin
if (msgid = '') or (ResourceStringDomainListCS = nil) then
begin
// This only happens during very complicated program startups that fail,
// or when Msgid=''
Result := msgid;
exit;
end;
ResourceStringDomainListCS.BeginRead;
try
for i := 0 to ResourceStringDomainList.Count - 1 do
begin
Result := dgettext(ResourceStringDomainList.Strings[i], msgid);
if Result <> msgid then
break;
end;
finally
ResourceStringDomainListCS.EndRead;
end;
end;
function ComponentGettext(msgid: MsgIdString;
Instance: TGnuGettextInstance = nil): TranslatedUnicodeString;
var
i: Integer;
begin
if (msgid = '') or (ComponentDomainListCS = nil) then
begin
// This only happens during very complicated program startups that fail,
// or when Msgid=''
Result := msgid;
exit;
end;
ComponentDomainListCS.BeginRead;
try
for i := 0 to ComponentDomainList.Count - 1 do
begin
if Assigned(Instance) then
Result := Instance.dgettext
(ComponentDomainList.Strings[i], msgid)
else
Result := dgettext(ComponentDomainList.Strings[i], msgid);
if Result <> msgid then
break;
end;
finally
ComponentDomainListCS.EndRead;
end;
end;
function gettext(const szMsgId: MsgIdString): TranslatedUnicodeString;
begin
Result := DefaultInstance.gettext(szMsgId);
end;
function gettext_NoExtract(const szMsgId: MsgIdString)
: TranslatedUnicodeString;
begin
// This one is very useful for translating text in variables.
// This can sometimes be necessary, and by using this function,
// the source code scanner will not trigger warnings.
Result := gettext(szMsgId);
end;
function gettext_NoOp(const szMsgId: MsgIdString)
: TranslatedUnicodeString;
begin
// *** With this function Strings can be added to the po-file without beeing
// ResourceStrings (dxgettext will add the string and this function will
// return it without a change)
// see gettext manual
// 4.7 - Special Cases of Translatable Strings
// http://www.gnu.org/software/hello/manual/gettext/Special-cases.html#Special-cases
Result := DefaultInstance.gettext_NoOp(szMsgId);
end;
{ *------------------------------------------------------------------------------
This is the main translation procedure used in programs. It takes a parameter,
looks it up in the translation dictionary, and returns the translation.
If no translation is found, the parameter is returned.
@param szMsgId The text, that should be displayed if no translation is found.
------------------------------------------------------------------------------- }
function _(const szMsgId: MsgIdString): TranslatedUnicodeString;
begin
Result := DefaultInstance.gettext(szMsgId);
end;
{ *------------------------------------------------------------------------------
Translates a text, using a specified translation domain.
If no translation is found, the parameter is returned.
@param szDomain Which translation domain that should be searched for a translation.
@param szMsgId The text, that should be displayed if no translation is found.
------------------------------------------------------------------------------- }
function dgettext(const szDomain: DomainString;
const szMsgId: MsgIdString): TranslatedUnicodeString;
begin
Result := DefaultInstance.dgettext(szDomain, szMsgId);
end;
function dgettext_NoExtract(const szDomain: DomainString;
const szMsgId: MsgIdString): TranslatedUnicodeString;
begin
// This one is very useful for translating text in variables.
// This can sometimes be necessary, and by using this function,
// the source code scanner will not trigger warnings.
Result := dgettext(szDomain, szMsgId);
end;
function dngettext(const szDomain: DomainString;
const singular, plural: MsgIdString; Number: longint)
: TranslatedUnicodeString;
begin
Result := DefaultInstance.dngettext(szDomain, singular,
plural, Number);
end;
function ngettext(const singular, plural: MsgIdString; Number: longint)
: TranslatedUnicodeString;
begin
Result := DefaultInstance.ngettext(singular, plural, Number);
end;
function ngettext_NoExtract(const singular, plural: MsgIdString;
Number: longint): TranslatedUnicodeString;
begin
// This one is very useful for translating text in variables.
// This can sometimes be necessary, and by using this function,
// the source code scanner will not trigger warnings.
Result := ngettext(singular, plural, Number);
end;
procedure textdomain(const szDomain: DomainString);
begin
DefaultInstance.textdomain(szDomain);
end;
procedure SetGettextEnabled(enabled: boolean);
begin
DefaultInstance.enabled := enabled;
end;
function getcurrenttextdomain: DomainString;
begin
Result := DefaultInstance.getcurrenttextdomain;
end;
procedure bindtextdomain(const szDomain: DomainString;
const szDirectory: FilenameString);
begin
DefaultInstance.bindtextdomain(szDomain, szDirectory);
end;
procedure TP_Ignore(AnObject: TObject; const name: FilenameString);
begin
DefaultInstance.TP_Ignore(AnObject, name);
end;
procedure TP_GlobalIgnoreClass(IgnClass: TClass);
begin
DefaultInstance.TP_GlobalIgnoreClass(IgnClass);
end;
function TP_TryGlobalIgnoreClass(IgnClass: TClass): boolean;
begin
Result := DefaultInstance.TP_TryGlobalIgnoreClass(IgnClass);
end;
procedure TP_IgnoreClass(IgnClass: TClass);
begin
DefaultInstance.TP_IgnoreClass(IgnClass);
end;
procedure TP_IgnoreClassProperty(IgnClass: TClass;
const propertyname: ComponentNameString);
begin
DefaultInstance.TP_IgnoreClassProperty(IgnClass, propertyname);
end;
procedure TP_GlobalIgnoreClassProperty(IgnClass: TClass;
const propertyname: ComponentNameString);
begin
DefaultInstance.TP_GlobalIgnoreClassProperty(IgnClass, propertyname);
end;
procedure TP_GlobalHandleClass(HClass: TClass; Handler: TTranslator);
begin
DefaultInstance.TP_GlobalHandleClass(HClass, Handler);
end;
procedure TP_Remember(AnObject: TObject; PropName: ComponentNameString;
OldValue: TranslatedUnicodeString);
begin
DefaultInstance.TP_Remember(AnObject, PropName, OldValue);
end;
procedure TranslateComponent(AnObject: TComponent;
const textdomain: DomainString = '');
begin
DefaultInstance.TranslateComponent(AnObject, textdomain);
end;
procedure RetranslateComponent(AnObject: TComponent;
const textdomain: DomainString = '');
begin
DefaultInstance.RetranslateComponent(AnObject, textdomain);
end;
{$IFDEF MSWINDOWS}
// These constants are only used in Windows 95
// Thanks to Frank Andreas de Groot for this table
const
IDAfrikaans = $0436;
IDAlbanian = $041C;
IDArabicAlgeria = $1401;
IDArabicBahrain = $3C01;
IDArabicEgypt = $0C01;
IDArabicIraq = $0801;
IDArabicJordan = $2C01;
IDArabicKuwait = $3401;
IDArabicLebanon = $3001;
IDArabicLibya = $1001;
IDArabicMorocco = $1801;
IDArabicOman = $2001;
IDArabicQatar = $4001;
IDArabic = $0401;
IDArabicSyria = $2801;
IDArabicTunisia = $1C01;
IDArabicUAE = $3801;
IDArabicYemen = $2401;
IDArmenian = $042B;
IDAssamese = $044D;
IDAzeriCyrillic = $082C;
IDAzeriLatin = $042C;
IDBasque = $042D;
IDByelorussian = $0423;
IDBengali = $0445;
IDBulgarian = $0402;
IDBurmese = $0455;
IDCatalan = $0403;
IDChineseHongKong = $0C04;
IDChineseMacao = $1404;
IDSimplifiedChinese = $0804;
IDChineseSingapore = $1004;
IDTraditionalChinese = $0404;
IDCroatian = $041A;
IDCzech = $0405;
IDDanish = $0406;
IDBelgianDutch = $0813;
IDDutch = $0413;
IDEnglishAUS = $0C09;
IDEnglishBelize = $2809;
IDEnglishCanadian = $1009;
IDEnglishCaribbean = $2409;
IDEnglishIreland = $1809;
IDEnglishJamaica = $2009;
IDEnglishNewZealand = $1409;
IDEnglishPhilippines = $3409;