Recent

Author Topic: [SOLVED] TMemo.Lines.LoadFromFile problem with French characters  (Read 11733 times)

JD

  • Hero Member
  • *****
  • Posts: 1848
Hi there everyone,

I am using a TMemo to view the contents of text files whose names and contents contain French accented characters.

Initially, it was impossible for me to load the text files into the TMemo because they had French names but I got round the problem by passing the filename into the UTF8ToAnsi function

TMemo.Lines.LoadFromFile(UTF8ToAnsi(FileName));

Now I can view the contents of the text files but the French words are garbled as shown below:
Code: [Select]
'Chirurgie'
'Gyn?cologie'
'H?pato-Gastrologie'
'Maternit?'
'M?decine Interne'

I've tried all I can (changing the TMemo character set) but I can't get the French text to display properly.

I would really appreciate your advice.

JD
« Last Edit: March 21, 2012, 12:06:12 pm by JD »
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: TMemo.Lines.LoadFromFile problem with French characters
« Reply #1 on: March 20, 2012, 05:49:43 pm »
I think those characters are written in english as a single quote, so you can search and replace the "?" with a single quote character, couldn't you?

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TMemo.Lines.LoadFromFile problem with French characters
« Reply #2 on: March 20, 2012, 07:04:40 pm »
I thought TMemo supports UTF by default? What Lazarus and fpc version do you have?

Then you can try something like:
Code: [Select]
memo1.lines.text:=UTF8ToAnsi(memo1.lines.text);Or who knows AnsiToUTF8() ...

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TMemo.Lines.LoadFromFile problem with French characters
« Reply #3 on: March 20, 2012, 08:04:30 pm »
The characters are encoded in the system code page while lazarus assumes utf8. Convert with

Code: [Select]
uses LazUTF8;
...
Memo1.Lines.Text:=SysToUtf8(Memo1.Lines.Text);

Quote
I think those characters are written in english as a single quote, so you can search and replace the "?" with a single quote character, couldn't you?
The character happens to be the 'é' but all characters above $7f are converted to '?'. French uses a lot of accented characters that are above $7f. 

joseme

  • Full Member
  • ***
  • Posts: 128
    • Logosoft sistemas
Re: TMemo.Lines.LoadFromFile problem with French characters
« Reply #4 on: March 20, 2012, 11:07:47 pm »
I finally found the answer!!!

You should do
Code: [Select]
  Memo1.Lines.Text := SystoUTF8(ReadFileToString(filename));

and voilá!!!
un aporte a la comunidad:
http://pascalylazarus.blogspot.com/

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: TMemo.Lines.LoadFromFile problem with French characters
« Reply #5 on: March 20, 2012, 11:34:55 pm »
I finally found the answer!!!

You should do
Code: [Select]
  Memo1.Lines.Text := SystoUTF8(ReadFileToString(filename));

and voilá!!!

Ça marche! Muchos gracias joseme.
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: TMemo.Lines.LoadFromFile problem with French characters
« Reply #6 on: March 21, 2012, 12:08:27 pm »
I thought TMemo supports UTF by default? What Lazarus and fpc version do you have?

Then you can try something like:
Code: [Select]
memo1.lines.text:=UTF8ToAnsi(memo1.lines.text);Or who knows AnsiToUTF8() ...

That is what I thought too. BTW I'm using Lazarus 0.9.31 svn 36014 of 17/03/2012 with FPC 2.6.0 on Windows Vista.
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

axline

  • New Member
  • *
  • Posts: 14
Re: [SOLVED] TMemo.Lines.LoadFromFile problem with French characters
« Reply #7 on: October 25, 2016, 02:19:01 pm »
Sorry for necro posting, guys. The code
Code: Pascal  [Select][+][-]
  1. memo1.lines.text:=UTF8ToAnsi(memo1.lines.text);
used to work fine before updating Lazarus to the version 1.6 (with FPC 3.0.0). Now the same code gives me just '?????' signs instead of Cyrillic symbols. Does anyone else experience the same problem? Is there any easy solution besides downgrading to Lazarus 1.4.4?

 

TinyPortal © 2005-2018