Forum > Packages and Libraries

Translator - New library to translate applications to any language super easily

(1/2) > >>

domasz:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TranslateForm('de', Form1);
How to prepare the translations?
In Form.OnDestroy, Form.OnClose or just in Button.OnClick add this code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---SaveOriginalTranslations('lang\original.txt');
This will save all the untranslated text to a nice file, like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---88483147=Choose04422C9A=Open5EB87A8B=Start 
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
Hello!

Nice idea.

On Linux this will not work:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---FName := TransDir + '\' + Lang + '.txt';
You can use DirectorySeparator instead.

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

domasz:

--- Quote from: Roland57 on February 09, 2023, 04:59:47 pm ---Hello!

Nice idea.

On Linux this will not work:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---FName := TransDir + '\' + Lang + '.txt';

--- End quote ---

Right, thanks! I updated the code.


--- Quote from: Roland57 on February 09, 2023, 04:59:47 pm ---Maybe you could also create the lang directory when it does not exist.

--- End quote ---

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

Roland57:
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:

--- Quote from: Roland57 on February 09, 2023, 06:51:00 pm ---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.

--- End quote ---

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

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


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Edit1.Text := _('Original text');

Navigation

[0] Message Index

[#] Next page

Go to full version