Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2008-02

Unicode resourcestrings

30 messages · 9 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Daniël Mantione
to Martin Schreiber

Op Fri, 29 Feb 2008, schreef Martin Schreiber:

Hi, Is there a way in current FPC to have unicode or wide resourcestrings? Thanks,

Resourcestrings are ansistrings, so the answer is no. This is indeed a shortcoming in a widestring environment, the only solution is probably to encode them UTF-8.

Daniël

Reply by email

Martin Schreiber
to Daniël Mantione
Daniël Mantione wrote 9 lines
Op Fri, 29 Feb 2008, schreef Martin Schreiber: > Hi, > Is there a way in current FPC to have unicode or wide resourcestrings? > Thanks, Resourcestrings are ansistrings, so the answer is no. This is indeed a shortcoming in a widestring environment, the only solution is probably to encode them UTF-8.

This works with utf-8 system encoding only -> not in an usual Windows installation.
Are there plans to implement unicode resourcestrings?

Martin

Reply by email

Daniël Mantione
to Martin Schreiber

Op Fri, 29 Feb 2008, schreef Martin Schreiber:

Show quoted text 12 lines
On Friday 29 February 2008 09.02:02 Daniël Mantione wrote: > Op Fri, 29 Feb 2008, schreef Martin Schreiber: >> Hi, >> Is there a way in current FPC to have unicode or wide resourcestrings? >> Thanks, > > Resourcestrings are ansistrings, so the answer is no. This is indeed a > shortcoming in a widestring environment, the only solution is probably to > encode them UTF-8. > This works with utf-8 system encoding only -> not in an usual Windows installation.

There is utf8decode/utf8encode.

Are there plans to implement unicode resourcestrings?

No, but this is just because you are the first one to ask. Could you make a proposal? Then we'll see how this can be implemented.

Daniël

Reply by email

Marco van de Voort
to Martin Schreiber: This works with utf-8 system encoding only -> not in an usual Windows installation. Are…
Show quoted text 11 lines
On Friday 29 February 2008 09.02:02 Dani?l Mantione wrote: > Op Fri, 29 Feb 2008, schreef Martin Schreiber: > > Hi, > > Is there a way in current FPC to have unicode or wide resourcestrings? > > Thanks, > > Resourcestrings are ansistrings, so the answer is no. This is indeed a > shortcoming in a widestring environment, the only solution is probably to > encode them UTF-8. This works with utf-8 system encoding only -> not in an usual Windows installation.

Afaik XP+ supports UTF-8, and it could be it is also backported to 2000?

Reply by email

Martin Schreiber
to Marco van de Voort
Marco van de Voort wrote 14 lines
> On Friday 29 February 2008 09.02:02 Dani?l Mantione wrote: > > Op Fri, 29 Feb 2008, schreef Martin Schreiber: > > > Hi, > > > Is there a way in current FPC to have unicode or wide > > > resourcestrings? Thanks, > > > > Resourcestrings are ansistrings, so the answer is no. This is indeed a > > shortcoming in a widestring environment, the only solution is probably > > to encode them UTF-8. > > This works with utf-8 system encoding only -> not in an usual Windows > installation. Afaik XP+ supports UTF-8, and it could be it is also backported to 2000?

MSEgui applications should run on win95/98 and on every locale encoding the user wants to use.

Martin

Reply by email

Martin Schreiber
to Daniël Mantione: Op Fri, 29 Feb 2008, schreef Martin Schreiber: There is utf8decode/utf8encode. No, but…
Daniël Mantione wrote 16 lines
Op Fri, 29 Feb 2008, schreef Martin Schreiber: > On Friday 29 February 2008 09.02:02 Daniël Mantione wrote: >> Op Fri, 29 Feb 2008, schreef Martin Schreiber: >>> Hi, >>> Is there a way in current FPC to have unicode or wide resourcestrings? >>> Thanks, >> >> Resourcestrings are ansistrings, so the answer is no. This is indeed a >> shortcoming in a widestring environment, the only solution is probably >> to encode them UTF-8. > > This works with utf-8 system encoding only -> not in an usual Windows > installation. There is utf8decode/utf8encode.

