how to internationalize a delphi application [duplicate]

感情迁移 提交于 2019-12-04 15:41:57

Maybe not the best tool for translations, but I'm using GNU Gettext for many years. The process is quite simple:

  • You run dxgettext to extract strings
  • You translate or give for translation the files
  • I personally love poEdit tool to translate and manage translation repository
  • Optional : You merge your translation files into the final EXE
  • OR you put the translation files in subdirectories and that's it !

http://dxgettext.po.dk/

Update:

1/ GNU Gettext is included in JCL/JVCL library, you just need to activate this option at startup.

2/ Gnu Gettext can translate everything in the library, as VCL, JCL/JVCL also ! It's not just limited to your code !

One option is to use the Integrated Translation Environment in Delphi:

http://docwiki.embarcadero.com/RADStudio/XE3/en/Localizing_Applications_by_Using_Translation_Manager_Overview

Here you can find two articles about this theme:

You can find other methods and commencial components (i have used TsiLang components -excellent library-)

A Greeting.

I don't know is this the best way of internationalize an application, but for me it worked. It's a kind of home made.

I created an xml file, which is the dictionary containing the translations, but you can use any other format, from json, to xls (maybe this is the best). Than implemented a class which read the translations from this file, and a way to register procedures in case of the language is changed runtime, which is I think a good feature.

TDictionary = class
private
  fOnChanged: TSignal;
  fLanguage: String;
  procedure setLanguage( const Value: String );
public
  procedure loadFromFile( filename: string );
  function getTranslation( id: string; lang: string = '' ): string;

  property language: String read fLanguage write setLanguage;
  property onChanged: TSignal read fonChanged;
end;

...

function TDictionary.getTranslation( id: string; lang: string = '' ): string;
begin  
  if lang = '' then 
    lang := Language; // use the default lang.
  // read the translation from the file.
end;

procedure TDictionary.setLanguage( const Value: String );
begin     
  fLanguage := Value;
  onChanged.Execute;
end;

TSignal is a class which register methods, and if you call Execute executes all the registered methods, Maybe in xe2 you have something built in for this, in delphi7 I had to create this class myself, but it's fun to implement.

in a form's createForm:

procedure TMyClass.doTranslate( dictionary: TObject );
begin
  with dictionary as TDictionary do
  begin
    caption := dictionary.getTranslation( 'myclass.caption' );
    button.caption := dictionary.getTranslation( 'some id' );
  end;
  // or you can go through Controls array, and automate the assignment.
end;

procedure TMyClass.FormCreate(Sender: TObject);
begin
  Dictionary.onChanged.register( doTranslate );
  doTranslate( dictionary );
end;

procedure TMyClass.FormDestroy(Sender: TObject);
begin
  Dictionary.onChanged.deregister( doTranslate ); 
end;

As you can see, this is not a working example what you can copy and paste, I just wanted to show you the idea behind. if something is not clear, comment it, and I can extend my answer.

Some notes:

  • I think it's important to have the translations in utf8 format.
  • using xls makes the localizers live easier, and your too, if they ruin your xml file (If the translator is not prof., It can happen that you get back your xml file in microsoft word format)
  • you can put your dictionary file into resource, and load from there.

Pros

  • This way you can change the language runtime
  • if you need another language, you just need to edit the dictionary file.

Cons

  • If you have many forms, it's a nightmare to connect all the labels, buttons, etc, but you can automate it in smart ways.
  • It slows down your app a little, but not much, if changing your app language is not happening so often.

There is a product called sisulizer, it works after you have build the executable fies I think. Haven't tried it but I've read a lot about it.

Have a look at this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!