Link to home
Start Free TrialLog in
Avatar of myleseven
myleseven

asked on

Chinese and Delphi

Hi there,
I have created a basic wordprocessor using Delphi. At present everything is in English.
I would like to know how I could make my program accessible to people who speak chinese.
For example I use richedit and edit boxes - how can I allow chinese characters to be types into these controls.
Is it possible?

kind regards
Myles
Avatar of saravananvg
saravananvg
Flag of India image

Hello Sir,

  You could check the following site if it is of any use to you.

http://delphi.about.com/library/weekly/aa102400b.htm

with regards,
padmaja.
Avatar of AmigoJack
AmigoJack

you could use these components http://www.tntware.com/delphicontrols/unicode/downloads.htm - they give you every standard control (also richedit and edit) with unicode (widestring) instead of string support. this way you can display any language of the world
I use the compos from tnt and they're working very nice

here is a sample code (excerpt) adding ideograms to a listbox. A click on the listbox insert the ideogram in the richedit. You need a Unicode font installed on the system (Arial Unicode MS, SimSun, ...)

procedure TCnEditor.BtCnClick(Sender: TObject);
{LMDLB is a ListBox}
var
i: integer;
begin
LMDLB.clear;
LMDLB.items.BeginUpdate;
{1ère série: 4E de 00 à FF}
LMDLB.Items.Add(WideChar($4E00)); // the first ideo
LMDLB.Items.Add(WideChar($4E01));
LMDLB.Items.Add(WideChar($4E02));
LMDLB.Items.Add(WideChar($4E03));
LMDLB.Items.Add(WideChar($4E04));
LMDLB.Items.Add(WideChar($4E05));
LMDLB.Items.Add(WideChar($4E06));
LMDLB.Items.Add(WideChar($4E07));
LMDLB.Items.Add(WideChar($4E08));
LMDLB.Items.Add(WideChar($4E09));
LMDLB.Items.Add(WideChar($4E0A));
LMDLB.Items.Add(WideChar($4E0B));
LMDLB.Items.Add(WideChar($4E0C));
LMDLB.Items.Add(WideChar($4E0D));
LMDLB.Items.Add(WideChar($4E0E));
LMDLB.Items.Add(WideChar($4E0F));

LMDLB.Items.Add(WideChar($4E10)); //
LMDLB.Items.Add(WideChar($4E11));
LMDLB.Items.Add(WideChar($4E12));
LMDLB.Items.Add(WideChar($4E13));
LMDLB.Items.Add(WideChar($4E14));
LMDLB.Items.Add(WideChar($4E15));
LMDLB.Items.Add(WideChar($4E16));
LMDLB.Items.Add(WideChar($4E17));
LMDLB.Items.Add(WideChar($4E18));
LMDLB.Items.Add(WideChar($4E19));
LMDLB.Items.Add(WideChar($4E1A));
LMDLB.Items.Add(WideChar($4E1B));
LMDLB.Items.Add(WideChar($4E1C));
LMDLB.Items.Add(WideChar($4E1D));
LMDLB.Items.Add(WideChar($4E1E));
LMDLB.Items.Add(WideChar($4E1F));
{and so on .... or use a loop to add a range of ideograms}
LMDLB.items.EndUpdate;
end;

procedure TCnEditor.LMDLBClick(Sender: TObject);
var
i : Integer;
begin
i:= LMDLB.ItemIndex;
tntRichedit1.clear;
tntRichedit1.lines.add(LMDLB.Items[i]);
end;

If you want to change the active keyboard, do this:

procedure TCnEditorArabicKeyboard1Click(Sender: TObject);
beg
//you can change the keyboard layout using the LoadKeyboardLayout API,
//input to chinese
richedit1.setfocus;
LoadKeyboardLayout('00000404',KLF_ACTIVATE); {404 - Traditional chinese - american keyboard}
//input to english
//LoadKeyboardLayout('00000409',KLF_ACTIVATE);
//
//to see the available languages layout look in the regedit in this key [XP]
//
//HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout\DosKeybCodes
end;

Avatar of myleseven

ASKER

Hi team,
Thanks for your help. The guys from TNT look very helpful.
I have installed the components onto my Delphi and have ahd a quick play around. No luck in getting them working yet but I think it is a matter of time.
Quick question should TCnEditorArabicKeyboard1Click be TCnEditor.ArabicKeyboard1Click where ArabicKeyboard1 is a buttton?
if so when I click this button should Richedit1 then type with chinese characters?
I don't appear to have 00000404' in my registry, for now could you guys help me to get it working with spanish?

padmaja thanks for your help but I think this only applies to enterprise as I have Delphi 7 pro and could not find the ITE

thanks again
Myles

create a new project, add a TntMemo, add a button, doubleclick the button and insert the following code:

TntMemo1.Lines.Add(WideChar($4E00)); // the first ideo
TntMemo1.Lines.Add(WideChar($4E01));
TntMemo1.Lines.Add(WideChar($4E02));
TntMemo1.Lines.Add(WideChar($4E03));
TntMemo1.Lines.Add(WideChar($4E04));
TntMemo1.Lines.Add(WideChar($4E05));
TntMemo1.Lines.Add(WideChar($4E06));
TntMemo1.Lines.Add(WideChar($4E07));
TntMemo1.Lines.Add(WideChar($4E08));
TntMemo1.Lines.Add(WideChar($4E09));
TntMemo1.Lines.Add(WideChar($4E0A));
TntMemo1.Lines.Add(WideChar($4E0B));
TntMemo1.Lines.Add(WideChar($4E0C));
TntMemo1.Lines.Add(WideChar($4E0D));
TntMemo1.Lines.Add(WideChar($4E0E));
TntMemo1.Lines.Add(WideChar($4E0F));

thats the demo :) you probably dont "get it working" because the delphi code editor itself cant handle unicode. the most simple demo is also to start this project, then open your internet browser, watch some chinese website, copy any text and then paste it to the memo. then you see that it is able to display the japanese characters.


to get this all working your windows version must support unicode. that are NT4, 2000, XP, 2003 - simply use "Tahoma" as font for your components
thanks for your guidence,
The main reason that I did not have it installed was because I needed to install chinese onto my op sys.
That LoadKeyboardLayout function is great though and has really got me started.
Now say I need to show chinese characters in a label. How would I go about doing that?

regards
Myles
like i stated above. you can copy chinese characters from a chinese website and paste them into the "caption" property of a TTntLabel in the property inspector. you had to install chinese on your os? all you need should be having asian/unicode-fonts installed. they may suck up to 260MB but then tahoma.ttf and others are able to display a lot of unicode characters.
ASKER CERTIFIED SOLUTION
Avatar of bernani
bernani
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
.... and it will be converted to the chinese sentence: 我在写汉字呢 (don't know if the 6 last ideo will be displayed on EE).

As you can see, the answer is no.

You can paste the line (我在写汉字&#21602) in your Html-Editor to see the chinese characters (save your html page in Big5).
Thanks alot guys,
I think I have got all of the above, you did answer my last question:
"Now say I need to show chinese characters in a label. How would I go about doing that?"

to some degree as you mentioned I can copy and paste chinese characters into the caption of the label.
Imagine I have over 100 specific labels in my application. These labels will help the user Identify screens and buttons etc.
Is this copy and paste method the only way I can change the caption of a label or a button?
Is there anyway that I can write chinese characters inside  a unit

eg tntlabel1.caption := '你好吗';

xie xie
(thank you)

:)
something like:

unit ChineseResources;

const
  sFileOpen = '你好吗';
  sFileClose = '你好吗#20320;好吗';

so you can have

uses
  ChineseResources;

TnTLabel1.Caption := sFileOpen;

next time, when you want to switch to another language, you can have

unit EnglishResources;

const
  sFileOpen = '&Open';
  sFileClose = '&Close';

and you do not need to modify your code.



DragonSlayer.
Thanks for the advice DragonSlayer,
Your method to build a program for ease of language switching is a good one and I think I will use it. However when I tried your suggestion I only ended up with the numbers above being displayed in the caption.

I am wondering is there a way (similar to windows) that I can write chinese characters directly into a Delphi unit.
ie my code would look like:

const
  sFileOpen = '你好吗';

xie xie ;)
谢谢你
interesting
in the above message I just wrote to you I copied and pasted chinese characters into the editor yet they still got displayed as numbers....
I just tried to copy and paste chinese characters into the IDE and I got a series of ? ? ? where the characters should be. So perhaps there is not a way? Or is it possible to install the chinese language into Delphi 7 profesional
Oops, didn't know that you would take it literally. I was just showing an example on how you can localise it.