The big advantage of resourcestrings is that they can be used like normal string constants in the source. If I need to call utf8encode for every used stringconstant I can use the MSEgui widestring stockcaption facility instead.

Show quoted text 5 lines
> Are there plans to implement unicode resourcestrings? No, but this is just because you are the first one to ask. Could you make a proposal? Then we'll see how this can be implemented.

Ideally from my point of view would be if the resourcestrings are stored in utf-8 if the unit is compiled with -Fcutf8 and decoded by utf8decode for widestring assignment on runtime independent of the system encoding.

Martin

Reply by email

Daniël Mantione
to Martin Schreiber

Op Fri, 29 Feb 2008, schreef Martin Schreiber:

Show quoted text 29 lines
On Friday 29 February 2008 09.25:18 Daniël Mantione wrote: > Op Fri, 29 Feb 2008, schreef Martin Schreiber: >> On Friday 29 February 2008 09.02:02 Daniël Mantione wrote: >>> Op Fri, 29 Feb 2008, schreef Martin Schreiber: >>>> Hi, >>>> Is there a way in current FPC to have unicode or wide resourcestrings? >>>> Thanks, >>> >>> Resourcestrings are ansistrings, so the answer is no. This is indeed a >>> shortcoming in a widestring environment, the only solution is probably >>> to encode them UTF-8. >> >> This works with utf-8 system encoding only -> not in an usual Windows >> installation. > > There is utf8decode/utf8encode. > The big advantage of resourcestrings is that they can be used like normal string constants in the source. If I need to call utf8encode for every used stringconstant I can use the MSEgui widestring stockcaption facility instead. >> Are there plans to implement unicode resourcestrings? > > No, but this is just because you are the first one to ask. Could you make > a proposal? Then we'll see how this can be implemented. > Ideally from my point of view would be if the resourcestrings are stored in utf-8 if the unit is compiled with -Fcutf8 and decoded by utf8decode for widestring assignment on runtime independent of the system encoding.

This has been discussed before. Automatic conversion to/from ansistrings will always be to/from system encoding. If you want another encoding than the system encoding you will have to do a manual conversion.

For the compiler, a resourcestring has the same type as an ansistring, so no distinction can be made between resourcestrings/ansistrings.

Daniël

Reply by email

Martin Schreiber
to Daniël Mantione
Daniël Mantione wrote 39 lines
Op Fri, 29 Feb 2008, schreef Martin Schreiber: > On Friday 29 February 2008 09.25:18 Daniël Mantione wrote: >> Op Fri, 29 Feb 2008, schreef Martin Schreiber: >>> On Friday 29 February 2008 09.02:02 Daniël Mantione wrote: >>>> Op Fri, 29 Feb 2008, schreef Martin Schreiber: >>>>> Hi, >>>>> Is there a way in current FPC to have unicode or wide >>>>> resourcestrings? Thanks, >>>> >>>> Resourcestrings are ansistrings, so the answer is no. This is indeed a >>>> shortcoming in a widestring environment, the only solution is probably >>>> to encode them UTF-8. >>> >>> This works with utf-8 system encoding only -> not in an usual Windows >>> installation. >> >> There is utf8decode/utf8encode. > > The big advantage of resourcestrings is that they can be used like normal > string constants in the source. If I need to call utf8encode for every > used stringconstant I can use the MSEgui widestring stockcaption facility > instead. > >>> Are there plans to implement unicode resourcestrings? >> >> No, but this is just because you are the first one to ask. Could you >> make a proposal? Then we'll see how this can be implemented. > > Ideally from my point of view would be if the resourcestrings are stored > in utf-8 if the unit is compiled with -Fcutf8 and decoded by utf8decode > for widestring assignment on runtime independent of the system encoding. This has been discussed before. Automatic conversion to/from ansistrings will always be to/from system encoding. If you want another encoding than the system encoding you will have to do a manual conversion. For the compiler, a resourcestring has the same type as an ansistring, so no distinction can be made between resourcestrings/ansistrings.

