Delphi程序实现多语言功能

xiaoxiao2021-02-28  55

在我们的产品需要适用于多种语言的用户时,多语言切换功能就非常重要了,今天又一朋友咨询多语言切换功能,下面我分享多年实践总结出来的经验及代码,希望对你有帮助,我这里是使用INI文件来存取,你也可以用XML或其他等,使用只时需要在基类Form(用于继承)的FORMSHOW中另一句代码 SwitchLanguage(self ,FLangID);// FLangID为你要切换的语言代码. 这样就实现多语言功能。

下面贴图为在程序中直接处理多语言内容界面. 即在Form的系统菜单添加一项"多语言资料"打开本Form多语言资料界面.

Procedure SwitchLanguage(FM: TForm; FLangID: String); Var   frmComponent: TComponent;   fInifile: TIniFile;   i, j: Integer;   Str: String; //读取控件的多语言内容,如果不存在就写默认值到INI文件, 这样你就可以直接在INI文件中修改多语言内容,这里默认为3种语言(英文,繁体中文,简体中文)   Function GetIniValue(fSection, fKey, fDefaultValue: String): String;   Var     s: String;   Begin     If Not fInifile.ValueExists(fSection, fKey) Then     Begin       If Not fInifile.ValueExists('LG1', fKey) Then         fInifile.WriteString('LG1', fKey, fDefaultValue);       If Not fInifile.ValueExists('LG2', fKey) Then         fInifile.WriteString('LG2', fKey, fDefaultValue);       If Not fInifile.ValueExists('LG3', fKey) Then         fInifile.WriteString('LG3', fKey, fDefaultValue);       Result := fDefaultValue;     End     Else     Begin       s := fInifile.ReadString(fSection, fKey, '');       If s = '' Then         Result := fDefaultValue       Else         Result := s;     End;   End; Begin   If FM = nil Then  exit;   fInifile := TIniFile.Create(LibDir + FM.Name + '.ini');   With fInifile, FM Do   Try     FM.Caption := GetIniValue('LG' + FLangID, FM.Name + '.C', FM.Caption);     For i := 0 To ComponentCount - 1 Do     Begin       If Components[i].Tag >= 999 Then   Continue; //某些控件不需要执行多语言切换,可以把该控件年Tag设置为大于999       frmComponent := Components[i];       If frmComponent Is TLabel Then       Begin

//设置Label的对齐方式,不同语言Label的长度不同,根据实际情况设置左对齐还是右对齐,这我里是默认为对齐         If (frmComponent As TLabel).tag = 0 Then //Label标签默认向右对齐           (frmComponent As TLabel).Alignment := taRightJustify         Else If (frmComponent As TLabel).tag = 1 Then //当Tag=1时,设置为向左对齐           (frmComponent As TLabel).Alignment := taCenter  //其他为居中对齐         Else           (frmComponent As TLabel).Alignment := taLeftJustify;         (frmComponent As TLabel).Caption := GetIniValue('LG' + FLangID, (frmComponent As TLabel).Name + '.C', (frmComponent As TLabel).Caption);       End

      Else If frmComponent Is TRzToolButton Then       Begin         (frmComponent As TRzToolButton).Caption := GetIniValue('LG' + FLangID, (frmComponent As TRzToolButton).Name + '.C', (frmComponent As TRzToolButton).Caption);

//当有Hint时如下面这行处理         (frmComponent As TRzToolButton).Hint := GetIniValue('LG' + FLangID, (frmComponent As TRzToolButton).Name + '.H', (frmComponent As TRzToolButton).Hint);       End             Else If frmComponent Is TMenuItem Then         (frmComponent As TMenuItem).Caption := GetIniValue('LG' + FLangID, (frmComponent As TMenuItem).Name + '.C', (frmComponent As TMenuItem).Caption)       Else If frmComponent Is TRzRadioButton Then         (frmComponent As TRzRadioButton).Caption := GetIniValue('LG' + FLangID, (frmComponent As TRzRadioButton).Name + '.C', (frmComponent As TRzRadioButton).Caption)

//有时不一定是多语言切换,也可以设置控件的统一风格,如下面对TDBGridEh的处理.       Else If frmComponent Is TDBGridEh Then       Begin         (frmComponent As TDBGridEh).TitleParams.MultiTitle := True; //设置表格多表头.         (frmComponent As TDBGridEh).ShowHint := true;         (frmComponent As TDBGridEh).SortLocal := True;         (frmComponent As TDBGridEh).DrawMemoText := True;  //显示备注信息         (frmComponent As TDBGridEh).STFilter.Visible := True;         (frmComponent As TDBGridEh).STFilter.Local := True;         (frmComponent as TDBGridEh).EvenRowColor := clMenu;         (frmComponent As TDBGridEh).RowHeight := 20;         (frmComponent As TDBGridEh).AllowedSelections := [gstRecordBookmarks, gstRectangle, gstColumns, gstAll];         (frmComponent As TDBGridEh).ColumnDefValues.Title.TitleButton := True;         (frmComponent As TDBGridEh).ColumnDefValues.ToolTips := true; //自动显示列不够宽时遮住的文字,必须打开ShowHint.         (frmComponent As TDBGridEh).IndicatorOptions := [gioShowRowIndicatorEh, gioShowRecNoEh];         (frmComponent As TDBGridEh).Options := [dgEditing, dgTitles, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgAlwaysShowSelection, dgConfirmDelete, dgCancelOnExit, dgMultiSelect];         (frmComponent as TDBGridEh).OptionsEh := [dghFixed3D, dghResizeWholeRightPart, dghHighlightFocus, dghRowHighlight, dghClearSelection, dghAutoSortMarking, dghDialogFind, dghShowRecNo, dghColumnResize, dghColumnMove, dghExtendVertLines, dghHotTrack, dghMultiSortMarking, dghDblClickOptimizeColWidth];       End

//把你系统中所用到的需要切换多语言的控件都列出在这个方法中并对相应处理, 还有如Combobox下拉框等,也是类似处理.     End;   Finally     FreeAndNil(fInifile);   End; End;

转载请注明原文地址: https://www.6miu.com/read-62288.html

最新回复(0)