I am using delphi 2010 in WinXP. In C# and Java there are Normalizer function. It can transform letters with diacritics into ASCII (remove those accent marks). I do not know if there is Normalizer in delphi. What I am testing is below, but failed. I do not know how to solve it.
I am using the NormalizeString
Test for stripping accents
aáeéiíoóöuúü AÁEÉIÍOÓÖUÚÜ --> aaeeiiooouuu AAEEIIOOOUUU
Type
...
const
NormalizationD=2;
var
Form1: TForm1;
implementation
{$R *.dfm}
function NormalizeString(NormForm: Integer; lpSrcString: LPCWSTR; cwSrcLength: Integer;
lpDstString: LPWSTR; cwDstLength: Integer): Integer; stdcall; external 'C:\WINDOWS\system32\normaliz.dll';
function NormalizeText(Str: string): string;
var
nLength: integer;
c: char;
i: integer;
temp: string;
CatStr:string;
begin
SetLength(temp, Length(Str));
nLength := NormalizeString(NormalizationD, PChar(Str), Length(Str), PChar(temp), 0);
CatStr:='';
for i := 1 to length(temp) do
begin
c:=temp[i];
if (TCharacter.GetUnicodeCategory(c) <> TUnicodeCategory.ucNonSpacingMark) or
(TCharacter.GetUnicodeCategory(c) <> TUnicodeCategory.ucCombiningMark) then
CatStr:=CatStr+c;
end;
result:=CatStr;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
memo1.lines.Text:= 'aáeéiíoóöuúü AÁEÉIÍOÓÖUÚÜ';
memo2.Lines.Add(NormalizeText(memo1.Lines.Text));
end;
procedure TForm1.Button4Click(Sender: TObject);
var
w:string;
begin
w:='aáeéiíoóöuúü AÁEÉIÍOÓÖUÚÜ';
memo2.Lines.Add(NormalizeText(w));
end;
Observation 1:
After starting test program, if clicking button3 for the first time, memo1 loads 'aáeéiíoóöuúü AÁEÉIÍOÓÖUÚÜ' but No string is added in memo2. If I click button3 for the second time, memo2 adds in 'aáeéiíoóöuúü AÁEÉIÍOÓÖUÚÜ', (same as memo1, NormalizeString not works).
Observation 2:
After starting test program, If I first click button4, memo2 adds in unknown characters '܀¨꽐', if I then click button3 memo2 still adds in those unknow characters, if I then click button3, mem2 adds in 'aáeéiíoóöuúü AÁEÉIÍOÓÖUÚÜ', (same as memo1, NormalizeString not works).
It seems NormalizeText does not work and
why I click button3 twice to have ouput in memo2.
How can I solve it? Thank you in advance.