Recent

Author Topic: [SOLVED] Translation of month names to Hebrew  (Read 12105 times)

wp

  • Hero Member
  • *****
  • Posts: 13264
[SOLVED] Translation of month names to Hebrew
« on: December 18, 2013, 12:31:40 am »
Running this code on Windows

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
const
  // http://msdn.microsoft.com/de-de/goglobal/bb964664.aspx
  LCID_EN = $0409;
  LCID_DE = $0407;
  LCID_FR = $040C;
  LCID_HE = $040D;
var
  s_EN, s_DE, s_FR, s_HE: String;
  fs: TFormatSettings;
begin
  // Month name in English
  GetLocaleFormatSettings(LCID_EN, fs);
  s_EN := SysToUTF8(FormatDateTime('mmmm', Date, fs));

  // Month name in German
  GetLocaleFormatSettings(LCID_DE, fs);
  s_DE := SysToUTF8(FormatDateTime('mmmm', Date, fs));

  // Month name in French
  GetLocaleFormatSettings(LCID_FR, fs);
  s_FR := SysToUTF8(FormatDateTime('mmmm', Date, fs));

  // Month name in Hebrew
  GetLocaleFormatSettings(LCID_HE, fs);
  s_HE := SysToUTF8(FormatDateTime('mmmm', Date, fs));

  ShowMessage(Format(
    'This month is'#13 +
    '  %s (in English)'#13 +
    '  %s (in German)'#13 +
    '  %s (in French)'#13 +
    '  %s (in Hebrew)', [
    s_EN, s_DE, s_FR, s_HE
  ]));

end;       

displays a messagebox with the current month in several languages. The Hebrew name, however, does not show Hebrew characters.

Am I missing something, or is this a bug?
« Last Edit: December 18, 2013, 01:27:37 pm by wp »

Bart

  • Hero Member
  • *****
  • Posts: 5643
    • Bart en Mariska's Webstek
Possible explanation:

Hebrew characters are outside your current codepage.
Hence SysToUTF8 will display "garbage".

You might try the WideString API instead and the use Utf16ToUtf8.
(And your current font needs to support displaying Hebrew characters as well.)

Bart

wp

  • Hero Member
  • *****
  • Posts: 13264
Quote
try the WideString API
What do I have to do for that?

kpeters58

  • Sr. Member
  • ****
  • Posts: 267
just to be sure:

Hebrew language support is installed on the machine in question?
Lazarus 2.0.4/FPC 3.0.4/Win 64

Avishai

  • Hero Member
  • *****
  • Posts: 1021
This is the code I use.  Note: Your "LCID_HE" and My "HE_IL" have the same definition, so just replace HE_IL with LCID_HE and it *should* work.

Code: [Select]
procedure FixHebShortDayNames;
var
  HebShortDayNames: TWeekNameArray = ('א','ב','ג','ד','ה','ו','ש');
begin
  if SysLocale.DefaultLCID = HE_IL then
    DefaultFormatSettings.ShortDayNames:= HebShortDayNames;
end;

function GetLongMonthNames(I: Integer): String;
begin
  Result:= AnsiToUTF8(FormatSettings.LongMonthNames[I]);
end;

function GetShortMonthNames(I: Integer): String;
begin
  Result:= AnsiToUTF8(FormatSettings.ShortMonthNames[I]);
end;

function GetLongDayNames(I: Integer): String;
begin
  Result:= AnsiToUTF8(FormatSettings.LongDayNames[I]);
end;

function GetShortDayNames(I: Integer): String;
begin
  Result:= AnsiToUTF8(FormatSettings.ShortDayNames[I]);
end;

function GetCurrencyString: String;
begin
  Result:= AnsiToUTF8(DefaultFormatSettings.CurrencyString);
end;

function GetHebFullDate(ADate: TDate): String;
{ 2013 ,יום ד 18 בדצמבר}
var
  CurSettings: TFormatSettings;
  CurLang: Integer;
begin
  CurSettings:= FormatSettings;
  CurLang:= SysLocale.PriLangID;
  SysLocale.PriLangID:= HE_IL;
  GetLocaleFormatSettings(SysLocale.PriLangID,FormatSettings);

  Result:= AnsiToUTF8(FormatDateTime('ddd, d בMMMM, YYYY',ADate));

  FormatSettings:= CurSettings;
  SysLocale.PriLangID:= CurLang;
  GetLocaleFormatSettings(SysLocale.PriLangID,FormatSettings);
end;

function GetHebLongDate(ADate: TDate): String;
{ 2013 יום רביעי 18 בדצמבר }
var
  CurSettings: TFormatSettings;
  CurLang: Integer;
