Recent

Author Topic: Add 'Language' property to Controls with Text?  (Read 58689 times)

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Add 'Language' property to Controls with Text?
« on: July 04, 2012, 07:47:44 pm »
This is just an idea that I have and would like some opinions.  I have not tried to do this so there could be huge problems to make it work.

The Idea:
For Controls that have Text, a new property could be added for 'Language' with the default set to the programmer's Language.  It could be used to:

1. Set the correct Keybord Language on Entry to the Control.  The user could still switch with the normal Keybord switch in run time.

2. In the Property Editor, if BiDiMode IsLeftToRight, only LeftToRight Languages would be listed in the DropDown List, and if BiDiMode IsRightToLeft, only RightToLeft Languages.

3. Set the correct Sort routine for Controls that have Lists.  The last time I checked, it seemed that each Control had a different default Sort Routine and all of them failed for many Languages.  So the programmer had to do extra work for this very basic task.

4. I do not know enough about Far East Languages to make any suggestions.

It might be a good idea to add a property SetKBD: Boolean
« Last Edit: July 04, 2012, 07:57:42 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Add 'Language' property to Controls with Text?
« Reply #1 on: July 05, 2012, 08:02:40 am »
Are you saying that you are using multiple keyboard layouts in a form - presumably one for English and one for Hebrew and/or Arabic?
Do other programming environments have similar facilities? I would expect functionality like that to be handled by the operating system, really... but I have 0 experience with RTL, so I'm probably wrong...

As for the sorting: apart from the obvious suggeston to unify the sorting routines as much as possible (to be able to override them in one spot): using some property to override standard sorting sounds like a good idea....

However: shouldn't sorting be specified by localization in general? No idea how Delphi and Lazarus do this now (I am myself only now discovering the joy of accented characters and other alphabets... ;)... but I would imagine this sorting behaviour to chnage automatically with localization.... unless explicitly overridden in the property editor (because something needs a certain fixed sorting rule).
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: Add 'Language' property to Controls with Text?
« Reply #2 on: July 05, 2012, 08:43:19 am »
Hi BigChimp.  I have several apps that use multiple Languages.  I don't think it's that uncommon.  One example a government office Form.  A person's name must be in both Hebrew and English.  So I have 'Avishai' and 'אבישי'.  But it could be Spanish and Russian or any other lang.

Sorting is one of the most basic operations in computing.  I think it all started with punchcards if you remember those things.  Yet in Lazarus (and I think most programming tools) sorting is not so 'basic'.  According to Blaazen, Croatian and many other languages have special sorting rules.  Lazarus can sort most langs, but you have to do extra work if it's not the 'standard' sort and it's not easy to figure out which sort method to use.  I still haven't found the right one for Hebrew.  You should see the mess I get with Hebrew and TStringList 'standard' sort!  It shouldn't be that way for such a basic task.

Locale doesn't really work too well.  Many people must switch between different langs on a routine basis and it isn't practical to change Locales just to get proper sorting, if that even works.  But I haven't tried it so I don't know.  Maybe I should.  Imagine working at an International travel agency.

As for exploring new languages, good for you :)
« Last Edit: July 07, 2012, 03:08:38 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Add 'Language' property to Controls with Text?
« Reply #3 on: July 05, 2012, 08:56:32 am »
I did a 'proof of concept' derived from TEdit and it worked perfectly for setting keyboard language.  One was set to Hebrew and another set to English.  Keyboard switching was automatic.  I haven't tried Sorting yet.  Of course, to make this work globally, it would take a lot more work.  I think you would have to start with TApplication.Lang and TControl.Lang + TControl.ParentLang.
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Add 'Language' property to Controls with Text?
« Reply #4 on: July 05, 2012, 09:34:14 am »
I just tried switching Locales and the sort order did not change.  For Hebrew the default sort was still a complete failure.  This is probably true for any language that does not use the default sort order.
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Add 'Language' property to Controls with Text?
« Reply #5 on: July 07, 2012, 03:24:40 pm »
If there was a 'TApplication.Lang' property, maybe 'Translations' could default to the Language setting as well.  So an English speaking programmer could use English to create a Menu for example, and set Application.Lang:= 'RU' and the Menu would be translated to Russian along with the rest of the Application.
Lazarus Trunk / fpc 2.6.2 / Win32

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9792
  • Debugger - SynEdit - and more
    • wiki
Re: Add 'Language' property to Controls with Text?
« Reply #6 on: July 07, 2012, 03:47:22 pm »
1) Keyboard mapping. Imho, that is a task, that must, be left to the OS.

I do not know, how you have done your proof of concept?
But just translating something like "a" becomes "whatever" will not work.

People may have different keyboards connected, which can have totally different layouts, and even make your mappings differ. And then there are dead keys, that will do nothing, unless combined with the next stroke. e.g one key says "accent" and the next "a", which results in an accented "a". OR try the IME for Japanese and Chinese

And you can't even be sure about one single language. I use a uk keymap. But dvorak. So even it produces the keystrokes for uk English, it has a different layout.

There are also special input methods for disabled people, which may again be different...

If you need to change the input for a specific TEdit, then you need to tell the OS, and let the OS handle that.
Afaik the way to do this, is to communicate with the IME (Input method manager). That exists on most OS.

On windows the user can set global hotkeys (outside the app), to change the layout. But afaik, the app can change certain IME settings too.

------------
2) Sorting:

Yes it would be good to have a global hook, for all string comparisation. Not sure, maybe it exist....

As for the rest, I think implementing custom sort orders, should be left to the user.

