source: tools/PoConv/UFormMain.pas

Last change on this file was 390, checked in by chronos, 3 years ago
  • Modified: Improved PoConv to convert txt files to po and back.
File size: 6.4 KB
Line 
1unit UFormMain;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
9
10type
11
12 { TFormMain }
13
14 TFormMain = class(TForm)
15 ButtonTxtToPo: TButton;
16 ButtonPoToTxt: TButton;
17 procedure ButtonPoToTxtClick(Sender: TObject);
18 procedure ButtonTxtToPoClick(Sender: TObject);
19 private
20 procedure TxtToPo(SourceFileName, DestFileName: string);
21 procedure PoToTxt(SourceFileName, DestFileName: string);
22 function Escape(Text: string): string;
23 function Unescape(Text: string): string;
24 function Unquote(Text: string): string;
25 procedure EmitPo(Lines: TStrings; Name, Value: string);
26 procedure EmitTxt(Lines: TStrings; Name, Value: string);
27 public
28
29 end;
30
31var
32 FormMain: TFormMain;
33
34implementation
35
36{$R *.lfm}
37
38{ TFormMain }
39
40procedure TFormMain.ButtonTxtToPoClick(Sender: TObject);
41var
42 BaseDir: string;
43begin
44 BaseDir := '..' + DirectorySeparator + '..' + DirectorySeparator + 'trunk' + DirectorySeparator;
45 TxtToPo(BaseDir + 'Language.txt', 'Language.po');
46 TxtToPo(BaseDir + 'Language2.txt', 'Language2.po');
47 TxtToPo(BaseDir + 'Help' + DirectorySeparator + 'help.txt', 'help.po');
48end;
49
50procedure TFormMain.ButtonPoToTxtClick(Sender: TObject);
51begin
52 PoToTxt('Language.po', 'Language.txt');
53 PoToTxt('Language2.po', 'Language2.txt');
54 PoToTxt('help.po', 'help.txt');
55end;
56
57procedure TFormMain.TxtToPo(SourceFileName, DestFileName: string);
58var
59 SourceLines: TStringList;
60 DestLines: TStringList;
61 I: Integer;
62 Line: string;
63 Name: string;
64 Value: string;
65 Index: Integer;
66 NameIndex: Integer;
67begin
68 SourceLines := TStringList.Create;
69 DestLines := TStringList.Create;
70 with DestLines do begin
71 Add('msgid ""');
72 Add('msgstr ""');
73 Add('"Content-Type: text/plain; charset=UTF-8\n"');
74 Add('"X-Generator: PoConv\n"');
75 Add('');
76 end;
77
78 SourceLines.LoadFromFile(SourceFileName);
79 Name := '';
80 NameIndex := 0;
81 Value := '';
82 I := 0;
83 while I < SourceLines.Count do begin
84 Line := SourceLines[I];
85 if (Length(Line) > 0) and (Line[1] = '''') then begin
86 if Name <> '' then begin
87 EmitPo(DestLines, Name, Value);
88 Name := '';
89 end;
90 DestLines.Add('#' + Copy(Line, 2, Length(Line)));
91 end else
92 if (Length(Line) > 0) and (Line[1] = '#') and (NameIndex = 0) then begin
93 if Name <> '' then begin
94 EmitPo(DestLines, Name, Value);
95 Name := '';
96 end;
97 Index := Pos(' ', Line);
98 if Index > 0 then begin
99 Name := Copy(Line, 2, Index - 2);
100 Value := Copy(Line, Index + 1, Length(Line));
101 end else begin
102 Name := Copy(Line, 2, Length(Line));
103 Value := '';
104 end;
105 end else begin
106 Value := Value + LineEnding + Line;
107 end;
108 Inc(I);
109 end;
110 if Name <> '' then begin
111 EmitPo(DestLines, Name, Value);
112 Name := '';
113 end;
114 DestLines.SaveToFile(DestFileName);
115 FreeAndNil(SourceLines);
116 FreeAndNil(DestLines);
117end;
118
119procedure TFormMain.PoToTxt(SourceFileName, DestFileName: string);
120var
121 SourceLines: TStringList;
122 DestLines: TStringList;
123 I: Integer;
124 Line: string;
125 Index: Integer;
126 Name: string;
127 Value: string;
128 Cmd: string;
129begin
130 SourceLines := TStringList.Create;
131 DestLines := TStringList.Create;
132 SourceLines.LoadFromFile(SourceFileName);
133 I := 0;
134 Value := '';
135 Name := '';
136 while I < SourceLines.Count do begin
137 Line := Trim(SourceLines[I]);
138 Line := StringReplace(Line, '\n', '', [rfReplaceAll]);
139 if (Length(Line) > 0) and (Line[1] = '#') then begin
140 if Name <> '' then begin
141 EmitTxt(DestLines, Name, Value);
142 Name := '';
143 end;
144 DestLines.Add('''' + Copy(Line, 2, Length(Line)));
145 end else begin
146 if (Length(Line) > 0) and (Line[1] = '"') and (Line[Length(Line)] = '"') then begin
147 Value := Value + LineEnding + Unquote(Line);
148 end else begin
149 Index := Pos(' ', Line);
150 if Index > 0 then begin
151 Cmd := Copy(Line, 1, Index - 1);
152 if Cmd = 'msgid' then begin
153 if Name <> '' then begin
154 EmitTxt(DestLines, Name, Value);
155 Name := '';
156 end;
157 Name := Unquote(Unescape(Copy(Line, Index + 1, Length(Line))));
158 end else
159 if Cmd = 'msgstr' then begin
160 Value := Unquote(Copy(Line, Index + 1, Length(Line)));
161 end;
162 end;
163 end;
164 end;
165 Inc(I);
166 end;
167 if Name <> '' then begin
168 EmitTxt(DestLines, Name, Value);
169 Name := '';
170 end;
171 DestLines.SaveToFile(DestFileName);
172 FreeAndNil(SourceLines);
173 FreeAndNil(DestLines);
174end;
175
176function TFormMain.Escape(Text: string): string;
177begin
178 Result := Text;
179 Result := StringReplace(Result, '\', '\\', [rfReplaceAll]);
180 Result := StringReplace(Result, '"', '\"', [rfReplaceAll]);
181end;
182
183function TFormMain.Unescape(Text: string): string;
184begin
185 Result := Text;
186 Result := StringReplace(Result, '\\', '\', [rfReplaceAll]);
187 Result := StringReplace(Result, '\"', '"', [rfReplaceAll]);
188end;
189
190function TFormMain.Unquote(Text: string): string;
191begin
192 Result := Text;
193 if Length(Text) >= 2 then begin
194 if (Text[1] = '"') and (Text[Length(Text)] = '"') then
195 Result := Copy(Text, 2, Length(Text) - 2);
196 end;
197end;
198
199procedure TFormMain.EmitPo(Lines: TStrings; Name, Value: string);
200var
201 MsgStr: string;
202 Index: Integer;
203 I: Integer;
204 Stop: Boolean;
205begin
206 Lines.Add('msgid "' + Name + '"');
207 I := 0;
208 Stop := False;
209 while not Stop do begin
210 Index := Pos(LineEnding, Value);
211 if Index > 0 then begin
212 MsgStr := '"' + Escape(Copy(Value, 1, Index - 1)) + '\n"';
213 Value := Copy(Value, Index + 1, Length(Value));
214 end else begin
215 MsgStr := '"' + Escape(Value) + '"';
216 Value := '';
217 Stop := True;
218 end;
219 if I = 0 then MsgStr := 'msgstr ' + MsgStr;
220 Lines.Add(MsgStr);
221 Inc(I);
222 end;
223 if I = 0 then Lines.Add('msgstr ""');
224 Lines.Add('');
225end;
226
227procedure TFormMain.EmitTxt(Lines: TStrings; Name, Value: string);
228var
229 MsgStr: string;
230 Index: Integer;
231 I: Integer;
232 Stop: Boolean;
233begin
234 I := 0;
235 Stop := False;
236 while not Stop do begin
237 Index := Pos(LineEnding, Value);
238 if Index > 0 then begin
239 MsgStr := Unescape(Copy(Value, 1, Index - 1));
240 Value := Copy(Value, Index + 1, Length(Value));
241 end else begin
242 MsgStr := Unescape(Value);
243 Stop := True;
244 end;
245 if I = 0 then begin
246 if Length(MsgStr) > 0 then MsgStr := ' ' + MsgStr;
247 MsgStr := '#' + Name + MsgStr;
248 end;
249 Lines.Add(MsgStr);
250 Inc(I);
251 end;
252 if I = 0 then begin
253 Lines.Add('#' + Name);
254 end;
255end;
256
257end.
258
Note: See TracBrowser for help on using the repository browser.