begin
  CurSettings:= FormatSettings;
  CurLang:= SysLocale.PriLangID;
  SysLocale.PriLangID:= HE_IL;
  GetLocaleFormatSettings(SysLocale.PriLangID,FormatSettings);

  Result:= AnsiToUTF8(FormatDateTime('dddd, d בMMMM, yyyy',ADate));

  FormatSettings:= CurSettings;
  SysLocale.PriLangID:= CurLang;
  GetLocaleFormatSettings(SysLocale.PriLangID,FormatSettings);
end;
« Last Edit: December 18, 2013, 05:26:49 am by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

wp

  • Hero Member
  • *****
  • Posts: 13264
@Avishai: same problem - as I expected because your code is essentially the same as mine.

@kpeters58: that could be the reason. Unfortunately, I cannot test since I am using a German Windows 7 Professional, and language packs can only be installed for the Enterprise and Ultimate versions. Piece of crap!

Avishai

  • Hero Member
  • *****
  • Posts: 1021
wp, that doesn't sound right.  I use English Windows 7 Professional and I can install Language Packs.  Have you tried http://windows.microsoft.com/en-us/windows/language-packs#lptabs=win7
Lazarus Trunk / fpc 2.6.2 / Win32

wp

  • Hero Member
  • *****
  • Posts: 13264
Thanks for the link. But the edition required for Hebrew is marked as Ultimate or Enterprise...

Avishai

  • Hero Member
  • *****
  • Posts: 1021
I was able to install it on my system with no problem.  I'm trying to remember if I had to do anything special.  It was so long ago that I don't remember.
Lazarus Trunk / fpc 2.6.2 / Win32

CM630

  • Hero Member
  • *****
  • Posts: 1550
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
*WP, I am quite sure, that you do not have to install a language pack (if you mean a language interface pack), but to change the language for non-unicode programs, like shown in this video.+
Yet, if you do so, you will have problems with German, the umlauts will go kaputt.
So, if I get things right, GetLocaleFormatSettings(LCID_DE, fs); returns the value as ASCII.
If so, what what you have to is to use CPsomethingToUTF8... I suppose. Probably CP862toUTF8.I could offer you to add some other nonlatin language to you app- Russian, Bulgaria, Georgian... they should display monkeys, too.
Edit: Exactly, your solution is:
Code: [Select]
uses ...LConvEncoding...
.
.
s_HE := CP1255ToUTF8(FormatDateTime('mmmm', Date, fs));
.
.
But, you will have to do the same for German (s_DE := CP1252ToUTF8(FormatDateTime('mmmm', Date, fs)); ), French, etc. As you already mentioned, this is the Windows only solution.
« Last Edit: December 18, 2013, 12:39:42 pm by paskal »
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

wp

  • Hero Member
  • *****
  • Posts: 13264
Thank you, the CP1255ToUTF8 instead of SysToUTF8 did the trick.

Of course, this leads to the next question: Is there a function which determines the code page from the LCID?

wp

  • Hero Member
  • *****
  • Posts: 13264
Ah, I found it:

Code: [Select]
uses
  ... LConvEncoding, ...

function GetLocaleStr(LID, LT: Longint; const Def: string): ShortString;
// borrowed from SysUtils
var
  L: Integer;
  Buf: array[0..255] of Char;
begin
  L := GetLocaleInfo(LID, LT, Buf, SizeOf(Buf));
  if L > 0 then
    SetString(Result, @Buf[0], L - 1)
  else
    Result := Def;
end;

var
  cp: String;
  s_HE: String;

  cp := 'cp'+GetLocaleStr(LCID_HE, LOCALE_IDEFAULTANSICODEPAGE, '');
  s_HE := ConvertEncoding(FormatDateTime('mmmm', Date, fs), cp, EncodingUTF8);
« Last Edit: December 18, 2013, 01:30:09 pm by wp »

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: [SOLVED] Translation of month names to Hebrew
« Reply #12 on: December 18, 2013, 03:46:31 pm »
wp, how is "fs" defined?
Lazarus Trunk / fpc 2.6.2 / Win32

wp

  • Hero Member
  • *****
  • Posts: 13264
Re: [SOLVED] Translation of month names to Hebrew
« Reply #13 on: December 18, 2013, 03:51:04 pm »
Sorry - it is defined in the first posting: fs is type TFormatSettings.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: [SOLVED] Translation of month names to Hebrew
« Reply #14 on: December 18, 2013, 04:29:05 pm »
Sorry, I'm fighting an ear infection for the last couple of weeks and the antibiotics aren't helping.  The pain makes it hard to think or even comprehend what I'm reading.

Thanks for pointing this problem out.  I was comfortable with my code but I see now that it has to be changed.  The only reason there hasn't been a problem is because anyone that has used my work is running a Hebrew enabled system.  With your code I should be able to fix it.
Lazarus Trunk / fpc 2.6.2 / Win32

 

TinyPortal © 2005-2018