Another suggestion:

implementation
resourcestrings
 resstring1 = 'abc';
 resstring2 = '1234';
resourcewidestrings
 resstring3 = 'abc';
 resstring4 = '1234';
procedure test;
var
 wstr1,wstr2: widestring;
begin
 wstr1:= resstring3; //no conversion
 wstr2:= resstring1; //uses system encoding to convert
end;

Martin

Reply by email

Marco van de Voort
to Martin Schreiber: Another suggestion: implementation resourcestrings resstring1 = 'abc'; resstring2 =…
Show quoted text 12 lines
On Friday 29 February 2008 10.07:29 Dani?l Mantione wrote: Another suggestion: implementation resourcestrings resstring1 = 'abc'; resstring2 = '1234'; resourcewidestrings resstring3 = 'abc'; resstring4 = '1234';

I don't like inventing Delphi incompat syntax for it, specially since probably starting with D2008 resourcestrings will be unicode too.

So a directive would make more sense (and more importantly, that can be abstracted in e.g. a global include files as many OSS pascal projects do)

Reply by email

peter green
to Daniël Mantione: Op Fri, 29 Feb 2008, schreef Martin Schreiber: This has been discussed before. Automatic…
Show quoted text 5 lines
This has been discussed before. Automatic conversion to/from ansistrings will always be to/from system encoding. If you want another encoding than the system encoding you will have to do a manual conversion.

Actually you as the application developer can control how conversions between ansistrings and widestrings are handled by using getwidestringmanager/setwidestringmanager .

So for example you could use a byte value of $7F at the beggining of an ansistring as a marker that the string was in UTF-8 and implement a Ansi2WideMoveProc that checked for it and either interpreted it as UTF-8 if it saw the marker and passed it off to the old Ansi2WideMoveProc if not.

Reply by email

Christian Iversen
to Martin Schreiber: This works with utf-8 system encoding only -> not in an usual Windows installation. Are…
Martin Schreiber wrote 12 lines
On Friday 29 February 2008 09.02:02 Daniël Mantione wrote: > Op Fri, 29 Feb 2008, schreef Martin Schreiber: >> Hi, >> Is there a way in current FPC to have unicode or wide resourcestrings? >> Thanks, > Resourcestrings are ansistrings, so the answer is no. This is indeed a > shortcoming in a widestring environment, the only solution is probably to > encode them UTF-8. > This works with utf-8 system encoding only -> not in an usual Windows installation. Are there plans to implement unicode resourcestrings?

There is a non-ambiguous UTF-8 to UTF-16 mapping. It should be no problem at all, except for the extra programming effort.

Med venlig hilsen Christian Iversen

Reply by email

Martin Schreiber
to Daniël Mantione: Op Fri, 29 Feb 2008, schreef Martin Schreiber: This has been discussed before. Automatic…
Daniël Mantione wrote 12 lines
> > Ideally from my point of view would be if the resourcestrings are stored > in utf-8 if the unit is compiled with -Fcutf8 and decoded by utf8decode > for widestring assignment on runtime independent of the system encoding. This has been discussed before. Automatic conversion to/from ansistrings will always be to/from system encoding. If you want another encoding than the system encoding you will have to do a manual conversion. For the compiler, a resourcestring has the same type as an ansistring, so no distinction can be made between resourcestrings/ansistrings.

That means that the program must be compiled in the same system encoding as the target runs in order to get working resourcestrings? That would resourcestrings for i18n purposes render useless apart from utf-8-only or latin-1-only. utf-8-only is not possible for older existing installations.

