Translations / i18n / localizations for programs

From Free Pascal wiki
Revision as of 01:51, 13 August 2015 by 007 (talk | contribs) (Additional info)

Deutsch (de) English (en) español (es) français (fr) 日本語 (ja) 한국어 (ko) português (pt) русский (ru) 中文(中国大陆)‎ (zh_CN)


This is about how a program can use different strings for various languages like english, chinese, german, finnish, italian and any other language.

Quick i18n

This is intended as be a quick guide to introduce yourself in the world of translations and get things done quickly. Check also Language Codes and BidiMode.

For your information the most used languages in the world (source) are:

   * 1 - Chinese: With more than 1.2 billion native speakers in the world
   * 2 - Spanish: Spanish occupies the No. 2 spot and is spoken in approximately 30 countries.
   * 3 - English: 335 million worldwide—about 5% of the world’s population. (This does not takes into account second language spoken).
   * 4 - Hindi: Spoken by over 260 million people.
   * 5 - Arabic: Spoken in almost 60 countries around the world.
   * 6 - Portuguese: Population of Brazil is over 200 million. The population of Portugal is just over 10 million.
   * 7 - Bengali: The main language of Bangladesh (population, 155 million) and one of India’s many official languages.
   * 8 - Russian and Japanese.


The best known tool is a program called poedit. poedit is a tool for translators. It produces both PO and MO as output.

Translating Resourcestrings

This is the way to store a resourcestring in an unit:

  Caption1 = 'Some text';
  HelloWorld1 = 'Hello World';

Resourcestrings are like normal string constants, that means you can assign them to any string:

Label1.Caption := HelloWorld1;

Compiling into .po files

Resourcestrings are compiled into .po files if you enable i18n in the Lazarus IDE. Go to Project > Project Options > i18n > Enable i18n. When you recompile your application the .po files will be updated. You may also select a directory where .po files will be stored, recommended po_files.

The default translation is recommended to be in English or the default language of your application, since it will be loaded if no other translation file is found.

When you have your project1.po copy and paste it and rename it to or one of the Language Codes of your choice. Then you will have 2 languages: English and Spanish. Then you must send to the translator the files for translation.

Your folder structure will look something like this:


Converting .po files to .mo files

When you have the translation finished in the .po file, you want to compile it to .mo to load it faster since is a binary format. To convert to .mo you can use poedit and go to File > Compile as .mo.

Automatic translation

When you have the .mo files ready, put them in the locale or languages folder right to your application executable, then include the unit DefaultTranslator and that's all. The translation will be done automatically.


You want to distribute only the .mo files in the locale or languages directory, since .po files are usefull only for making the translation and compiling them into .mo.

Your folder structure will look something like this:


Testing translations

When you have everything ready you want to test if translations looks fine in your application for each language you have. Automatic translation has a feature that you can use in order to test each language quickly.

You must run your executable with the command line parameter --lang followed by the language code of your choice.

You will run your executable like this in order to test Spanish translation:

project1.exe --lang es

And you will see the translated application.

You can do this with the IDE. Go to Run > Run Parameters .... In that window in the input Command line parameters (without application name) write this:

--lang it

Then Run (F9) and you will see the translated application.

Final steps

These steps are to get a better result of your entire translation.

Translate LCL

To get everything translated you must include the LCL translations into your application locale folder.

Copy everything inside the folder C:\lazarus\lcl\languages to yout locale folder. Then you will have the LCL translated for your application.

Format Settings

Also you must read Step-by-step instructions for creating multi-language applications#Format settings to get format settings in right place.


BidiMode is for languages like Arabic that read and write right to left.

With this unit you can determine what BiDiMode is right for you.

First save this unit as ubidimodetools.pas:

unit ubidimodetools;

{$mode objfpc}{$H+}


  Classes, SysUtils;

function GetLang: string;
function GetBiDiMode: TBiDiMode;


  FileUtil, LCLProc;

function GetLang: string;
  T: string; // unused FallBackLang
  i: integer;
  { We use the same method that is used in LCLTranslator unit }

  for i := 1 to Paramcount - 1 do
    if (ParamStrUTF8(i) = '--LANG') or (ParamStrUTF8(i) = '-l') or
      (ParamStrUTF8(i) = '--lang') then
      Result := ParamStrUTF8(i + 1);

  //Win32 user may decide to override locale with LANG variable.
  if Result = '' then
    Result := GetEnvironmentVariableUTF8('LANG');

  if Result = '' then
    LCLGetLanguageIDs(Result, T);

// ToDO: Add a full list of languages specifing their BiDiMode in the function GetBiDiMode.
function GetBiDiMode: TBiDiMode;
  case GetLang of
    // Arabic
    'ar': Result := bdRightToLeft;
      Result := bdLeftToRight;


When you create a form do this:

BiDiMode := GetBiDiMode;

Note: You must be sure that you have the proper translations in order to get it working.

Technical Details


The main technology involved in the process of translations is GNU gettext. FPC comes with the gettext unit.



PO – Portable Object. This is the file that you receive back from the translators. It’s a text file that includes the original texts and the translations.


MO – Machine Object. The MO file includes the exact same contents as PO file. The two files differ in their format. While a PO file is a text file and is easy for humans to read, MO files are compiled and are easy for computers to read. The unit gettext implements TMOFile and has several procedures to do the translation from .mo files, if you want to use it.

unit gettext;


TMOFile = class


  procedure GetLanguageIDs(var Lang, FallbackLang: string);
  procedure TranslateResourceStrings(AFile: TMOFile);
  procedure TranslateUnitResourceStrings(const AUnitName:string; AFile: TMOFile);
  procedure TranslateResourceStrings(const AFilename: String);
  procedure TranslateUnitResourceStrings(const AUnitName:string; const AFilename: String);

Everything else about translations

Here goes all the translations stuff that was here in the past. There are a lot of articles and stuff about translations that can cause trouble to novices. To keep it simple these other stuff is moved here: Everything else about translations. Please keep this article as clean as possible.

See also