Recent

Author Topic: Localizing format settings  (Read 10181 times)

wp

  • Hero Member
  • *****
  • Posts: 13264
Localizing format settings
« on: February 11, 2013, 10:39:47 pm »
I am working at a multi-language project. I am using po files, the Translations and DefaultTranslator units, and mostly the translation is working fine. Now I came across the issue how to translate the FormatSettings, for example month and weekday names. OK, I could put them into the po files, but my feeling is that there must be an easier way. Can anybody give me a hint?
« Last Edit: February 13, 2013, 04:50:35 pm by wp »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Localizing format settings
« Reply #1 on: February 13, 2013, 08:41:03 am »
IMO they should depend by default on current OS settings (locale).
Perhaps they should be overridable by an application-specific locale setting.

AFAIR, there is such a setting for decimal commas etc.... it would make sense to have this for locale/language too.

Then again, I suspect this has not been implemented. Perhaps people like Avishai, who have been working on similar issues, can comment.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Localizing format settings
« Reply #2 on: February 13, 2013, 11:39:29 am »
It seems to me that it is already working.  When I log in to my English account and use TCalendar, I get English Month and Day names.  When I log in to my Hebrew account, I get Hebrew names.  So the system seem to be there but I haven't looked in to it.  Of course TCalendar is junk and I don't use it.
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Localizing format settings
« Reply #3 on: February 13, 2013, 12:02:16 pm »
I just did a very small test and it worked.  If I logged in English I got English.  If I logged in Hebrew I got Hebrew.  I used 2 listBox to put the names.

Code: [Select]
Procedure Tform1.Formshow(Sender: Tobject);
var
  I: Integer;
Begin
  for I:= 1 to 12 do begin
    ListBox1.Items.Add(SysToUTF8(LongMonthNames[I]));
  End;
  for I:= 1 to 7 do begin
    ListBox2.Items.Add(SysToUTF8(LongDayNames[I]));
  End;
End;
Lazarus Trunk / fpc 2.6.2 / Win32

wp

  • Hero Member
  • *****
  • Posts: 13264
Re: Localizing format settings
« Reply #4 on: February 13, 2013, 12:37:43 pm »
I think too that this is working when going from a machine with English settings to a machine with German settings. But how can I change format settings at run-time, or at least at startup on the SAME machine without going into the machine's language settings?

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Localizing format settings
« Reply #5 on: February 13, 2013, 12:53:42 pm »
I have had long discussions about this exact point.  Including adding property Language and property ParentLanguage to TApplication and all components that display or sort  text.  As far as I know, nothing has been done.  In my case the situation is even worse because I have to deal with LeftToRight and RightToLeft languages which means mirroring the TForm and all components.  Sorry, but I doubt that helps you very much.

As for changing Application language without resetting System Locale, pretty much the response I got was "why would you ever do that???"
Lazarus Trunk / fpc 2.6.2 / Win32

wp

  • Hero Member
  • *****
  • Posts: 13264
Re: Localizing format settings
« Reply #6 on: February 13, 2013, 01:05:55 pm »
OK - looks like there is no "official" solution which I might have overlooked. Anyway, if somebody out there has the same issue, here is my translation unit which I use instead of the DefaultTranslator.pas and which adds some code inspired by the Lazarus IDE code. Switching of formatsettings occurs in UpdateFormatSettings which, however, works only for Windows.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Localizing format settings
« Reply #7 on: February 13, 2013, 01:44:07 pm »
It looks like you have found your solution, but I did want to point out that there is 'Var SysLocale' although it doesn't seem to be used for much.

With your solution, how would you deal with having mixed languages on a Form?  I ask because I do this fairly often.
Lazarus Trunk / fpc 2.6.2 / Win32

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12562
  • FPC developer.
Re: Localizing format settings
« Reply #8 on: February 13, 2013, 03:56:41 pm »
I think too that this is working when going from a machine with English settings to a machine with German settings. But how can I change format settings at run-time, or at least at startup on the SAME machine without going into the machine's language settings?

Load defaultformatsettings with your own values?

On Windows you can sysutils.getlocaleformatsettings to load the record with  LCID. I do this in Delphi in the .dpr before application object initialization.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Localizing format settings
« Reply #9 on: February 13, 2013, 04:26:01 pm »
Thanks Marcov, that works like a charm.  I don't think I would have found that on my own. :)
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Localizing format settings
« Reply #10 on: February 13, 2013, 04:47:48 pm »
Wow! that solves a lot of problems.  I put a TMemo and a TListBox on a Form to test and it works perfectly. :)

Code: [Select]
const
  Lang: Integer = $40D;  // Hebrew

var
  I: Integer;
  LocaleInfo: TFormatSettings;

Procedure Tform1.Button1click(Sender: Tobject);
Begin
  if Lang= $40D then
    Lang:= $409
  else
    Lang:= $40D;
  SysUtils.GetLocaleFormatSettings(Lang, LocaleInfo);
  Memo1.Clear;
  ListBox1.Clear;
  Memo1.Lines.Add(IntToStr(Lang));
  Memo1.Lines.Add(LocaleInfo.ShortDateFormat);
  for I:= 1 to 12 do begin
    ListBox1.Items.Add(SysToUTF8(LocaleInfo.LongMonthNames[I]));
  End;
End;
« Last Edit: February 13, 2013, 04:49:24 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

wp

  • Hero Member
  • *****
  • Posts: 13264
Re: Localizing format settings
« Reply #11 on: February 13, 2013, 04:49:56 pm »
Quote
On Windows you can sysutils.getlocaleformatsettings to load the record with  LCID
That's what I am doing in the posted unit. But how about Linux?

 

TinyPortal © 2005-2018