The compiler does not know that the source is a resource string? But the compiler could know it, the information is available? If the resourcestring kind of source is known, fpc_ResourceStr_To_WideStr could be called instead of fpc_AnsiStr_To_WideStr, the widestring manager needs an additional procedure Resource2WideMoveProc where the appropriate conversion takes place.

Martin

Reply by email

Daniël Mantione
to Martin Schreiber

Op Sun, 2 Mar 2008, schreef Martin Schreiber:

Show quoted text 17 lines
On Friday 29 February 2008 10.07:29 Daniël Mantione wrote: >> >> Ideally from my point of view would be if the resourcestrings are stored >> in utf-8 if the unit is compiled with -Fcutf8 and decoded by utf8decode >> for widestring assignment on runtime independent of the system encoding. > > This has been discussed before. Automatic conversion to/from ansistrings > will always be to/from system encoding. If you want another encoding than > the system encoding you will have to do a manual conversion. > > For the compiler, a resourcestring has the same type as an ansistring, so > no distinction can be made between resourcestrings/ansistrings. > That means that the program must be compiled in the same system encoding as the target runs in order to get working resourcestrings? That would resourcestrings for i18n purposes render useless apart from utf-8-only or latin-1-only. utf-8-only is not possible for older existing installations.

In the traditional way of translating software (from the age when resourcestrings were invented) you encode your translation in the encoding of the target system. If you do a Dutch translation you could encoude your translation in ISO-8859-1 and if you do a Czech translation you could encoude the translation in ISO-8859-2.

A more modern approach is to encode all your translations in Unicode and convert them to the target encoding. I.e. UTF-8 to system encoding is what Lazarus does. There is utf8toansi function in the system unit for this purpose. But then you have to call utf82ansi on every resourcestring you access, similar to utf8decode if you use widestrings.

The compiler does not know that the source is a resource string? But the compiler could know it, the information is available?

Language wise, resourcestrings are ansistring constants. So whenever you use a resourcestring identifier in your code, the compiler considers this an expression with type ansistring. Therefore, regarding typing they are equal.

Regarding code generation, there is a difference between ansistrings and resourcestrings, since with a resourcestring load, the compiler must look into the resourcestring tables to find the ansistring constant.

Daniël

Reply by email

Martin Schreiber
to Daniël Mantione
Daniël Mantione wrote 5 lines
Regarding code generation, there is a difference between ansistrings and resourcestrings, since with a resourcestring load, the compiler must look into the resourcestring tables to find the ansistring constant.

So it is theoretical possible to call "fpc_ResourceStr_To_WideStr" instead of fpc_AnsiStr_To_WideStr if the source is a resourcestring?

Reply by email

Daniël Mantione
to Martin Schreiber

Op Sun, 2 Mar 2008, schreef Martin Schreiber:

Show quoted text 8 lines
On Sunday 02 March 2008 10.22:32 Daniël Mantione wrote: > > Regarding code generation, there is a difference between ansistrings and > resourcestrings, since with a resourcestring load, the compiler must look > into the resourcestring tables to find the ansistring constant. > So it is theoretical possible to call "fpc_ResourceStr_To_WideStr" instead of fpc_AnsiStr_To_WideStr if the source is a resourcestring?

These kind of routines are called by the code generator part that does a type conversion. As there is no type conversion, no such routine can be called. I'm not stating it is theoretically impossible, just stating that the normal type conversion mechanism cannot be used.

Daniël

Reply by email

Martin Schreiber
to Daniël Mantione
Daniël Mantione wrote 14 lines
Op Sun, 2 Mar 2008, schreef Martin Schreiber: > On Sunday 02 March 2008 10.22:32 Daniël Mantione wrote: >> Regarding code generation, there is a difference between ansistrings and >> resourcestrings, since with a resourcestring load, the compiler must >> look into the resourcestring tables to find the ansistring constant. > > So it is theoretical possible to call "fpc_ResourceStr_To_WideStr" > instead of fpc_AnsiStr_To_WideStr if the source is a resourcestring? These kind of routines are called by the code generator part that does a type conversion. As there is no type conversion, no such routine can be called. I'm not stating it is theoretically impossible, just stating that the normal type conversion mechanism cannot be used.