An alternative is to use resource files (string tables) as shown here http://delphi.about.com/library/weekly/aa011805a.htm

Type in the string using a Unicode-enabled editor and then compile the resource. And modify the code as in this link http://www.multilizer.com/dev/article.asp?a=583 to make it work for Unicode.


Cheers,
DragonSlayer.
If you use the tnt compo, you can directly input the chinese char in the text properties (caption, .... ) of the compos.

See the code beneath: I put 2 tntLabels on the form and in the caption of each form I type in chinese character:
- the sentence : ni hao ma
- the word: xiexie
without changing the least other properties of the labels except the font size (from 8 to 20). To achieve this you only toggle your language bar to chinese, type the desired word or sentence in the IME Editor and toggle back to english when you're done.
The code beneath compile OK with D5 on Windows XP Pro with asian languages installed.And D5 isn't unicode.

See in the DFM how the unicode text of the two tntlabeln.caption are stored by Delphi:

 Caption = '???'
    ...
    ...
    ...
    ...
    ...
    ...
    Caption_UTF7 = '+T2BZfVQX' {for the sentence ni hao ma}

___________________________
unit Unit1; {unit1.pas}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, TntStdCtrls;

type
  TForm1 = class(TForm)
    TntLabel1: TTntLabel;
    TntLabel2: TTntLabel;
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

end.

______________________________
{unit1.dfm}

object Form1: TForm1
  Left = 192
  Top = 114
  Width = 214
  Height = 153
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object TntLabel1: TTntLabel
    Left = 32
    Top = 24
    Width = 57
    Height = 24
    Caption = '???'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -20
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
    Caption_UTF7 = '+T2BZfVQX'
  end
  object TntLabel2: TTntLabel
    Left = 32
    Top = 64
    Width = 38
    Height = 24
    Caption = '??'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -20
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
    Caption_UTF7 = '+jCKMIg'
  end
end

And if I add a tntMemo1 on the form with the words:
ni hao ma
zhongguo ren
hanyu

the dfm is the following:

object Form1: TForm1
  Left = 192
  Top = 114
  Width = 356
  Height = 244
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object TntLabel1: TTntLabel
    Left = 32
    Top = 24
    Width = 57
    Height = 24
    Caption = '???'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -20
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
    Caption_UTF7 = '+T2BZfVQX'
  end
  object TntLabel2: TTntLabel
    Left = 32
    Top = 64
    Width = 38
    Height = 24
    Caption = '??'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -20
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
    Caption_UTF7 = '+jCKMIg'
  end
  object TntMemo1: TTntMemo
    Left = 136
    Top = 32
    Width = 185
    Height = 145
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -33
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    Lines.WideStrings = (
      '???'
      '???'
      '??'
      '')
    Lines.WideStrings_UTF7 = (
      '+T2BZfVQX'
      '+Ti1W/U66'
      '+bEmL7Q'
      '')
    ParentFont = False
    TabOrder = 0
  end
end

Note that you don't need to install chinese for Delphi. It's your OS (Windows XP) wich must be able to manage asian languages and provide you the necessary keyboards and editors for input. Windows provide in the installation pack all the required tools for japanese, chinese and korean. You also can extend the service of those languages to all programs. If you do so, you can even type chinese in the standard Notepad.

If you past chinese chars in the Delphi 7 IDE and you receive the ???? result, it probably means that the required language (chinese) insn't installed on your system or that you don't use Unicode aware compo: in the standard Label of Delphi 5, for example, you can't paste chinese chars. If you do so, you'll get the ????? result. Use the tntCompo instead and all is OK


Sorry I forgot to give points for this one.
Thaks for everyones help
Bernani the points are yours