秋·风

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
在界面实时切换语言,代码挺简单的,一看就明白:
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  DefaultTranslator,LCLTranslator,Translations
  ,LazUTF8
  {$IFDEF linux}
  , Unix
  {$else}
  ,windows
  {$endif}
  ;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    ComboBox1: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    ToggleBox1: TToggleBox;
    procedure ComboBox1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;

implementation


{$R *.lfm}
function GetOSLanguage: string;
var
  l, fbl: string;
  {$IFDEF LCLCarbon}
  theLocaleRef: CFLocaleRef;
  locale: CFStringRef;
  buffer: StringPtr;
  bufferSize: CFIndex;
  encoding: CFStringEncoding;
  success: boolean;
  {$ENDIF}
begin
  {$IFDEF LCLCarbon}
  theLocaleRef := CFLocaleCopyCurrent;
  locale := CFLocaleGetIdentifier(theLocaleRef);
  encoding := 0;
  bufferSize := 256;
  buffer := new(StringPtr);
  success := CFStringGetPascalString(locale, buffer, bufferSize, encoding);
  if success then
    l := string(buffer^)
  else
    l := '';
  fbl := Copy(l, 1, 2);
  dispose(buffer);
  {$ELSE}
  {$IFDEF LINUX}
    fbl := Copy(GetEnvironmentVariable('LANG'), 1, 2);
    {$ELSE}
    lazGetLanguageIDs(l, fbl);
    {$ENDIF}
  {$ENDIF}
  Result := fbl;
end;
{ TForm1 }

procedure TForm1.ComboBox1Change(Sender: TObject);
var
  lan:String;
begin
  if ComboBox1.Text='en' then
    lan:='en'
  else
    lan:='zh_CN';
  SetDefaultLang(lan,'','');
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  lan:String;
begin
  lan:=GetOSLanguage; //取当前系统语言
if lan='zh' then
  begin
    ComboBox1.ItemIndex:=ComboBox1.Items.IndexOf ('zh_CN(中文)');
    lan:='zh_CN';
  end
  else
    lan:='en';
  SetDefaultLang(lan,'','');//切换为当前系统使用的语言
end;

end.

 

 

posted on 2025-06-14 21:18  秋·风  阅读(186)  评论(0)    收藏  举报