-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.pas
153 lines (124 loc) · 3.89 KB
/
config.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
{
/***************************************************************************
config.pas
------------
***************************************************************************/
*****************************************************************************
This file is part of the Lazarus packages by Andreas Jakobsche
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
unit Config;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
TTranslateEvent = procedure (Sender: TObject; Lang: string) of object;
TSetPropertyEvent = procedure(Sender: TObject; PropName, PropValue: string) of object;
{ TKarinaConfiguration }
TKarinaConfiguration = class(TComponent)
private
FList: TStringList;
FOnSetProperty: TSetPropertyEvent;
FOnTranslate: TTranslateEvent;
function GetConfigFileName: string;
function GetList: TStringList;
private
HasChanged: Boolean;
function GetLanguage: string;
procedure SetLanguage(AValue: string);
procedure Translate(Lang: string);
property List: TStringList read GetList;
protected
procedure SetProperty(PropName, AValue: string);
procedure GetProperty(PropName: string; var AValue: string);
public
constructor Create(AnOwner: TComponent); override;
destructor Destroy; override;
property ConfigFileName: string read GetConfigFileName;
published
property Language: string read GetLanguage write SetLanguage;
property OnSetProperty: TSetPropertyEvent read FOnSetProperty write FOnSetProperty;
property OnTranslate: TTranslateEvent read FOnTranslate write FOnTranslate;
end;
var
KarinaConfig: TKarinaConfiguration;
implementation
uses Forms, LCLTranslator;
{ TKarinaConfiguration }
function TKarinaConfiguration.GetConfigFileName: string;
begin
Result := GetAppConfigFile(False)
end;
function TKarinaConfiguration.GetList: TStringList;
begin
if not Assigned(FList) then begin
FList := TStringList.Create;
try
FList.LoadFromFile(ConfigFileName);
except
on Exception do;
end;
end;
Result := FList
end;
function TKarinaConfiguration.GetLanguage: string;
begin
GetProperty('language', Result);
end;
procedure TKarinaConfiguration.SetLanguage(AValue: string);
begin
if AValue <> Language then begin
SetProperty('language', AValue);
Translate(AValue)
end;
end;
procedure TKarinaConfiguration.Translate(Lang: string);
begin
SetDefaultLang(Lang, '', True);
if Assigned(FOnTranslate) then FOnTranslate(Self, Lang)
end;
procedure TKarinaConfiguration.SetProperty(PropName, AValue: string);
begin
if List.Values[PropName] <> AValue then begin
List.Values[PropName] := AValue;
HasChanged := True;
if Assigned(FOnSetProperty) then FOnSetProperty(Self, PropName, AValue)
end;
end;
procedure TKarinaConfiguration.GetProperty(PropName: string; var AValue: string
);
begin
AValue := List.Values[PropName];
end;
constructor TKarinaConfiguration.Create(AnOwner: TComponent);
var
i: Integer;
Translated: Boolean;
begin
inherited Create(AnOwner);
{ Spracheinstellung in der Befehlszeile finden }
i := 1; Translated := False;
while i <= Application.ParamCount do
if (Application.Params[i] = '-l') or (Application.Params[i] = '--lang') then begin
Language := Application.Params[i + 1];
Translated := True;
Break;
end;
{ Übersetzung in die Sprache aus der Konfigurationsdatei durchführen }
if not Translated then Translate(Language)
end;
destructor TKarinaConfiguration.Destroy;
begin
if HasChanged then begin
ForceDirectories(ExtractFilePath(ConfigFileName));
List.SaveToFile(ConfigFileName);
end;
FList.Free;
inherited Destroy;
end;
initialization
KarinaConfig := TKarinaConfiguration.Create(Application);
end.