* * *

Author Topic: Embed .po files into exe, unclear on the next step  (Read 670 times)

CyberFilth

  • New member
  • *
  • Posts: 34
    • CyberFilth.co.uk
Embed .po files into exe, unclear on the next step
« on: January 07, 2017, 09:08:31 pm »
I have several language translations (.po files) for a program of mine stored in a languages subfolder and starting the program using switches on the commandline successfully changes the language of the program.
Now I want to embed the translations in the program so that I can distribute it as a standalone binary but I'm finding conflicting tutorials online.
Some advise converting the .po files to a resource, some say to compile to a .mo file first and then add that as a resource.
Also the advice on how to add a resource differs from using lazres on the command line to just going to Project Options > Add Resource

Can anyone recommend an up-to-date tutorial online, or can offer tips here, that will allow me to embed multiple translations into a program?
Thanks
Running Windows 10 & Xubuntu 64bit | Lazarus 1.6.4 | FPC 3.0.2

lainz

  • Hero Member
  • *****
  • Posts: 1836
  • Nace una flor, todos los días sale el sol...
    • BGRABitmap
Re: Embed .po files into exe, unclear on the next step
« Reply #1 on: January 07, 2017, 09:18:48 pm »
Quote
I have several language translations (.po files) for a program of mine stored in a languages subfolder and starting the program using switches on the commandline successfully changes the language of the program.

If you embed the po or mo files into the executable you will not be able to switch them using the command line (if you're using the command line that comes with default). You will need to code your own switching code.

I've done that some time ago, there are several ways all described in the wiki.

minesadorada

  • Hero Member
  • *****
  • Posts: 537
  • Retired
Re: Embed .po files into exe, unclear on the next step
« Reply #2 on: January 07, 2017, 09:34:06 pm »
Example that unpacks the .po files in the initialization section:

Code: Pascal  [Select]
  1. begin
  2.     // This uses a resource file added via Project/Options (Laz 1.7+)
  3.     if not FileExistsUTF8(sPoPath_en) then
  4.     begin
  5.       // create a resource stream which points to the po file
  6.       S := TResourceStream.Create(HInstance, 'JSONEDITOR.EN', MakeIntResource(10));
  7.       try
  8.         ForceDirectoriesUTF8(ProgramDirectory + 'locale');
  9.         F := TFileStream.Create(sPoPath_en, fmCreate);
  10.         try
  11.           F.CopyFrom(S, S.Size); // copy data from the resource stream to file stream
  12.         finally
  13.           F.Free; // destroy the file stream
  14.         end;
  15.       finally
  16.         S.Free; // destroy the resource stream
  17.       end;
  18.     end;
  19.     if not FileExistsUTF8(sPoPath_es) then
  20.     begin
  21.       S := TResourceStream.Create(HInstance, 'JSONEDITOR.ES', MakeIntResource(10));
  22.       try
  23.         ForceDirectoriesUTF8(ProgramDirectory + 'locale');
  24.         F := TFileStream.Create(sPoPath_es, fmCreate);
  25.         try
  26.           F.CopyFrom(S, S.Size);
  27.         finally
  28.           F.Free
  29.         end;
  30.       finally
  31.         S.Free;
  32.       end;
  33.     end;
  34.   end
  35.  
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus