K
K
Keenest2013-03-07 15:28:06
Delphi
Keenest, 2013-03-07 15:28:06

Delphi XE3, unicode & utf-8?

Good afternoon.
For the third day I've been struggling with the following task:
There is a template - an html-file in utf-8 encoding and a form on which, conditionally, there are several TEdit components.
All you need to do is to replace the string "{{ edit1 }}" in the template with the content of Edit.Text and save the resulting file back to utf-8.
It would seem that the task of replacing a substring in a string is, but no: a problem with encodings. No matter how hard I try, “non-standard” characters like ü and ö are lost .
Tried many ways:

// template: TStringList, в котором хранится содержимое файла template.html
        for i:=0 to template.Count-1 do
            begin
                 s:=template.Strings[i];
                 s:=StringReplace(s,'{{ edit1 }}',Edit1.Text,[rfReplaceAll,rfIgnoreCase]);
                 s:=StringReplace(s,'{{ edit2 }}',Edit2.Text,[rfReplaceAll,rfIgnoreCase]);
                 WS:=s; // пробовал и так
                 WriteFile(HF,PChar(WS)^,Length(WS)*2,D, nil);
                 //writeln(f,s); // и так
            end;

either umlauts are lost from the template or from edits ( ü becomes u ).
I also tried this option - manually overtake encodings:
var
   s,s8:UTF8String;
   s2:string;
   fi,fo:textfile;
   l:integer;
begin
     AssignFile(fi,'template8.html');
     AssignFile(fo,'result.html');
     Reset(fi);
     Rewrite(fo);
     while not EoF(fi) do
           begin
                readln(fi,s);
                s2:=UTF8ToUnicodeString(s);
                l:=length(s2);
                SetLength(s8,l*SizeOf(Char)+1);
                UnicodeToUtf8(PAnsiChar(s8),length(s8),PWideChar(s2),l);
                writeln(fo,s8);
           end;
     CloseFile(fi);
     CloseFile(fo)
end;

But it doesn't work.
Maybe someone faced similar? In which direction to dig?
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Hint, 2013-03-07
@Keenest

There shouldn't be a problem. Throw off the finished code of the minimum size, where your problem manifests itself.

V
Vitaly Zheltyakov, 2013-03-07
@VitaZheltyakov

Time passes, but the problems are the same...
The problem is in the incorrect operation of the component code. The problem with encodings is observed in a number of standard components of the Delphi environment after version 7. There is only one solution (of which I know) - to replace the problematic components with components where there will be no problem with encodings. You can also roll back the project to version 7.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question