Recent

Author Topic: Translate a String that is not a constant?  (Read 4044 times)

MaxM74

  • New Member
  • *
  • Posts: 37
Translate a String that is not a constant?
« on: July 28, 2025, 11:46:27 am »
Is it possible to translate a string that isn't a constant?
Let me explain.
Using the WIA interface on Windows, it returns the item name of a Device, for example, Feeder.
Obviously, the string is NOT a constant but a variable.
If I put a string in resourcestring with the value 'Feeder' and the corresponding
Italian translation in the po file 'Caricatore', how can I get it translated?

I tried Format(theVariable, []) but it doesn't work.
Thanks.

Thaddy

  • Hero Member
  • *****
  • Posts: 18363
  • Here stood a man who saw the Elbe and jumped it.
Re: Translate a String that is not a constant?
« Reply #1 on: July 29, 2025, 08:50:56 am »
As far as I know that is not possible using po files. These rely on constants.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

alpine

  • Hero Member
  • *****
  • Posts: 1410
Re: Translate a String that is not a constant?
« Reply #2 on: July 29, 2025, 11:03:14 am »
Is it possible to translate a string that isn't a constant?
Let me explain.
Using the WIA interface on Windows, it returns the item name of a Device, for example, Feeder.
Obviously, the string is NOT a constant but a variable.
If I put a string in resourcestring with the value 'Feeder' and the corresponding
Italian translation in the po file 'Caricatore', how can I get it translated?

I believe you can use the TPOFile class from the Translations unit.
Code: Pascal  [Select][+][-]
  1.  POF := TPOFile.Create('translation.it.po');
  2.  //...
  3.  Translated := POF.Translate('wia.device.feeder', 'Feeder');

You'll probably have to play around a bit.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 18363
  • Here stood a man who saw the Elbe and jumped it.
Re: Translate a String that is not a constant?
« Reply #3 on: July 29, 2025, 04:29:08 pm »
That won't solve it, because the pdf? That is not a constant.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

jcmontherock

  • Sr. Member
  • ****
  • Posts: 316
Re: Translate a String that is not a constant?
« Reply #4 on: July 29, 2025, 04:50:04 pm »
You have to set the I18n parm in the project options. The resources strings are automatically included in pot and po files and translated at execution time.
Windows 11 UTF8-64 - Lazarus 4.2-64 - FPC 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 18363
  • Here stood a man who saw the Elbe and jumped it.
Re: Translate a String that is not a constant?
« Reply #5 on: July 30, 2025, 11:18:30 am »
Yes @alpine
totally forgot that.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

MaxM74

  • New Member
  • *
  • Posts: 37
Re: Translate a String that is not a constant?
« Reply #6 on: July 30, 2025, 06:07:06 pm »
You have to set the I18n parm in the project options. The resources strings are automatically included in pot and po files and translated at execution time.
is not a resourcestring i get this string from an interface.

MaxM74

  • New Member
  • *
  • Posts: 37
Re: Translate a String that is not a constant?
« Reply #7 on: July 30, 2025, 06:10:11 pm »
I believe you can use the TPOFile class from the Translations unit.
Code: Pascal  [Select][+][-]
  1.  POF := TPOFile.Create('translation.it.po');
  2.  //...
  3.  Translated := POF.Translate('wia.device.feeder', 'Feeder');

You'll probably have to play around a bit.

A work Around maybe something like
if theVar='Feeder' then Result:= rsMyConstantEqualToFeeder
 %)

alpine

  • Hero Member
  • *****
  • Posts: 1410
Re: Translate a String that is not a constant?
« Reply #8 on: July 31, 2025, 10:59:35 am »
A work Around maybe something like
if theVar='Feeder' then Result:= rsMyConstantEqualToFeeder
 %)
In the .po file:
Code: Text  [Select][+][-]
  1. ...
  2. msgid "Feeder"
  3. msgstr "Caricatore"
  4. ...

PO files are defining pairs msgid-->msgstr and the TPOFile is just a HashTable(or AvlTree?) with ability to load a .po file and then you can search by msgid. Perhaps that is just what you need.
In addition, LCLTranslator takes care of loading the appropriate .po file according to the current/given language and can crawl over active forms to change the child captions. But that seems unnecessary for the case.
I would just make a lookup table in the source in that simple case (en --> it translation). May be even a TStringList with a Key=Value pairs which one to load from a file.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018