Page 1 of 1

UTF8

Posted: Fri Jun 18, 2021 3:31 pm
by standay
What is the simplest way to load a file with specific encoding? On most editor components I use I can do it this way:

Code: Select all

memo.Lines.LoadFromFile('test.txt',TEncoding.UTF8);
What I did with the SRichView was this:

Code: Select all

  ms := TStringStream.Create('', TEncoding.UTF8);
  ms.LoadFromFile(FileName);
  ms.Position := 0;
  SRichViewEdit1.ActiveEditor.InsertText( ms.DataString );
  ms.Free;
That works but is there some easier way I'm missing?

Also, is there a way to make opening and saving text files as UTF8 default?

Thanks!

Re: UTF8

Posted: Sat Jun 19, 2021 8:24 am
by Sergey Tkachenko
The simplest way is to specify 65001 in the CodePage parameter of the text loading methods.

Loading:

Code: Select all

  RichViewEdit1.Clear;
  RichViewEdit1.LoadText(FileName, 0, 0, False, 65001);
  RichViewEdit1.Format;
Inserting at the caret position:

Code: Select all

  RichViewEdit1.InsertTextFromFileUTF8(FileName);
or

Code: Select all

  RichViewEdit1.InsertTextFromFile(FileName, 65001);

Re: UTF8

Posted: Sun Jun 20, 2021 11:49 am
by standay
Thanks Sergey, I'll give this a try later today.

Yes, all these worked, thanks. Also used:

Code: Select all

SRichViewEdit1.ActiveEditor.InsertTextFromFile(FileName, TEncoding.UTF8.CodePage);
Which works too.