1 | unit ULocalization;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | SysUtils, Classes, Forms, IniFiles, Dialogs;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TLanguage = class
|
---|
10 | Items: TStringList;
|
---|
11 | Name: string;
|
---|
12 | end;
|
---|
13 |
|
---|
14 | TLocalization = class(TDataModule)
|
---|
15 | procedure DataModuleCreate(Sender: TObject);
|
---|
16 | public
|
---|
17 | ActiveLanguage: Integer;
|
---|
18 | Languages: array of TLanguage;
|
---|
19 | procedure Add(New: string);
|
---|
20 | function Item(NameIndex: string): string;
|
---|
21 | procedure Activate(New: Integer);
|
---|
22 | procedure LoadLanguages;
|
---|
23 | end;
|
---|
24 |
|
---|
25 | var
|
---|
26 | Localization: TLocalization;
|
---|
27 |
|
---|
28 | implementation
|
---|
29 |
|
---|
30 | {$R *.dfm}
|
---|
31 |
|
---|
32 | { TLokalizace }
|
---|
33 |
|
---|
34 | procedure TLocalization.Activate(New: Integer);
|
---|
35 | var
|
---|
36 | I: Integer;
|
---|
37 | begin
|
---|
38 | ActiveLanguage := New;
|
---|
39 | if (ActiveLanguage < 0) or (ActiveLanguage >= Length(Languages)) then ActiveLanguage := 0;
|
---|
40 | with Application do
|
---|
41 | for I := 0 to ComponentCount-1 do
|
---|
42 | if Components[I] is TForm then
|
---|
43 | with (Components[I] as TForm) do
|
---|
44 | if Visible then OnShow(Self);
|
---|
45 | end;
|
---|
46 |
|
---|
47 | procedure TLocalization.LoadLanguages;
|
---|
48 | var
|
---|
49 | LanguageFile: TSearchRec;
|
---|
50 | Path: string;
|
---|
51 | I: Integer;
|
---|
52 | begin
|
---|
53 | Path := ExtractFileDir(Application.ExeName)+'\'+'*.lng';
|
---|
54 | if FindFirst(Path, $2f, LanguageFile)=0 then begin
|
---|
55 | repeat
|
---|
56 | Add(ExtractFileName(LanguageFile.Name));
|
---|
57 | with Languages[High(Languages)] do
|
---|
58 | with TIniFile.Create(ExtractFileDir(Application.ExeName)+'\'+LanguageFile.Name) do
|
---|
59 | try
|
---|
60 | Name := ReadString('Info', 'Language', 'None');
|
---|
61 | Items := TStringList.Create;
|
---|
62 | ReadSectionValues('Main',Items);
|
---|
63 | finally
|
---|
64 | Free;
|
---|
65 | end;
|
---|
66 | until FindNext(LanguageFile)<>0;
|
---|
67 | FindClose(LanguageFile);
|
---|
68 | end;
|
---|
69 | if Length(Languages) = 0 then raise Exception.Create('Nebyl nalezen žádný jazykový soubor!');
|
---|
70 | end;
|
---|
71 |
|
---|
72 | function TLocalization.Item(NameIndex: string): string;
|
---|
73 | var
|
---|
74 | Index: Integer;
|
---|
75 | begin
|
---|
76 | if (ActiveLanguage < Length(Languages)) and (ActiveLanguage >= 0) then begin
|
---|
77 | with Languages[ActiveLanguage].Items do begin
|
---|
78 | Index := IndexOfName(NameIndex);
|
---|
79 | if Index <> -1 then Result := Values[NameIndex] else Result := '#' + NameIndex;
|
---|
80 | end;
|
---|
81 | end else Result := '#' + NameIndex;
|
---|
82 | end;
|
---|
83 |
|
---|
84 | procedure TLocalization.Add(New: string);
|
---|
85 | begin
|
---|
86 | SetLength(Languages, Length(Languages)+1);
|
---|
87 | Languages[High(Languages)] := TLanguage.Create;
|
---|
88 | Languages[High(Languages)].Name := Name;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | procedure TLocalization.DataModuleCreate(Sender: TObject);
|
---|
92 | begin
|
---|
93 | LoadLanguages;
|
---|
94 | ActiveLanguage := 0;
|
---|
95 | end;
|
---|
96 |
|
---|
97 | end.
|
---|