Then the only possible way to use resourcestrings independent from the system encoding is to compile all resource units with -Fcutf8 and to write every assignment as "widestringvar:= Utf8Decode(resourceconstant);"? Would this method work?

Martin

Reply by email

Daniël Mantione
to Martin Schreiber

Op Sun, 2 Mar 2008, schreef Martin Schreiber:

Show quoted text 19 lines
On Sunday 02 March 2008 14.09:47 Daniël Mantione wrote: > Op Sun, 2 Mar 2008, schreef Martin Schreiber: >> On Sunday 02 March 2008 10.22:32 Daniël Mantione wrote: >>> Regarding code generation, there is a difference between ansistrings and >>> resourcestrings, since with a resourcestring load, the compiler must >>> look into the resourcestring tables to find the ansistring constant. >> >> So it is theoretical possible to call "fpc_ResourceStr_To_WideStr" >> instead of fpc_AnsiStr_To_WideStr if the source is a resourcestring? > > These kind of routines are called by the code generator part that does a > type conversion. As there is no type conversion, no such routine can be > called. I'm not stating it is theoretically impossible, just stating that > the normal type conversion mechanism cannot be used. > Then the only possible way to use resourcestrings independent from the system encoding is to compile all resource units with -Fcutf8 and to write every assignment as "widestringvar:= Utf8Decode(resourceconstant);"? Would this method work?

Yes, that method would work.

Daniël

Reply by email

Martin Schreiber
to Daniël Mantione
Daniël Mantione wrote 8 lines
> Then the only possible way to use resourcestrings independent from the > system encoding is to compile all resource units with -Fcutf8 and to > write every assignment as "widestringvar:= > Utf8Decode(resourceconstant);"? > Would this method work? Yes, that method would work.
Does not work for me, see attachment. Compiled with -Fcutf8 on win2000
with "ANSI" encoding.
"
const
 strco1 = 'abcäöüûôõ';
 strco2 = 'Русский';
 strco3 = 'ABCDEFG';
resourcestring
 strco1res = 'abcäöüûôõ';
 strco2res = 'Русский';
 strco3res = 'ABCDEFG';
implementation
uses
 main_mfm;
procedure tmainfo.test(const sender: TObject);
begin
 di1.value:= '"'+strco1+'"';
 di2.value:= '"'+strco2+'"';
 di3.value:= '"'+strco3+'"';
 di1res.value:= '"'+strco1res+'"';
 di2res.value:= '"'+strco2res+'"';
 di3res.value:= '"'+strco3res+'"';
 di1resutf8.value:= '"'+utf8decode(strco1res)+'"';
 di2resutf8.value:= '"'+utf8decode(strco2res)+'"';
 di3resutf8.value:= '"'+utf8decode(strco3res)+'"';
end;
"
What did I wrong?

Martin

Reply by email

Florian Klaempfl
to Martin Schreiber
Show quoted text 38 lines
On Sunday 02 March 2008 15.31:04 Daniël Mantione wrote: >> Then the only possible way to use resourcestrings independent from the >> system encoding is to compile all resource units with -Fcutf8 and to >> write every assignment as "widestringvar:= >> Utf8Decode(resourceconstant);"? >> Would this method work? > Yes, that method would work. > Does not work for me, see attachment. Compiled with -Fcutf8 on win2000 with "ANSI" encoding. " const strco1 = 'abcäöüûôõ'; strco2 = 'Русский'; strco3 = 'ABCDEFG'; resourcestring strco1res = 'abcäöüûôõ'; strco2res = 'Русский'; strco3res = 'ABCDEFG'; implementation uses main_mfm; procedure tmainfo.test(const sender: TObject); begin di1.value:= '"'+strco1+'"'; di2.value:= '"'+strco2+'"'; di3.value:= '"'+strco3+'"'; di1res.value:= '"'+strco1res+'"'; di2res.value:= '"'+strco2res+'"'; di3res.value:= '"'+strco3res+'"'; di1resutf8.value:= '"'+utf8decode(strco1res)+'"'; di2resutf8.value:= '"'+utf8decode(strco2res)+'"'; di3resutf8.value:= '"'+utf8decode(strco3res)+'"'; end; " What did I wrong?

