Recent

Author Topic: Example project for translation  (Read 24012 times)

wp

  • Hero Member
  • *****
  • Posts: 7620
Example project for translation
« on: December 06, 2013, 06:13:12 pm »
Here is another example showing how to translate an application to various languages. The example is somehow based on my tutorial on this subject (http://wiki.lazarus.freepascal.org/Step-by-step_instructions_for_creating_multi-language_applications). In addition, it shows how to
- handle translation of combobox strings
- create translated strings by using the format statement
- change translation at runtime
- switch format settings
- use a shared resource string unit
- update language in several open forms.

A brief description of steps is contained in the main unit.

I guess some important items are still missing. In particular, I do not treat BiDi here since I am left-to-right only. But I would appreciate if somebody from these regions could have a look and add modifications. It would be fine if we could propose this project as an official Lazarus example.

Note: Lazarus 1.2RC or trunk needed for run-time switching of languages.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

kpeters58

  • Sr. Member
  • ****
  • Posts: 267
Re: Example project for translation
« Reply #1 on: December 06, 2013, 07:04:02 pm »
Nice work - thanks!
Lazarus 2.0.4/FPC 3.0.4/Win 64

wp

  • Hero Member
  • *****
  • Posts: 7620
Re: Example project for translation
« Reply #2 on: December 07, 2013, 09:34:10 pm »
Here is an update showing more details of format settings. This version covers BiDiMode as well. Select language "Hebrew" to see right-to-left mode. I know from another posting in this forum that the pagecontrol of Lazarus is not correct in this mode, but the rest of the demo application should work properly.

@Avishai: Would you mind to translate the Hebrew language file? Load it into poedit (http://sourceforge.net/projects/poedit/) and add the translated strings - it is easy! And it would be great if you could give me hints on missing critical items.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Windsurfer

  • Sr. Member
  • ****
  • Posts: 364
    • Windsurfer
Re: Example project for translation
« Reply #3 on: December 08, 2013, 07:01:41 pm »
Hi wp,

It is a great example. I have tested it and it looks great, including the explanations.

When I compiled it, the following messages were shown:

Options changed, recompiling clean with -B
Compiling translation_demo.lpr
Compiling main.pas
Compiling localizedforms.pas
localizedforms.pas(56,67) Warning: Local variable "nchars" does not seem to be initialized
localizedforms.pas(90,10) Warning: Function result does not seem to be set
localizedforms.pas(93,3) Note: Local variable "lng" not used
localizedforms.pas(90,30) Hint: Parameter "ALang" not used
localizedforms.pas(163,3) Warning: Symbol "GetLocaleFormatSettings" is not portable
localizedforms.pas(172,3) Note: Local variable "i" not used

I have not looked very closely, but i does not need to be declared twice in FlipChildren and lng should be commented out in the var section.

ALang possibly needs a comment to explain the compiler hint.

wp

  • Hero Member
  • *****
  • Posts: 7620
Re: Example project for translation
« Reply #4 on: December 08, 2013, 08:00:35 pm »
No warnings in the attached version...
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

eny

  • Hero Member
  • *****
  • Posts: 1609
Re: Example project for translation
« Reply #5 on: December 08, 2013, 09:49:39 pm »
Great contribution wp!
I might even start using localization myself  :D
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Example project for translation
« Reply #6 on: December 08, 2013, 10:35:34 pm »
I was trying to stay out of this topic, but since you were kind enough to ask me directly I'll give you the best answer I can.

For MSWindows platform, there is no point in trying to add RightToLeft to your globalization.  Lazarus doesn't support RightToLeft on MSWindows.

For other platforms, you would have to ask someone that uses them and knows what is required for RightToLeft because I don't know what kind of support they have for RightToLeft.
Lazarus Trunk / fpc 2.6.2 / Win32

wp

  • Hero Member
  • *****
  • Posts: 7620
Re: Example project for translation
« Reply #7 on: December 08, 2013, 11:01:41 pm »
Quote
Lazarus doesn't support RightToLeft on MSWindows.
This answer confuses me a lot: There is BiDiMode, there is FlipChildren, most controls are reacting in a way that appears reasonable to me and which is the same as in Delphi, except for a few buggy ones - I patched three of them (TRadioGroup, TCheckGroup, TCheckListbox) yesterday in http://mantis.freepascal.org/view.php?id=25408. So, what is missing to justify that harsh statement?

In the example posted above, select language Hebrew to see RightToLeft with Lazarus on Windows - at least in my understanding of what RTL should look like from what I learned from your postings here.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Example project for translation
« Reply #8 on: December 08, 2013, 11:50:29 pm »
Thant's why I was trying to stay out of this topic.  But I'll try to give you at least a partial answer.

Everything I say here is MSWindows related.  I don't know other OSs.

To the best of my knowledge you can not set TForm to be RightToLeft.  You can set BiDiMode:= bdRightToLeft, but it does not create a RightToLeft Form.  A RightToLeft Form has the Close button on the Left and the Caption/Icon on the Right.  LeftToRight Forms are not acceptable.

None of the controls on the Common Controls Tab or that descend from them and have a direction are drawn RightToLeft when BiDiMode:= bdRightToLeft and most don't publish BiDiMode.

Many of the Dialog Boxes are LeftToRight only and have to be manipulated in ways that can not be considered to be acceptable.  Even a simple MessageBox.

Far too many Lazarus Controls either lack BiDi support or can't draw themselves RightToLeft.

Other that TBarChart, I haven't even looked at the Charts.  But my guess is that they can not draw RightToLeft.

The bottom line is that if I have to write a serious RightToLeft application, I have to turn to some other tool than Lazarus.  And believe me, I hate saying that.

I think what bothers me most is that Lazarus is so very close to being a great RightToLeft tool.  Almost everything is already there and working.  It just takes that one final step.

Lazarus Trunk / fpc 2.6.2 / Win32

wp

  • Hero Member
  • *****
  • Posts: 7620
Re: Example project for translation
« Reply #9 on: December 09, 2013, 12:07:16 am »
Thanks for this answer anyway. From what I saw yesterday when comparing Delphi and Lazarus for the bugreport mentioned above, I realised that some of these issues apply also to Delphi: In RTL mode, the form's Close button is at the right as well, the pagecontrol and treeview don't change when switching BiDiMode.

When trying to fix BiDiMode for TPageControl I read that BiDiMode is not implemented by Microsoft for the common controls - then we are out of luck with Lazarus as well, at least for the native controls. I'll have to take a look at VirtualTreeView which is written without Microsoft controls.

[EDIT]: VirtualTreeView does support BiDiMode and look reasonable, it is still some kind of buggy, though.
« Last Edit: December 09, 2013, 12:20:51 am by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Example project for translation
« Reply #10 on: December 09, 2013, 12:23:14 am »
In Delphi it is a very simple thing to produce a RightToLeft application.

Code: [Select]
Type
  TForm1 = Class(TForm)
    ...
    procedure CreateParams(var Params: TCreateParams); override;
  End;

Implementation

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  if SysLocale.MiddleEast then
    Params.ExStyle:= Params.ExStyle or WS_EX_LAYOUTRTL;
end;

Initialization
  SysLocale.MiddleEast:= True;

This Mirrors everything.  You don't even set BiDiMode:= bdRightToLeft or change the Anchors.  Mirroring takes care of almost everything with only a few trivial 'fixups' to do.

But you can't do this in Lazarus.  There is a bug with TCanvas that I reported over 2 years age.  When you use MS Mirroring Technology it corrupts TCanvas and it can't be used.

I've looked for 2 years to find the problem and I'm no closer today than the day I started.  My skills aren't good enough.

See Bug Report #20506

Edit:  Mirroring also takes care of the Anchors.  It doesn't change the anchors.  If controls are anchored to the Left, they still are.  But the "Left" is now on the Right side of the Form and the "Right" is now on the  Left side of the Form.
« Last Edit: December 12, 2013, 04:41:10 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Example project for translation
« Reply #11 on: December 09, 2013, 12:41:16 am »
This is just something my friend put together quickly in Delphi to show how easy it is to make a true R2L Form.
Lazarus Trunk / fpc 2.6.2 / Win32

wp

  • Hero Member
  • *****
  • Posts: 7620
Re: Example project for translation
« Reply #12 on: December 09, 2013, 12:46:15 am »
Quote
This Mirrors everything.
Yes I see: Just started Delphi XE2, new project, add a pagecontrol to a form, used your code (without the SysLocale.MiddleEast) - and the form is mirrored! So that entire thing with BiDiMode is just outdated?
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Example project for translation
« Reply #13 on: December 09, 2013, 12:52:00 am »
BiDiMode isn't entirely outdated.  There are a few cases where it may still be useful (although I don't know where).  FlipChildren is definatel obsolete.  At least for MSWindows.

The few fixups for Delphi are things like reversing the Right/Left arrow keys when in a StringGrid and some other trivial thing like that.  But for the most part, that tiny bit of code takes care of *almost* everything.
« Last Edit: December 12, 2013, 04:35:20 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Example project for translation
« Reply #14 on: December 09, 2013, 01:07:46 am »
I just remembered where you still need BiDiMode.  If you have a RightToLeft Form, but need a LeftToRight Control on that Form, you set the BiDiMode:= bdRightToLeft.  Backward from what you want. Two Rights make a Left.
Lazarus Trunk / fpc 2.6.2 / Win32

 

TinyPortal © 2005-2018