- Posts: 3
- Thank you received: 0
×
CodeTyphon MS Windows (XP, Vista, Win7, Win8.x and Win10) OS Development, discussions and problems
Question UTF-8 localize error
- Felix Sanchez
- Topic Author
- Offline
- New Member
-
Less
More
10 years 1 month ago #2205
by Felix Sanchez
UTF-8 localize error was created by Felix Sanchez
Anyone know why when I are in the Ide and make a text in a edit book or other LCL like calendar and use special characters (spanish, áéíóú or ñ) I can't not see only wrong character
I use windows 7 and appear the fpc have a bug form information I can found in google
but how can fix?
I use windows 7 and appear the fpc have a bug form information I can found in google
but how can fix?
Please Log in or Create an account to join the conversation.
- frtrnr
- Offline
- New Member
-
Less
More
- Posts: 4
- Thank you received: 0
9 years 11 months ago - 9 years 11 months ago #2450
by frtrnr
Replied by frtrnr on topic UTF-8 localize error
"áéíóú " are displayed correctly in my laz code editor. (win7 x64 Chinese)
so I think if your OS lacks a font, e.g. simsun.ttf?
///////////////////////////////////////////////
Sorry, maybe I have mistaken your meaning!
so I think if your OS lacks a font, e.g. simsun.ttf?
///////////////////////////////////////////////
Sorry, maybe I have mistaken your meaning!
Last edit: 9 years 11 months ago by frtrnr.
Please Log in or Create an account to join the conversation.
- avra
- Visitor
-
9 years 11 months ago #2452
by avra
Replied by avra on topic UTF-8 localize error
If you can type these characters in Notepad, then change font in IDE source editor.
Please Log in or Create an account to join the conversation.
- 4aiman
-
- Offline
- Junior Member
-
- Comix creator
9 years 11 months ago - 9 years 11 months ago #2455
by 4aiman
コンソールマニアック
Replied by 4aiman on topic UTF-8 localize error
I decided to post it here rarther then starting new thread:
I have some cross-platform app that works with files and there're smth strange about codepages under win (xp)
Here's an example:
Strangely, there's NO difference whether I use {H+} or not - forcedirectories always works and savetofile not
Forcedirectories: If {H+} is ON then I get my folder in ANSI (yep, like I should) and if there's no {H+} then I get this: db.tt/UgTYFsf5 (yep, like I should, 2)
Savetofile: There's always error while creating a file.
Could anyone suggest anything to save a png file under win with non-latin character in the path?
I have some cross-platform app that works with files and there're smth strange about codepages under win (xp)
Here's an example:
if SPD.Execute then [color=#4400ff]//don't ask why, but SPD is of TSaveFileDialog class[/color]
begin
[color=#ff0000]path:=Utf8ToSys(ExtractFilePath(SPD.FileName));
nam:=UTF8ToSys(ExtractFileNameOnly(SPD.FileName));[/color] [color=#4400ff]//here I change IDE's utf8 into system cp, right? Right?[/color]
s:=TMemoryStream.Create;
s.Write(Length(icons.icons),sizeof(integer));
i:=1; s.Write(i,sizeof(byte));
for i:= 0 to length(icons.icons) - 1 do
begin
s.WriteAnsiString(icons.icons[i].name);
ForceDirectories(path+nam);
//now ^ THIS works fine with russian letters and I have my folder created :)
icons.icons[i].graphic.SaveToFile(path+nam+PathDelim+'pix_'+inttostr(i)+'.png');
// ^ but this line produces exception!
// My program can't create file "??????? ????".
// Yep, instead this "?" there should be russian characters.
// Oh, icons.icons[i].graphic is of TPortableNetworkGraphic
// class...[/color]
end;
s.SaveToFile(ChangeFileExt(spd.FileName,'.sat'));
s.Free;
exit;
end;
Strangely, there's NO difference whether I use {H+} or not - forcedirectories always works and savetofile not

Forcedirectories: If {H+} is ON then I get my folder in ANSI (yep, like I should) and if there's no {H+} then I get this: db.tt/UgTYFsf5 (yep, like I should, 2)
Savetofile: There's always error while creating a file.
Could anyone suggest anything to save a png file under win with non-latin character in the path?
コンソールマニアック
Last edit: 9 years 11 months ago by 4aiman.
Please Log in or Create an account to join the conversation.
- 4aiman
-
- Offline
- Junior Member
-
- Comix creator
- Sternas Stefanos
-
- Offline
- Moderator
-
- Ex Pilot, M.Sc, Ph.D
9 years 11 months ago #2485
by Sternas Stefanos
PilotLogic Architect and Core Programmer
Replied by Sternas Stefanos on topic UTF-8 localize error
Try this procedure
function ChangeFileExt2(const aFileName, aExtension: string): string;
var
c1,c2:integer;
ss1:string;
begin
result:='';
ss1:='';
if aFileName='' then exit;
c1:=Length(aFileName);
c2:=Length(aExtension);
if c2<1 then
begin
result:=aFileName+'.'+aExtension;
end else
begin
ss1:=LeftStr(aFileName,c1-c2-1);
result:=ss1+'.'+aExtension;
end;
end;
PilotLogic Architect and Core Programmer
Please Log in or Create an account to join the conversation.
- Rain
- Offline
- Junior Member
-
Less
More
- Posts: 61
- Thank you received: 7
9 years 11 months ago - 9 years 11 months ago #2500
by Rain
Replied by Rain on topic UTF-8 localize error
If saving an UTF8 filename does not work (as in case of TStringlist.SaveToFile() )
use a temp flename first, then rename, usingfrom FileUtil (by adding package LazUtils)
use a temp flename first, then rename, using
RenameFileUTF8(tempname, utf8name)
program testutf8;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes ,
{ you can add units after this }
SysUtils , FileUtil
;
var
utf8name, tempansiname :string;
testlist: TStringlist;
begin
testlist := TStringList.create();
testlist.Append('That is 0x03E2 = Ϣ');
utf8name := 'ϢϢϢ.txt' ;
//testlist.SaveToFile(UTF8ToSys(utf8name)); // unable to create file ???.txt
//testlist.SaveToFile(SysToUTF8(utf8name)); // creates wrong filename: âââ.txt
//testlist.SaveToFile(utf8name); // creates wrong filename: ϢϢϢ.txt
tempansiname := 'temp8899.tmp';
testlist.SaveToFile(tempansiname); // creates temp filename: temp8899.tmp
RenameFileUTF8(tempansiname, utf8name); // creates correct filename: ϢϢϢ.txt
end.
Last edit: 9 years 11 months ago by Rain.
Please Log in or Create an account to join the conversation.
- 4aiman
-
- Offline
- Junior Member
-
- Comix creator
9 years 11 months ago - 9 years 11 months ago #2511
by 4aiman
コンソールマニアック
Replied by 4aiman on topic UTF-8 localize error
Sternas, Rain - thanks to both of you, and my apologies for delay.
Once I dug it up, all of that "UTF business" seems pretty obvious to me, but just in case some other people might need this, "the path I've walked through" is here, under spoiler:
So, my code should be as folows:
Once I dug it up, all of that "UTF business" seems pretty obvious to me, but just in case some other people might need this, "the path I've walked through" is here, under spoiler:
Warning: Spoiler!
I experienced troubles with this:
And sholuld I press "Stop" Lazarus IDE would bring forward this:
As I could see in UTF8toSYS implementation - there is some checking function used to avoid the situation when some string in ANSI would be treated as UTF8 and converted to "ANSI" for a second time.
So whether I use UTF8toSys or not, Tgraphic.SavetoFile should allways return ANSI for my windows system, right?
Well, I actually have tried to check that again:Now this time the error message looks a bit different:
So, icons had been saved and now it's "*.sat.icons" turn.
As TFIleSaveDialog produces an UTF8 string, I tried to convert it to ANSI and then tried to use changefileext:From what I've read I thought that spd.filename would be converted to ANSI and changefileext with 2 ANSI strings would result in ANSI string... But this didn't work >_<
So, I tried to change that code above to this:And NOW it works!
And sholuld I press "Stop" Lazarus IDE would bring forward this:
procedure TGraphic.SaveToFile(const Filename: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(UTF8ToSys(Filename), fmCreate); < - here's where my error!
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;
As I could see in UTF8toSYS implementation - there is some checking function used to avoid the situation when some string in ANSI would be treated as UTF8 and converted to "ANSI" for a second time.
So whether I use UTF8toSys or not, Tgraphic.SavetoFile should allways return ANSI for my windows system, right?
Well, I actually have tried to check that again:
icons.icons[i].graphic.SaveToFile(ChangeFileExt(spd.FileName,'')+PathDelim+'pix_'+inttostr(i)+'.png');
So, icons had been saved and now it's "*.sat.icons" turn.
As TFIleSaveDialog produces an UTF8 string, I tried to convert it to ANSI and then tried to use changefileext:
s.SaveToFile(ChangeFileExt(UTF8toSys(spd.FileName),'.sat.icons')));
So, I tried to change that code above to this:
s.SaveToFile(UTF8toSys(ChangeFileExt(spd.FileName,'.sat.icons')));
So, my code should be as folows:
Warning: Spoiler!
if SPD.Execute then
begin
path:=Utf8ToSys(ExtractFilePath(SPD.FileName));
nam:=UTF8ToSys(ExtractFileNameOnly(SPD.FileName));
s:=TMemoryStream.Create;
s.Write(Length(icons.icons),sizeof(integer));
i:=1; s.Write(i,sizeof(byte));
for i:= 0 to length(icons.icons) - 1 do
begin
s.WriteAnsiString(icons.icons[i].name);
ForceDirectories(path+nam);
icons.icons[i].graphic.SaveToFile(ChangeFileExt(spd.FileName,'')+PathDelim+'pix_'+inttostr(i)+'.png');
end;
s.SaveToFile(UTF8toSys(ChangeFileExt(spd.FileName,'.sat.icons')));
s.Free;
exit;
end;
コンソールマニアック
Last edit: 9 years 11 months ago by 4aiman.
Please Log in or Create an account to join the conversation.