Debian Codegraph @

src/deprecated_units/castlelocalizationfileloader.pas

raw
1 {
2 Copyright 2018 Benedikt Magnus.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Contains the standard file loader for the CastleLocalization unit. }
17 unit CastleLocalizationFileLoader deprecated 'use CastleLocalizationGetText instead, it offers more features for now (GenerateGetTextPo, TranslateAllDesigns, TCastleComponent.TranslateProperties)';
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses
24 Classes, SysUtils, Generics.Collections,
25 {$warnings off}
26 DOM, XMLRead, fpjson, fpjsonrtti, CSVDocument, GetText,
27 {$warnings on}
28 CastleUtils, CastleClassUtils,
29 CastleXmlUtils;
30
31 type
32 { Represents a single translated string in the language JSON file. }
33 TFileLoaderJSONEntry = class(TCollectionItem)
34 private
35 FKey: String;
36 FValue: String;
37 published //Properties are case-sensitive in JSON!
38 property key: String read FKey write FKey;
39 property value: String read FValue write FValue;
40 end;
41
42 { Called by CastleLocalization to load all standard file loader.
43 See Castle examples or documentation for detailed description of the file formats. }
44 procedure ActivateAllFileLoader;
45
46 implementation
47
48 {$warnings off} // using deprecated unit here
49 uses CastleLocalization,
50 { For TCastleMOFile }
51 CastleLocalizationGetText;
52 {$warnings on}
53
54 { TGenericCollection -------------------------------------------------------- }
55
56 type
57 { A generic version of TCollection.
58 Main usage is preventing code redundancy when working with JSON serialisation. }
59 generic TGenericCollection<T> = class(TCollection)
60 private
61 function GetItems(AIndex: Integer): T;
62 procedure SetItems(AIndex: Integer; AValue: T);
63 public
64 constructor Create;
65 function Add: T;
66 property Items[AIndex: Integer]: T read GetItems write SetItems; default;
67 end;
68
69 function TGenericCollection.GetItems(AIndex: Integer): T;
70 begin
71 Result := T(inherited Items[AIndex]);
72 end;
73
74 procedure TGenericCollection.SetItems(AIndex: Integer; AValue: T);
75 begin
76 Items[AIndex].Assign(AValue);
77 end;
78
79 constructor TGenericCollection.Create;
80 begin
81 inherited Create(T);
82 end;
83
84 function TGenericCollection.Add: T;
85 begin
86 Result := T(inherited Add);
87 end;
88
89 { TFileLoaderJSONList -------------------------------------------------------- }
90
91 type
92 { Represents the full language JSON file containing all translated strings. }
93 TFileLoaderJSONList = {$ifdef FPC}specialize{$endif} TGenericCollection<TFileLoaderJSONEntry>;
94
95 { LoadLanguageFiles ---------------------------------------------------------- }
96
97 procedure LoadLanguageFileXML(const AFileStream: TStream; const ALanguageDictionary: TLanguageDictionary);
98 var
99 LanguageXML: TXMLDocument;
100 I: TXMLElementIterator;
101 begin
102 ReadXMLFile(LanguageXML, AFileStream);
103
104 try
105 Check(LanguageXML.DocumentElement.TagName = 'strings', 'Root node of local/index.xml must be <strings>');
106
107 I := LanguageXML.DocumentElement.ChildrenIterator;
108 try
109 while I.GetNext do
110 begin
111 Check(I.Current.TagName = 'string', 'Each child of local/index.xml root node must be the <string> element');
112
113 ALanguageDictionary.AddOrSetValue(I.Current.AttributeString('key'), I.Current.AttributeString('value'));
114 end;
115 finally
116 I.Free;
117 end;
118 finally
119 LanguageXML.Free;
120 end;
121 end;
122
123 procedure LoadLanguageFileJSONObj(const AFileStream: TStream; const ALanguageDictionary: TLanguageDictionary);
124 var
125 StringStream: TStringStream;
126 DeStreamer: TJSONDeStreamer;
127 LanguageJSON: TFileLoaderJSONList;
128 i: Integer;
129 begin
130 StringStream := TStringStream.Create('');
131 try
132 StringStream.CopyFrom(AFileStream, AFileStream.Size);
133
134 DeStreamer := TJSONDeStreamer.Create(nil);
135 try
136 LanguageJSON := TFileLoaderJSONList.Create;
137 try
138 DeStreamer.JSONToObject(StringStream.DataString, LanguageJSON);
139
140 //Save RAM:
141 FreeAndNil(StringStream);
142 FreeAndNil(DeStreamer);
143
144 for i := 0 to LanguageJSON.Count - 1 do
145 ALanguageDictionary.AddOrSetValue(LanguageJSON[i].Key, LanguageJSON[i].Value);
146 finally
147 LanguageJSON.Free;
148 end;
149 finally
150 DeStreamer.Free;
151 end;
152 finally
153 StringStream.Free;
154 end;
155 end;
156
157 procedure LoadLanguageFileJSON(const AFileStream: TStream; const ALanguageDictionary: TLanguageDictionary);
158 var
159 StringStream: TStringStream;
160 Data: TJSONData;
161 i: Integer;
162 Key: String;
163 begin
164 StringStream := TStringStream.Create('');
165 try
166 StringStream.CopyFrom(AFileStream, AFileStream.Size);
167
168 Data := GetJSON(StringStream.DataString);
169 try
170 FreeAndNil(StringStream); //Save RAM.
171
172 for i := 0 to Data.Count - 1 do
173 begin
174 Key := TJSONObject(Data).Names[i];
175 ALanguageDictionary.AddOrSetValue(Key, Data.FindPath(Key).AsString);
176 end;
177 finally
178 Data.Free;
179 end;
180 finally
181 StringStream.Free;
182 end;
183 end;
184
185 procedure LoadLanguageFileMO(const AFileStream: TStream; const ALanguageDictionary: TLanguageDictionary);
186 var
187 LanguageMO: TCastleMOFile;
188 i: Integer;
189 begin
190 LanguageMO := TCastleMOFile.Create(AFileStream);
191 try
192 for i := 0 to LanguageMO.Count - 1 do
193 ALanguageDictionary.AddOrSetValue(LanguageMO.Keys[i], LanguageMO.Values[i]);
194 finally
195 LanguageMO.Free;
196 end;
197 end;
198
199 procedure LoadLanguageFileCSV(const AFileStream: TStream; const ALanguageDictionary: TLanguageDictionary);
200 var
201 Parser: TCSVParser;
202 Key: String;
203 begin
204 Parser := TCSVParser.Create;
205 try
206 Parser.SetSource(AFileStream);
207 Key := '';
208 while Parser.ParseNextCell do
209 begin
210 if Parser.CurrentCol mod 2 = 0 then
211 Key := Parser.CurrentCellText
212 else
213 ALanguageDictionary.AddOrSetValue(Key, Parser.CurrentCellText);
214 end;
215 finally
216 Parser.Free;
217 end;
218 end;
219
220 procedure ActivateAllFileLoader;
221 begin
222 Localization.FileLoader.Add('.xml', @LoadLanguageFileXML);
223 Localization.FileLoader.Add('.json', @LoadLanguageFileJSON);
224 Localization.FileLoader.Add('.jsonobj', @LoadLanguageFileJSONObj);
225 Localization.FileLoader.Add('.mo', @LoadLanguageFileMO);
226 Localization.FileLoader.Add('.csv', @LoadLanguageFileCSV);
227 end;
228
229 end.