mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-05 08:45:59 +02:00
* support setting translations per unit
* store unitname as the first entry in the resourcestring table git-svn-id: trunk@2987 -
This commit is contained in:
parent
9381c9360e
commit
096113d370
@ -126,20 +126,45 @@ uses
|
|||||||
|
|
||||||
|
|
||||||
procedure Tresourcestrings.CreateResourceStringData;
|
procedure Tresourcestrings.CreateResourceStringData;
|
||||||
|
|
||||||
|
function WriteValueString(p:pchar;len:longint):TasmLabel;
|
||||||
|
var
|
||||||
|
s : pchar;
|
||||||
|
begin
|
||||||
|
current_asmdata.getdatalabel(result);
|
||||||
|
current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(aint))));
|
||||||
|
current_asmdata.asmlists[al_const].concat(tai_const.create_aint(-1));
|
||||||
|
current_asmdata.asmlists[al_const].concat(tai_const.create_aint(len));
|
||||||
|
current_asmdata.asmlists[al_const].concat(tai_label.create(result));
|
||||||
|
getmem(s,len+1);
|
||||||
|
move(p^,s^,len);
|
||||||
|
s[len]:=#0;
|
||||||
|
current_asmdata.asmlists[al_const].concat(tai_string.create_pchar(s,len));
|
||||||
|
current_asmdata.asmlists[al_const].concat(tai_const.create_8bit(0));
|
||||||
|
end;
|
||||||
|
|
||||||
Var
|
Var
|
||||||
namelab,
|
namelab,
|
||||||
valuelab : tasmlabel;
|
valuelab : tasmlabel;
|
||||||
resstrlab : tasmsymbol;
|
resstrlab : tasmsymbol;
|
||||||
s : pchar;
|
|
||||||
l : longint;
|
|
||||||
R : TResourceStringItem;
|
R : TResourceStringItem;
|
||||||
begin
|
begin
|
||||||
{ This is only smartlinkable using section smartlinking. Using objects there is
|
|
||||||
no garantuee that }
|
|
||||||
current_asmdata.asmlists[al_resourcestrings].concat(Tai_cutobject.Create_begin);
|
current_asmdata.asmlists[al_resourcestrings].concat(Tai_cutobject.Create_begin);
|
||||||
new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,'resstridx_'+current_module.localsymtable.name^+'_start',sizeof(aint));
|
new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,'resstridx_'+current_module.localsymtable.name^+'_start',sizeof(aint));
|
||||||
current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.createname_global(
|
current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.createname_global(
|
||||||
make_mangledname('RESSTR',current_module.localsymtable,'START'),AT_DATA,0));
|
make_mangledname('RESSTR',current_module.localsymtable,'START'),AT_DATA,0));
|
||||||
|
|
||||||
|
{ Write unitname entry }
|
||||||
|
namelab:=WriteValueString(@current_module.localsymtable.name^[1],length(current_module.localsymtable.name^));
|
||||||
|
current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(namelab));
|
||||||
|
current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(nil));
|
||||||
|
current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(nil));
|
||||||
|
current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
|
||||||
|
{$ifdef cpu64bit}
|
||||||
|
current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
|
||||||
|
{$endif cpu64bit}
|
||||||
|
|
||||||
|
{ Add entries }
|
||||||
R:=TResourceStringItem(List.First);
|
R:=TResourceStringItem(List.First);
|
||||||
while assigned(R) do
|
while assigned(R) do
|
||||||
begin
|
begin
|
||||||
@ -147,32 +172,11 @@ uses
|
|||||||
new_section(current_asmdata.asmlists[al_const],sec_rodata,'resstrdata_'+R.name,sizeof(aint));
|
new_section(current_asmdata.asmlists[al_const],sec_rodata,'resstrdata_'+R.name,sizeof(aint));
|
||||||
{ Write default value }
|
{ Write default value }
|
||||||
if assigned(R.value) and (R.len<>0) then
|
if assigned(R.value) and (R.len<>0) then
|
||||||
begin
|
valuelab:=WriteValueString(R.Value,R.Len)
|
||||||
current_asmdata.getdatalabel(valuelab);
|
|
||||||
current_asmdata.asmlists[al_const].concat(tai_align.Create(const_align(sizeof(aint))));
|
|
||||||
current_asmdata.asmlists[al_const].concat(tai_const.create_aint(-1));
|
|
||||||
current_asmdata.asmlists[al_const].concat(tai_const.create_aint(R.len));
|
|
||||||
current_asmdata.asmlists[al_const].concat(tai_label.create(valuelab));
|
|
||||||
getmem(s,R.len+1);
|
|
||||||
move(R.value^,s^,R.len);
|
|
||||||
s[R.len]:=#0;
|
|
||||||
current_asmdata.asmlists[al_const].concat(tai_string.create_pchar(s,R.len));
|
|
||||||
current_asmdata.asmlists[al_const].concat(tai_const.create_8bit(0));
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
valuelab:=nil;
|
valuelab:=nil;
|
||||||
{ Append the name as a ansistring. }
|
{ Append the name as a ansistring. }
|
||||||
current_asmdata.getdatalabel(namelab);
|
namelab:=WriteValueString(@R.Name[1],length(R.name));
|
||||||
l:=length(R.name);
|
|
||||||
current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(aint))));
|
|
||||||
current_asmdata.asmlists[al_const].concat(tai_const.create_aint(-1));
|
|
||||||
current_asmdata.asmlists[al_const].concat(tai_const.create_aint(l));
|
|
||||||
current_asmdata.asmlists[al_const].concat(tai_label.create(namelab));
|
|
||||||
getmem(s,l+1);
|
|
||||||
move(R.Name[1],s^,l);
|
|
||||||
s[l]:=#0;
|
|
||||||
current_asmdata.asmlists[al_const].concat(tai_string.create_pchar(s,l));
|
|
||||||
current_asmdata.asmlists[al_const].concat(tai_const.create_8bit(0));
|
|
||||||
|
|
||||||
{
|
{
|
||||||
Resourcestring index:
|
Resourcestring index:
|
||||||
|
@ -72,22 +72,20 @@ type
|
|||||||
|
|
||||||
procedure GetLanguageIDs(var Lang, FallbackLang: string);
|
procedure GetLanguageIDs(var Lang, FallbackLang: string);
|
||||||
procedure TranslateResourceStrings(AFile: TMOFile);
|
procedure TranslateResourceStrings(AFile: TMOFile);
|
||||||
|
procedure TranslateUnitResourceStrings(const AUnitName:string; AFile: TMOFile);
|
||||||
procedure TranslateResourceStrings(const AFilename: String);
|
procedure TranslateResourceStrings(const AFilename: String);
|
||||||
|
procedure TranslateUnitResourceStrings(const AUnitName:string; const AFilename: String);
|
||||||
|
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
uses {$ifdef win32} windows, {$endif}dos;
|
uses {$ifdef win32} windows, {$endif}dos;
|
||||||
|
|
||||||
var
|
|
||||||
GettextUsed: Boolean;
|
|
||||||
|
|
||||||
|
|
||||||
constructor TMOFile.Create(AStream: TStream);
|
constructor TMOFile.Create(AStream: TStream);
|
||||||
var
|
var
|
||||||
header: TMOFileHeader;
|
header: TMOFileHeader;
|
||||||
i: Integer;
|
i: Integer;
|
||||||
s: String;
|
|
||||||
begin
|
begin
|
||||||
inherited Create;
|
inherited Create;
|
||||||
|
|
||||||
@ -183,7 +181,7 @@ begin
|
|||||||
Result := '';
|
Result := '';
|
||||||
exit;
|
exit;
|
||||||
end;
|
end;
|
||||||
if (OrigTable^[nstr - 1].length = ALen) and
|
if (OrigTable^[nstr - 1].length = LongWord(ALen)) and
|
||||||
(StrComp(OrigStrings^[nstr - 1], AOrig) = 0) then
|
(StrComp(OrigStrings^[nstr - 1], AOrig) = 0) then
|
||||||
begin
|
begin
|
||||||
Result := TranslStrings^[nstr - 1];
|
Result := TranslStrings^[nstr - 1];
|
||||||
@ -224,6 +222,12 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
procedure TranslateUnitResourceStrings(const AUnitName:string; AFile: TMOFile);
|
||||||
|
begin
|
||||||
|
SetUnitResourceStrings(AUnitName,@Translate,AFile);
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
{$ifdef win32}
|
{$ifdef win32}
|
||||||
procedure GetLanguageIDs(var Lang, FallbackLang: string);
|
procedure GetLanguageIDs(var Lang, FallbackLang: string);
|
||||||
var
|
var
|
||||||
@ -297,7 +301,35 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
finalization
|
|
||||||
if GettextUsed then
|
procedure TranslateUnitResourceStrings(const AUnitName:string; const AFilename: String);
|
||||||
ResetResourceTables;
|
var
|
||||||
|
mo: TMOFile;
|
||||||
|
lang, FallbackLang: String;
|
||||||
|
begin
|
||||||
|
GetLanguageIDs(Lang, FallbackLang);
|
||||||
|
try
|
||||||
|
mo := TMOFile.Create(Format(AFilename, [FallbackLang]));
|
||||||
|
try
|
||||||
|
TranslateUnitResourceStrings(AUnitName,mo);
|
||||||
|
finally
|
||||||
|
mo.Free;
|
||||||
|
end;
|
||||||
|
except
|
||||||
|
on e: Exception do;
|
||||||
|
end;
|
||||||
|
|
||||||
|
lang := Copy(lang, 1, 5);
|
||||||
|
try
|
||||||
|
mo := TMOFile.Create(Format(AFilename, [lang]));
|
||||||
|
try
|
||||||
|
TranslateUnitResourceStrings(AUnitName,mo);
|
||||||
|
finally
|
||||||
|
mo.Free;
|
||||||
|
end;
|
||||||
|
except
|
||||||
|
on e: Exception do;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
@ -76,6 +76,7 @@ unit objpas;
|
|||||||
Function Hash(S : AnsiString) : LongWord;
|
Function Hash(S : AnsiString) : LongWord;
|
||||||
Procedure ResetResourceTables;
|
Procedure ResetResourceTables;
|
||||||
Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
|
Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
|
||||||
|
Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
|
||||||
{$ifndef RESSTRSECTIONS}
|
{$ifndef RESSTRSECTIONS}
|
||||||
Function ResourceStringTableCount : Longint;
|
Function ResourceStringTableCount : Longint;
|
||||||
Function ResourceStringCount(TableIndex : longint) : longint;
|
Function ResourceStringCount(TableIndex : longint) : longint;
|
||||||
@ -260,15 +261,49 @@ Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
|
|||||||
Var
|
Var
|
||||||
ResStr : PResourceStringRecord;
|
ResStr : PResourceStringRecord;
|
||||||
i : Longint;
|
i : Longint;
|
||||||
|
s : AnsiString;
|
||||||
begin
|
begin
|
||||||
With ResourceStringTable do
|
With ResourceStringTable do
|
||||||
begin
|
begin
|
||||||
For i:=0 to Count-1 do
|
For i:=0 to Count-1 do
|
||||||
begin
|
begin
|
||||||
ResStr:=Tables[I].TableStart;
|
ResStr:=Tables[I].TableStart;
|
||||||
|
{ Skip first entry (name of the Unit) }
|
||||||
|
inc(ResStr);
|
||||||
while ResStr<Tables[I].TableEnd do
|
while ResStr<Tables[I].TableEnd do
|
||||||
begin
|
begin
|
||||||
ResStr^.CurrentValue:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,ResStr^.HashValue,arg);
|
s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,ResStr^.HashValue,arg);
|
||||||
|
if s<>'' then
|
||||||
|
ResStr^.CurrentValue:=s;
|
||||||
|
inc(ResStr);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
|
||||||
|
Var
|
||||||
|
ResStr : PResourceStringRecord;
|
||||||
|
i : Longint;
|
||||||
|
s,
|
||||||
|
UpUnitName : AnsiString;
|
||||||
|
begin
|
||||||
|
With ResourceStringTable do
|
||||||
|
begin
|
||||||
|
UpUnitName:=UpCase(UnitName);
|
||||||
|
For i:=0 to Count-1 do
|
||||||
|
begin
|
||||||
|
ResStr:=Tables[I].TableStart;
|
||||||
|
{ Check name of the Unit }
|
||||||
|
if ResStr^.Name<>UpUnitName then
|
||||||
|
continue;
|
||||||
|
inc(ResStr);
|
||||||
|
while ResStr<Tables[I].TableEnd do
|
||||||
|
begin
|
||||||
|
s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,ResStr^.HashValue,arg);
|
||||||
|
if s<>'' then
|
||||||
|
ResStr^.CurrentValue:=s;
|
||||||
inc(ResStr);
|
inc(ResStr);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
@ -286,6 +321,8 @@ begin
|
|||||||
For i:=0 to Count-1 do
|
For i:=0 to Count-1 do
|
||||||
begin
|
begin
|
||||||
ResStr:=Tables[I].TableStart;
|
ResStr:=Tables[I].TableStart;
|
||||||
|
{ Skip first entry (name of the Unit) }
|
||||||
|
inc(ResStr);
|
||||||
while ResStr<Tables[I].TableEnd do
|
while ResStr<Tables[I].TableEnd do
|
||||||
begin
|
begin
|
||||||
ResStr^.CurrentValue:=ResStr^.DefaultValue;
|
ResStr^.CurrentValue:=ResStr^.DefaultValue;
|
||||||
@ -306,6 +343,8 @@ begin
|
|||||||
For i:=0 to Count-1 do
|
For i:=0 to Count-1 do
|
||||||
begin
|
begin
|
||||||
ResStr:=Tables[I].TableStart;
|
ResStr:=Tables[I].TableStart;
|
||||||
|
{ Skip first entry (name of the Unit) }
|
||||||
|
inc(ResStr);
|
||||||
while ResStr<Tables[I].TableEnd do
|
while ResStr<Tables[I].TableEnd do
|
||||||
begin
|
begin
|
||||||
ResStr^.CurrentValue:='';
|
ResStr^.CurrentValue:='';
|
||||||
@ -363,6 +402,12 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
|
||||||
|
begin
|
||||||
|
SetResourceStrings (SetFunction,arg);
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
Procedure ResetResourceTables;
|
Procedure ResetResourceTables;
|
||||||
|
|
||||||
Var I,J : longint;
|
Var I,J : longint;
|
||||||
|
Loading…
Reference in New Issue
Block a user