Recent

Author Topic: Translator - New library to translate applications to any language super easily  (Read 1012 times)

domasz

  • Sr. Member
  • ****
  • Posts: 275
I've made a simple one-file library to translate applications quickly. With this lib you can switch to different languages without restarting your app.

How to use?
Simply add "Translator" to your uses and then somewhere (for example in Form.OnCreate):
Code: Pascal  [Select][+][-]
  1. TranslateForm('de', Form1);

How to prepare the translations?
In Form.OnDestroy, Form.OnClose or just in Button.OnClick add this code:
Code: Pascal  [Select][+][-]
  1. SaveOriginalTranslations('lang\original.txt');

This will save all the untranslated text to a nice file, like this:
Code: Pascal  [Select][+][-]
  1. 88483147=Choose
  2. 04422C9A=Open
  3. 5EB87A8B=Start
  4.  

Then you can just copy contents of this file into Google Translate and translate to whatever you want, for example German.
Then save your file as "lang\de.txt" so your program can find this translation.

You can save your file as "lang\German.txt" but then you should use:
Code: Pascal  [Select][+][-]
  1. TranslateForm('German', Form1);

if you don't like "lang" dir, you can use SetTranlationDir("other dir") somewhere in your code before loading your translations.


Github repo:
https://github.com/PascalVault/Lazarus_TranslateForm

Roland57

  • Sr. Member
  • ****
  • Posts: 397
    • GitLab
Hello!

Nice idea.

On Linux this will not work:

Code: Pascal  [Select][+][-]
  1. FName := TransDir + '\' + Lang + '.txt';

You can use DirectorySeparator instead.

Maybe you could also create the lang directory when it does not exist.

domasz

  • Sr. Member
  • ****
  • Posts: 275
Hello!

Nice idea.

On Linux this will not work:

Code: Pascal  [Select][+][-]
  1. FName := TransDir + '\' + Lang + '.txt';


Right, thanks! I updated the code.

Maybe you could also create the lang directory when it does not exist.

Not sure about that. The directory can be changed to any other pretty much on the fly.

Roland57

  • Sr. Member
  • ****
  • Posts: 397
    • GitLab
OK, it works here. Nice little tool (if I may say, because I don't really understand how it works).

Maybe (don't take it bad) not really useful for the real life, because it translates only the Caption property of the components. How can I do if I wish to translate a string not attached to a component?

By the way, I notice that it doesn't translate the Caption of the form.


domasz

  • Sr. Member
  • ****
  • Posts: 275
OK, it works here. Nice little tool (if I may say, because I don't really understand how it works).

Maybe (don't take it bad) not really useful for the real life, because it translates only the Caption property of the components. How can I do if I wish to translate a string not attached to a component?

By the way, I notice that it doesn't translate the Caption of the form.

Caption of the form- I'll fix that. Thanks!

Other strings - you can use _() function, eg.:

Code: Pascal  [Select][+][-]
  1. Edit1.Text := _('Original text');

Nicole

  • Hero Member
  • *****
  • Posts: 790
WOW, thank you!

Roland57

  • Sr. Member
  • ****
  • Posts: 397
    • GitLab
Caption of the form- I'll fix that. Thanks!

Welcome.  ;)

Other strings - you can use _() function, eg.:

Code: Pascal  [Select][+][-]
  1. Edit1.Text := _('Original text');

Great! I will try.

domasz

  • Sr. Member
  • ****
  • Posts: 275

Code: Pascal  [Select][+][-]
  1. Edit1.Text := _('Original text');


Great! I will try.

Please get newest version from Github. Older version doesn't save untranslated text from _() to files.

Roland57

  • Sr. Member
  • ****
  • Posts: 397
    • GitLab
Test successful.  :)

domasz

  • Sr. Member
  • ****
  • Posts: 275

 

TinyPortal © 2005-2018