Recent

Author Topic: Internationalizationg a program  (Read 10479 times)

dcelso

  • Full Member
  • ***
  • Posts: 158
Internationalizationg a program
« on: March 03, 2013, 12:47:04 am »
Hello to all, Im trying to internationalize an application, so I have activated internationalization in the option project.
I have obtained the .po file, I have translated it to two languages, myproject.es.po, myproject.en.po
But now, I dont know how use those files. I would like that the end user can change between the two languages manually. And by defaul, the aplication use the locale variable envirotmen  to select the initial language.


dcelso

  • Full Member
  • ***
  • Posts: 158
Re: Internationalizationg a program
« Reply #2 on: March 03, 2013, 12:55:55 pm »
Yes, I saw it before. Else why will i ask here? :'(.
I done an example to test it, and it continues without work,( I have lastest SVN lazarus compiled, in debian wheezy)
I have activated internationalization, in projet, and I have two .po translated.  project.en.po, and project.es.po in the LANG directory of my appdir.
The form ins the next
Code: [Select]
object Form1: TForm1
  Left = 402
  Height = 283
  Top = 319
  Width = 466
  Caption = 'Form1'
  ClientHeight = 254
  ClientWidth = 466
  Menu = MainMenu1
  LCLVersion = '1.1'
  object Button1: TButton
    Left = 288
    Height = 25
    Top = 216
    Width = 75
    Caption = '&Acept'
    OnClick = Button1Click
    TabOrder = 0
  end
  object Button2: TButton
    Left = 376
    Height = 25
    Top = 216
    Width = 75
    Caption = '&Cancel'
    TabOrder = 1
  end
  object Edit1: TEdit
    Left = 152
    Height = 31
    Top = 32
    Width = 80
    TabOrder = 2
    Text = '2'
  end
  object Label1: TLabel
    Left = 103
    Height = 21
    Top = 36
    Width = 30
    Caption = 'Age:'
    ParentColor = False
  end
  object Label2: TLabel
    Left = 103
    Height = 21
    Top = 83
    Width = 47
    Caption = 'Result:'
    ParentColor = False
  end
  object Label3: TLabel
    Left = 184
    Height = 21
    Top = 83
    Width = 47
    Caption = 'Is Even'
    ParentColor = False
  end
  object MainMenu1: TMainMenu
    left = 36
    top = 27
    object MenuItem1: TMenuItem
      Caption = '&File'
      object MenuItem2: TMenuItem
        Caption = '&Open'
      end
      object MenuItem5: TMenuItem
        Caption = '&Save'
      end
      object MenuItem3: TMenuItem
        Caption = '&Exit'
      end
    end
    object MenuItem4: TMenuItem
      Caption = '&Help'
      object MenuItem6: TMenuItem
        Caption = '&About ..'
      end
    end
  end
end

the .pas is the next
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
  StdCtrls, LResources, DefaultTranslator;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
  value : Integer;
begin
  value := StrToInt(Edit1.Text);
  if value mod 2 = 0 then
     label3.Caption:='Is Even'
  else
     label3.Caption:='Is Odd';
end;

initialization
  LRSTranslator := TPoTranslator.Create('./LANG');  (* I tested with absoulte path, too*)
end.

In the lang directory i have project.en.po, and project.es.po

My envirotment LANG var is equal to es_ES.utf8

But the program continues show the values of captions and texts made in the design time.

