Changeset 390 for tools/PoConv/UFormMain.pas
- Timestamp:
- Jun 1, 2021, 1:13:28 AM (3 years ago)
- Location:
- tools/PoConv
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/PoConv
-
Property svn:ignore
set to
lib
PoConv.lps
PoConv.res
PoConv
-
Property svn:ignore
set to
-
tools/PoConv/UFormMain.pas
r389 r390 14 14 TFormMain = class(TForm) 15 15 ButtonTxtToPo: TButton; 16 ButtonPoToTxt: TButton; 17 procedure ButtonPoToTxtClick(Sender: TObject); 16 18 procedure ButtonTxtToPoClick(Sender: TObject); 17 19 private 18 20 procedure TxtToPo(SourceFileName, DestFileName: string); 21 procedure PoToTxt(SourceFileName, DestFileName: string); 19 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); 20 27 public 21 28 … … 39 46 TxtToPo(BaseDir + 'Language2.txt', 'Language2.po'); 40 47 TxtToPo(BaseDir + 'Help' + DirectorySeparator + 'help.txt', 'help.po'); 48 end; 49 50 procedure TFormMain.ButtonPoToTxtClick(Sender: TObject); 51 begin 52 PoToTxt('Language.po', 'Language.txt'); 53 PoToTxt('Language2.po', 'Language2.txt'); 54 PoToTxt('help.po', 'help.txt'); 41 55 end; 42 56 … … 65 79 Name := ''; 66 80 NameIndex := 0; 81 Value := ''; 67 82 I := 0; 68 83 while I < SourceLines.Count do begin 69 Line := Trim(SourceLines[I]);84 Line := SourceLines[I]; 70 85 if (Length(Line) > 0) and (Line[1] = '''') then begin 86 if Name <> '' then begin 87 EmitPo(DestLines, Name, Value); 88 Name := ''; 89 end; 71 90 DestLines.Add('#' + Copy(Line, 2, Length(Line))); 72 91 end else 73 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; 74 97 Index := Pos(' ', Line); 75 98 if Index > 0 then begin 76 99 Name := Copy(Line, 2, Index - 2); 77 100 Value := Copy(Line, Index + 1, Length(Line)); 78 NameIndex := 0;79 DestLines.Add('msgid "' + Name + '"');80 DestLines.Add('msgstr "' + Escape(Value) + '"');81 DestLines.Add('');82 101 end else begin 83 102 Name := Copy(Line, 2, Length(Line)); 84 103 Value := ''; 85 NameIndex := 1; 86 end; 87 end else begin 88 if (NameIndex > 0) and (Line <> '') then begin 89 DestLines.Add('msgid "' + Name + '_' + IntToStr(NameIndex) + '"'); 90 DestLines.Add('msgstr "' + Escape(Line) + '"'); 91 DestLines.Add(''); 92 Inc(NameIndex); 93 end else 94 if Line = '' then begin 95 NameIndex := 0; 96 end; 97 end; 98 Inc(I); 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 := ''; 99 113 end; 100 114 DestLines.SaveToFile(DestFileName); … … 103 117 end; 104 118 119 procedure TFormMain.PoToTxt(SourceFileName, DestFileName: string); 120 var 121 SourceLines: TStringList; 122 DestLines: TStringList; 123 I: Integer; 124 Line: string; 125 Index: Integer; 126 Name: string; 127 Value: string; 128 Cmd: string; 129 begin 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); 174 end; 175 105 176 function TFormMain.Escape(Text: string): string; 106 177 begin 107 Result := StringReplace(Text, '\', '\\', [rfReplaceAll]); 178 Result := Text; 179 Result := StringReplace(Result, '\', '\\', [rfReplaceAll]); 108 180 Result := StringReplace(Result, '"', '\"', [rfReplaceAll]); 109 181 end; 110 182 183 function TFormMain.Unescape(Text: string): string; 184 begin 185 Result := Text; 186 Result := StringReplace(Result, '\\', '\', [rfReplaceAll]); 187 Result := StringReplace(Result, '\"', '"', [rfReplaceAll]); 188 end; 189 190 function TFormMain.Unquote(Text: string): string; 191 begin 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; 197 end; 198 199 procedure TFormMain.EmitPo(Lines: TStrings; Name, Value: string); 200 var 201 MsgStr: string; 202 Index: Integer; 203 I: Integer; 204 Stop: Boolean; 205 begin 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(''); 225 end; 226 227 procedure TFormMain.EmitTxt(Lines: TStrings; Name, Value: string); 228 var 229 MsgStr: string; 230 Index: Integer; 231 I: Integer; 232 Stop: Boolean; 233 begin 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; 255 end; 256 111 257 end. 112 258
Note:
See TracChangeset
for help on using the changeset viewer.