I'am not sure how this could be made working just a remark: -Fcutf8 influences _only_ the interpretation of string constants when converting them to unicode.

Reply by email

Daniël Mantione
to Florian Klaempfl

Op Sun, 2 Mar 2008, schreef Florian Klaempfl:

Show quoted text 5 lines
> What did I wrong? I'am not sure how this could be made working just a remark: -Fcutf8 influences _only_ the interpretation of string constants when converting them to unicode.

Right. Try to compile the (utf-8 encoded) file without specifying a code page, this should result in the constants ending up in the resourcestring without processing.

Daniël

Reply by email

Martin Schreiber
to Daniël Mantione: Op Sun, 2 Mar 2008, schreef Florian Klaempfl: Right. Try to compile the (utf-8 encoded)…
Daniël Mantione wrote 11 lines
Op Sun, 2 Mar 2008, schreef Florian Klaempfl: >> What did I wrong? > > I'am not sure how this could be made working just a remark: -Fcutf8 > influences _only_ the interpretation of string constants when converting > them to unicode. Right. Try to compile the (utf-8 encoded) file without specifying a code page, this should result in the constants ending up in the resourcestring without processing.

And now the widestring constants are broken, see attachment...

Martin

Reply by email

ik
to Martin Schreiber

I wonder, do you care also to try BIDI text as well ?

Ido

Martin Schreiber wrote 22 lines
On Sunday 02 March 2008 18.48:01 Daniël Mantione wrote: > Op Sun, 2 Mar 2008, schreef Florian Klaempfl: > >> What did I wrong? > > > > I'am not sure how this could be made working just a remark: -Fcutf8 > > influences _only_ the interpretation of string constants when converting > > them to unicode. > > Right. Try to compile the (utf-8 encoded) file without specifying a code > page, this should result in the constants ending up in the resourcestring > without processing. > And now the widestring constants are broken, see attachment... Martin _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Florian Klaempfl
to Martin Schreiber: And now the widestring constants are broken, see attachment... Martin
Show quoted text 11 lines
On Sunday 02 March 2008 18.48:01 Daniël Mantione wrote: > Op Sun, 2 Mar 2008, schreef Florian Klaempfl: >>> What did I wrong? >> I'am not sure how this could be made working just a remark: -Fcutf8 >> influences _only_ the interpretation of string constants when converting >> them to unicode. > Right. Try to compile the (utf-8 encoded) file without specifying a code > page, this should result in the constants ending up in the resourcestring > without processing. > And now the widestring constants are broken, see attachment...

Did you utf-8 decode the string constants?

Reply by email

Daniël Mantione
to Martin Schreiber: And now the widestring constants are broken, see attachment... Martin

Op Sun, 2 Mar 2008, schreef Martin Schreiber:

Show quoted text 13 lines
On Sunday 02 March 2008 18.48:01 Daniël Mantione wrote: > Op Sun, 2 Mar 2008, schreef Florian Klaempfl: >>> What did I wrong? >> >> I'am not sure how this could be made working just a remark: -Fcutf8 >> influences _only_ the interpretation of string constants when converting >> them to unicode. > > Right. Try to compile the (utf-8 encoded) file without specifying a code > page, this should result in the constants ending up in the resourcestring > without processing. > And now the widestring constants are broken, see attachment...