In any way it is not done by language, but by "collation". Alone the German lang, has several sort orders...
And knowing that, within one application, different places could use different collations of the same language.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Add 'Language' property to Controls with Text?
« Reply #7 on: July 07, 2012, 04:04:45 pm »
Thanks Martin_fr.  It's always good to get input from you.

Presently, when I need to switch Keyboards from one control to the next, I use

const
  LangHeb = $40D;

var
  BiDiKeyboard, NonBiDiKeyboard: LongInt;

procedure SetLang(AForm: TForm);
{ Load Keyboard Language - Windows ONLY! }
begin
{$IfDef Windows}
  case AForm.ActiveControl.IsRightToLeft of
    True : ActivateKeyboardLayout(BiDiKeyboard, 0);
    False: ActivateKeyboardLayout(NonBiDiKeyboard, 0);
  end;
{$EndIf}
end;

initialization
  NonBiDiKeyboard:= GetUserDefaultLangId;
  BiDiKeyboard:= LangHeb;

This is enough for my needs because I usually only have to deal with two languages.  But it doesn't take much to add other languages.  This is only good for Windows, but I have read that the same thing can be done for Linux and Mac.
Lazarus Trunk / fpc 2.6.2 / Win32

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9792
  • Debugger - SynEdit - and more
    • wiki
Re: Add 'Language' property to Controls with Text?
« Reply #8 on: July 07, 2012, 04:12:18 pm »
{$IfDef Windows}
  case AForm.ActiveControl.IsRightToLeft of
    True : ActivateKeyboardLayout(BiDiKeyboard, 0);
    False: ActivateKeyboardLayout(NonBiDiKeyboard, 0);
  end;
{$EndIf}

Ok, so that leaves it to the OS, which is correct.

Only question is, if something like dvorak exists. Because then the keys for the same language, and same bidi setting, are on different locations.

So unless you know, the user has a standard keyboard, the user should maybe be able to configure the required language?

There are more things I do not know.
If you use Japaneese, then there is a special IME, and it knows different modes. I do not know, if or if not that code will reset (or should reset) those....


Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Add 'Language' property to Controls with Text?
« Reply #9 on: July 07, 2012, 04:26:58 pm »
Haha!  Yes, it is a very complicated process.  My little routine is very specific and a bit specialized.  And of course, the keyboard must already be installed.  But I think with some work Lazarus might be able to help make it a bit simpler.

Sorting is really kind of a major issue in my opinion.  Lazarus has several custom sorts already in code, but the programmer has to know which one to use and they must manually override the 'standard' sort.  Again, I think it could be made simpler. 

But the key to both keyboard and sort issues depends on knowing what language you are dealing with for each control.  That is why I started this post.

Lazarus and FPC are such fine tools, and having a truly international user base gives it a strength that most tools do not have. 
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Add 'Language' property to Controls with Text?
« Reply #10 on: July 07, 2012, 05:47:52 pm »
Wow!  I may be the last to know this, but I just discovered that TListBox doesn't even sort English correctly!  I added the English Alphabet in UpperCase and then in LowerCase and set TListBox.Sorted:= True;  I did the same with TComboBox and the Sort was consistent, but TListBox sorted  Upper,Lower - Lower,Upper - Upper,Lower...  %)
Lazarus Trunk / fpc 2.6.2 / Win32

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9792
  • Debugger - SynEdit - and more
    • wiki
Re: Add 'Language' property to Controls with Text?
« Reply #11 on: July 07, 2012, 06:58:18 pm »
It simply looks, like it sort case insensitive.

So "A" and "a" is the same, and they have therefore no specific order.

I can think of 3 sort results:

1) letter, then case (upper, then lower)
  Age, abort, Bold, bend
2) case, then letter
  Age, Bold, abort, bend
3) letter, case ignored (upper, then lower)
 abort, Age, bend, Bold

of course, in the 3rd case, items that are identical, except for casing, will be in random order, as case is 100% ignored

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Add 'Language' property to Controls with Text?
« Reply #12 on: July 07, 2012, 08:21:17 pm »
What you say is true, but why is it that TComboBox can get it 'right' and TListBox can not?  Of course they both can, but you have to use a custom sort.  This seems very strange.

Luckily, Hebrew does not have 'Case' so it is not a problem, but I still have to use a custom sort anyway.

Edit:  If all of the controls that need to be able to sort simply pointed to a CustomSort instead of having there own unique sort at least their sort be be consistent.  Then the TControl.Lang property could be used to selet the CustomSort.
« Last Edit: August 03, 2012, 04:27:14 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9792
  • Debugger - SynEdit - and more
    • wiki
Re: Add 'Language' property to Controls with Text?
« Reply #13 on: July 07, 2012, 10:43:04 pm »
Well, I don't know the concept behind...

I would have thought thy would have an OnCompareItems. But it looks not... Then the actual sort is in the widgetset. which means, it may even be OS dependent.... But as I said I do not know the answer to that

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Add 'Language' property to Controls with Text?
« Reply #14 on: July 07, 2012, 10:55:42 pm »
I looked at the code for TListBox and TComboBox and they each have there own unique sort code 'built in'.  I think this may be true for other Controls as well.  If this is true, it really makes no sense.  I'm sort of new to Lazarus so I may be completely wrong about this.  Anyway Sorting is a very basic method and the language is what must drive the sort method.  In my opinion, there can not be such a thing as a 'default' sort method.  It must be Language specific and that is one of the reasons that I think it would be worthwhile to add TControl.Lang.  If you start with TApplication.Lang then that gives a 'default.Sort' to all children.
Lazarus Trunk / fpc 2.6.2 / Win32

 

TinyPortal © 2005-2018