{********************************************************************} { } { Developer Express Visual Component Library } { Express Cross Platform Library classes } { } { Copyright (c) 2000-2008 Developer Express Inc. } { ALL RIGHTS RESERVED } { } { The entire contents of this file is protected by U.S. and } { International Copyright Laws. Unauthorized reproduction, } { reverse-engineering, and distribution of all or any portion of } { the code contained in this file is strictly prohibited and may } { result in severe civil and criminal penalties and will be } { prosecuted to the maximum extent possible under the law. } { } { RESTRICTIONS } { } { THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES } { (DCU, OBJ, DLL, ETC.) ARE CONFIDENTIAL AND PROPRIETARY TRADE } { SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS } { LICENSED TO DISTRIBUTE THE EXPRESSCROSSPLATFORMLIBRARY AND ALL } { ACCOMPANYING VCL CONTROLS AS PART OF AN EXECUTABLE PROGRAM } { ONLY. } { } { THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED } { FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE } { COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE } { AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT } { AND PERMISSION FROM DEVELOPER EXPRESS INC. } { } { CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON } { ADDITIONAL RESTRICTIONS. } { } {********************************************************************} unit cxLocalization; interface uses Classes; type TcxLanguage = class(TCollectionItem) private FName: string; FDictionary: TStrings; FResStringNames: TStrings; public constructor Create(Collection: TCollection); override; destructor Destroy; override; procedure AddResString(const AResStringName, ATranslation: string); function GetTranslation(const AResStringName: string): string; published property Name: string read FName write FName; end; TcxLanguages = class(TCollection) private FActiveLanguage: Integer; FOwner: TPersistent; protected function GetOwner: TPersistent; override; property ActiveLanguage: Integer read FActiveLanguage write FActiveLanguage; public constructor Create(AOwner: TPersistent); function Add: TcxLanguage; procedure Translate; end; TcxLocalizer = class(TComponent) private FLanguages: TcxLanguages; function GetLanguage: Integer; procedure SetLanguage(AValue: Integer); procedure SetLanguages(AValue: TcxLanguages); public constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure Translate; published property Languages: TcxLanguages read FLanguages write SetLanguages; property Language: Integer read GetLanguage write SetLanguage; end; var FcxResourceStrings: TStrings; implementation uses SysUtils, cxClasses; { TcxLanguage } constructor TcxLanguage.Create(Collection: TCollection); begin inherited; FDictionary := TStringList.Create; FResStringNames := TStringList.Create; end; destructor TcxLanguage.Destroy; begin FreeAndNil(FResStringNames); FreeAndNil(FDictionary); inherited; end; procedure TcxLanguage.AddResString(const AResStringName, ATranslation: string); begin FResStringNames.AddObject(AResStringName, TObject(ATranslation)); // FDictionary.Add(ATranslation); end; function TcxLanguage.GetTranslation(const AResStringName: string): string; var AIndex: Integer; begin AIndex := FResStringNames.IndexOf(AResStringName); if AIndex > -1 then // Result := FDictionary[AIndex] Result := string(FResStringNames.Objects[AIndex]) else Result := ''; end; { TcxLanguages } constructor TcxLanguages.Create(AOwner: TPersistent); begin inherited Create(TcxLanguage); FActiveLanguage := -1; FOwner := AOwner; end; function TcxLanguages.Add: TcxLanguage; begin Result := TcxLanguage(inherited Add); end; procedure TcxLanguages.Translate; procedure InternalSetResourceString(AResString: Pointer; const AValue: string); begin if AValue <> '' then cxSetResourceString(AResString, AValue); end; var I: Integer; begin if (ActiveLanguage > -1) and (ActiveLanguage < Count) then begin for I := 0 to FcxResourceStrings.Count - 1 do InternalSetResourceString(FcxResourceStrings.Objects[I], TcxLanguage(Items[ActiveLanguage]).GetTranslation(FcxResourceStrings[I])); end; end; function TcxLanguages.GetOwner: TPersistent; begin Result := FOwner; end; { TcxLocalizer } constructor TcxLocalizer.Create(AOwner: TComponent); begin inherited; FLanguages := TcxLanguages.Create(Self); end; destructor TcxLocalizer.Destroy; begin FreeAndNil(FLanguages); inherited; end; procedure TcxLocalizer.Translate; begin FLanguages.Translate; end; function TcxLocalizer.GetLanguage: Integer; begin Result := FLanguages.ActiveLanguage; end; procedure TcxLocalizer.SetLanguage(AValue: Integer); begin FLanguages.ActiveLanguage := AValue; end; procedure TcxLocalizer.SetLanguages(AValue: TcxLanguages); begin if FLanguages <> AValue then FLanguages.Assign(Avalue) end; initialization FcxResourceStrings := TStringList.Create; finalization FreeAndNil(FcxResourceStrings); end.