I don't see what  I need more to do. :'(


Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Internationalizationg a program
« Reply #3 on: March 03, 2013, 02:09:27 pm »
Quote
I have activated internationalization, in projet, and I have two .po translated.  project.en.po, and project.es.po in the LANG directory of my appdir.
I think you misunderstand the article and miss the little explanation: LANG stands for the desired language
I'll update the article so that people don't think it's literal LANG, but rather a variable.

Anyway, test the attached project, with any way you know to change language (EnvVar LANG, --lang / -l switch, whatever), of course you have to add your desired language to languages directory. You should see the label caption changes.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Internationalizationg a program
« Reply #4 on: March 03, 2013, 02:18:27 pm »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

dcelso

  • Full Member
  • ***
  • Posts: 158
Re: Internationalizationg a program
« Reply #5 on: March 04, 2013, 12:56:46 am »
Thanks to both,
After of see the example of leledumbo, I was surprised seeing how easy is translate a lazarus application.
You only need:
* activate internationalization, selecting "language" directory (other name does not work direcly) in your options project.
*translate the autogenerated language/projec1.so to a language and rename it with correct name for selected language.
* Add to your project source uses section the unit defaultranslator (going to projet-view source project menu)
* And run the application , configuring before LANG environment  variable with the language to test.

Now I'll study the blazzen example to advanced options for translating appications. So thanks to both again.

And other question, how can I add a new custom message (msgid, and msgsrt) in my .po files, and dont lose it at recompile the application? and how can I use it with gettext.
I want do it to put translated messages in my application that they aren't on visual components properties.

dcelso

  • Full Member
  • ***
  • Posts: 158
Re: Internationalizationg a program
« Reply #6 on: March 04, 2013, 01:18:43 am »
 :o, I can't test your example blaazen, when I open translatedemo.lpi, lazarus sais vinfo.pas does not found, and after of open it, at compile it sais  invalid x86_64 option, I change it to default option in option project, and to try to compile again it sais. duplicate identifier TRANSLATEDEMO in translatedemo.lpr 10,23
If I delete it of uses section, to try to compile again it sais,
fatal error cannot find unit vinfo used by translatedemo of the project inspector.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Internationalizationg a program
« Reply #7 on: March 04, 2013, 01:48:30 am »
Try "transdemo.lpi" instead. The attached archive is messy, there's too many files. I made the this demo by reducing my other project and I wasn't proper.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Internationalizationg a program
« Reply #8 on: March 04, 2013, 02:37:31 pm »
Quote
* activate internationalization, selecting "language" directory (other name does not work direcly) in your options project.
The search logic is explained in the article.
Quote
And other question, how can I add a new custom message (msgid, and msgsrt) in my .po files, and dont lose it at recompile the application? and how can I use it with gettext.
I want do it to put translated messages in my application that they aren't on visual components properties.
Use resourcestring, I don't know about gettext

dcelso

  • Full Member
  • ***
  • Posts: 158
Re: Internationalizationg a program
« Reply #9 on: March 04, 2013, 10:39:15 pm »
Thanks blaazen. Those code works well. I'll study it.

Thanks leledumbo,too.  Resourcestring is what im searching.

Another thing that i have discovered is that i can change the language in runtime with setdefaultlang. Very useful.

I have a related question more. How can i do that my appication uses the design string instead of translated strings. I thougth with setdefaultlang('') but i was wrong. Is there another function to do it?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Internationalizationg a program
« Reply #10 on: March 05, 2013, 04:55:56 pm »
What do you mean by design string?

dcelso

  • Full Member
  • ***
  • Posts: 158
Re: Internationalizationg a program
« Reply #11 on: March 05, 2013, 06:21:56 pm »
strings written in object inspector (like caption, or text fields) while you was programmign the applications.
ie,
The values of  the strings of the application without internationalization activated,
ie,
The values of the msgids in the .po files instead of the msgstr value.
ie,
default values of strings.
 ;)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Internationalizationg a program
« Reply #12 on: March 05, 2013, 06:51:43 pm »
As the last resort, DefaultTranslator will look the default <app name>.po (those with empty msgstr) in the same directory as the application binary. This should not translate your app.

However, if you have <app name>.<your system locale>.po in the languages directory (or whatever known to DefaultTranslator), it will be preferred over the default .po.

dcelso

  • Full Member
  • ***
  • Posts: 158
Re: Internationalizationg a program
« Reply #13 on: March 06, 2013, 01:02:20 pm »
 :'(, You does not understand me.
May be with an example.
I have designed an application in english language. If you open this project with lazarus you will see it in english.
I have done internationalization to english and italian.
If you run the application you can select four languages.
Default: May be english, because is the programed language
System: using locale system, getting LANG variable of system.
Spanish; using .es.po
Italian; using .es.it.

The problem is the default language, it does nothing. It leaves the last selected language instead of change it with the strings designed values.


Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Internationalizationg a program
« Reply #14 on: March 06, 2013, 02:32:09 pm »
Hi,
I don't see the difference between "System" and "Default".
In the demo I attached above, there is a choice "Default" which takes the language setting form system
Code: [Select]
{ If 'Default' then Get Language from Environment }
  if (Result='') or (LowerCase(Result)=LowerCase(csDefault)) then
    Result:=Copy(GetEnvironmentVariableUTF8('LANG'), 1, 5);
or there is setting "No translation" and it keeps english (because the demo is developed in english).
What do you expect from "Default" in your case?
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

 

TinyPortal © 2005-2018