Ok, that was indeed to be expected.

Yes, I believe some compiler changes need to be made. Resourcestrings should have the encoding of the source files, if you encode your source file in utf-8, the resourcestrings will have to be encoded in utf-8. No idea what processing currently occurs. The compiler will see the string constants as widestring constants, then must do some conversion back to ansistring to get them into the resourcestring tables.

Daniël

Reply by email

Martin Schreiber
to Florian Klaempfl: Did you utf-8 decode the string constants?
Florian Klaempfl wrote 16 lines
Martin Schreiber schrieb: > On Sunday 02 March 2008 18.48:01 Daniël Mantione wrote: >> Op Sun, 2 Mar 2008, schreef Florian Klaempfl: >>>> What did I wrong? >>> >>> I'am not sure how this could be made working just a remark: -Fcutf8 >>> influences _only_ the interpretation of string constants when >>> converting them to unicode. >> >> Right. Try to compile the (utf-8 encoded) file without specifying a code >> page, this should result in the constants ending up in the >> resourcestring without processing. > > And now the widestring constants are broken, see attachment... Did you utf-8 decode the string constants?

The program was the same as before.

Reply by email

Anton Kavalenka
to Florian Klaempfl: Did you utf-8 decode the string constants?
Florian Klaempfl wrote 26 lines
Martin Schreiber schrieb: > On Sunday 02 March 2008 18.48:01 Daniël Mantione wrote: > >> Op Sun, 2 Mar 2008, schreef Florian Klaempfl: >> >>>> What did I wrong? >>>> >>> I'am not sure how this could be made working just a remark: -Fcutf8 >>> influences _only_ the interpretation of string constants when converting >>> them to unicode. >>> >> Right. Try to compile the (utf-8 encoded) file without specifying a code >> page, this should result in the constants ending up in the resourcestring >> without processing. >> >> > And now the widestring constants are broken, see attachment... > Did you utf-8 decode the string constants? _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Inside our projects there are lots of WIdeStrings handling.

For Delphi/Kylix we use the following dirty hack (somewhat reworked routine from Borland RTL):
This one is Widows implementation,
There is also linux implementation

function LoadResStringW(const pRec: PResStringRec): WideString;
var
  hInst:cardinal;
  Buffer: array [0..1023] of char;
begin
  if pRec = nil then Exit;
  if pRec^.Identifier < 64*1024 then
  begin
    hInst:=FindResourceHInstance(pRec^.Module^);
    // try wide
    if LoadStringW(hInst,pRec^.Identifier, @Buffer, SizeOf(Buffer))>0 then
      Result:=PWideChar(@Buffer)
    else // try narrow
    if LoadString(hInst,pRec^.Identifier, @Buffer, SizeOf(Buffer))>0 then
      Result:=PChar(@Buffer)
    else
      Result:='-lost-';
  end
  else
    Result := PWideChar(pRec^.Identifier);
end;

To get the wide string from resource string by its identifier, we go the following:

resourcestring
  sWideStringID='Some string';
var
    mw:WideString;
mw:=LoadresStringW(@sWideStringID)

Under FPC there is special implementation, but dirty hack does not work all the time, since there is hard to determine - where was the resource string
was implicitly decoded to current codepage. PRestringRec is boralnd specific, under FPC it is PAnsiString;

function LoadResStringW(const pRec: PResStringRec): WideString;inline;
begin
  if pRec=nil then
  Result:=''
  else
  begin
    {$ifdef LINUX}
    Result:=PAnsiString(pRec)^;
    {$else}
    {$ifdef WindowsUnicodeSupport}
    Result:=UTF8Decode(PAnsiString(pRec)^);
    {$else}
    Result:=PAnsiString(pRec)^;
    {$endif}
    {$endif}
  end;
end;
{$ENDIF} // end platform-compiler dependent string loader

Please help -
How to implement that dirty hack.

Best regards,
Anton Kavalenka

Reply by email