I saw that it's the same as used in Lazarus IDE but it's a pain in the ass write again the strings as a resource file, I want a way that's magically automatic, I'm frustrated with this aproach and I'm creating a function than open the "mo" file ans deep in the forms to translate it's contents, something like this:
unit datamodule;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Dialogs, Postgres3dyn, dblogin, gettext, translations;
type
{ TDataModuleMain }
TDataModuleMain = class(TDataModule)
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
{ private declarations }
conn : PPGConn;
pghost,pgport,pgoptions,pgtty,pgdbname, pgdbuser, pgdbpass : Pchar;
mofTranslations : TMOFile;
public
{ public declarations }
property DbHost : Pchar write pghost;
property DbPort : Pchar write pgport;
property DbOptions : Pchar write pgoptions;
property DbTTY : Pchar write pgtty;
property DbName : Pchar write pgdbname;
property DbUser : Pchar write pgdbuser;
property DbPass : Pchar write pgdbpass;
function GetPGconn() : PPGConn;
procedure TranslateForm(frm : TForm);
function TranslateStr(str : String) : String;
end;
var
DataModuleMain: TDataModuleMain;
implementation
uses menus;
{ TDataModuleMain }
procedure TDataModuleMain.DataModuleCreate(Sender: TObject);
var
PODirectory, Lang, FallbackLang : String;
begin
InitialisePostgres3;
conn := nil;
PODirectory := 'languages/';
GetLanguageIDs(Lang, FallbackLang); // in unit gettext
try
TranslateUnitResourceStrings('LCLStrConsts', PODirectory + 'lclstrconsts.%s.po', Lang, FallbackLang);
TranslateUnitResourceStrings('lr_const', PODirectory + 'lr_const.%s.po', Lang, FallbackLang);
mofTranslations := TMOFile.Create(PODirectory + Format('dadbiz.%s.mo', [FallbackLang]));
except
mofTranslations := nil;
end;
end;
procedure TDataModuleMain.DataModuleDestroy(Sender: TObject);
begin
PQfinish(conn);
ReleasePostgres3;
mofTranslations.Destroy;
end;
.......
procedure TDataModuleMain.TranslateForm(frm : TForm);
var
i, j : Integer;
str, strt : String;
procedure deepControls(ctrl : TWinControl);
var
i, j : Integer;
str, strt : String;
wctrl : TControl;
begin
for j := 0 to ctrl.ControlCount-1 do
begin
wctrl := TWinControl(ctrl).Controls[j];
wctrl.Caption:= TranslateStr(wctrl.Caption);
if wctrl is TWinControl then
deepControls(TWinControl(wctrl));
end;
end;
procedure deepMenus(mn : TMenuItem);
var
i, j : Integer;
str, strt : String;
wmn : TMenuItem;
begin
for j := 0 to mn.Count-1 do
begin
wmn := mn.Items[j];
wmn.Caption:= TranslateStr(wmn.Caption);
deepMenus(wmn);
end;
end;
begin
if mofTranslations = nil then exit;
deepControls(frm);
if frm.Menu <> nil then
begin
for j := 0 to frm.Menu.Items.Count -1 do
begin
frm.Menu.Items[j].Caption:= TranslateStr(frm.Menu.Items[j].Caption);
deepMenus(frm.Menu.Items[j])
end;
end;
end;
function TDataModuleMain.TranslateStr(str : String) : String;
begin
if mofTranslations = nil then
Result := str
else
begin
Result := mofTranslations.Translate(str);
if Result = '' then Result := str;
end;
end;
initialization
{$I datamodule.lrs}
end.
This is the first attempt, I should go inside grids and translates it column headers and with others I should find it's way,
I only need to add
DataModuleMain.TranslateForm(self);
on the FormCreate event and voyla it's done, well almost done !
I still appreciate new feedback, thanks for your answer !