Skip to content

Commit

Permalink
LangMgr: +AddMenuItem, AddComponentWithBoundLabel, overloaded AddStri…
Browse files Browse the repository at this point in the history
…ng, refactorings...
  • Loading branch information
jackdp committed Jun 6, 2022
1 parent 098cac6 commit 7905894
Showing 1 changed file with 78 additions and 31 deletions.
109 changes: 78 additions & 31 deletions Base/JPL.LangMgr.pas
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface

uses
SysUtils, Classes, Generics.Collections, IniFiles, Rtti, TypInfo,
StdCtrls, ExtCtrls, ActnList,
StdCtrls, ExtCtrls, ActnList, Menus,
Dialogs,
JPL.Strings, JPL.Conversion, JPL.IniFile, JPL.FileSearch, JPL.RTTI;

Expand All @@ -42,8 +42,6 @@ TLangIniListItem = record

{$region ' --- TLangIniList --- '}
TLangIniList = class
type
//TLangIniListItems = TList<TLangIniListItem>;
private
FItems: TLangIniListItems;
FIniInfoSectionName: string;
Expand Down Expand Up @@ -122,7 +120,8 @@ TLangSection = class
procedure LoadFromIniSection(const Ini: TJPIniFile);
procedure UpdateComponents;

procedure AddString(const KeyName, StrValue: string);
procedure AddString(const KeyName, StrValue: string); overload; // AddString('Key', 'Value);
procedure AddString(const KeyNameAndValue: string); overload; // AddString('Key=Value');
function GetString(const KeyName: string; Default: string = ''): string;


Expand All @@ -143,6 +142,8 @@ TLangSection = class
function AddComponentWithCaptionAndHint(Component: TComponent): TLangComponentItem;
function acch(Component: TComponent): TLangComponentItem; // calls AddComponentWithCaptionAndHint

function AddComponentWithBoundLabel(Component: TComponent): TLangComponentItem;


function AddLabel(const ALabel: TCustomLabel; bCaption: Boolean = True; bHint: Boolean = False): TLangComponentItem;
function AddStaticText(const StaticText: TStaticText; bCaption: Boolean = True; bHint: Boolean = False): TLangComponentItem;
Expand All @@ -151,6 +152,7 @@ TLangSection = class
procedure AddCheckBoxArray(Arr: array of TCheckBox; bCaption: Boolean = True; bHint: Boolean = True);
function AddRadioButton(const RadioButton: TRadioButton; bCaption: Boolean = True; bHint: Boolean = True): TLangComponentItem;
function AddLabeledEdit(const LabeledEdit: TCustomLabeledEdit; bLabelCaption: Boolean = True; bEditText: Boolean = False): TLangComponentItem;
function AddMenuItem(MenuItem: TMenuItem): TLangComponentItem;

function TryGetComponentItem(const KeyName: string; out Item: TLangComponentItem): Boolean;
function GetComponentProperty(const ComponentName, PropertyName: string; Default: string = ''): string;
Expand All @@ -165,6 +167,8 @@ TLangSection = class
property ComponentItemsCount: integer read GetComponentItemsCount;
property AllItemsCount: integer read GetAllItemsCount;
property ComponentNamePropertyNameSeparator: string read FComponentNamePropertyNameSeparator write SetComponentNamePropertyNameSeparator;

property StringItems: TLangStringItems read FLangStringItems;
end;
{$endregion TLangSection}

Expand Down Expand Up @@ -222,22 +226,29 @@ TLangMgr = class
{$endregion TLangMgr}


function fixs(s: string; p1: string = ''; p2: string = ''): string;
function fixs(SrcStr: string; SubStr1: string = ''; SubStr2: string = ''): string;


implementation


function fixs(s: string; p1: string = ''; p2: string = ''): string;
function fixs(SrcStr: string; SubStr1: string = ''; SubStr2: string = ''): string;
begin
s := StringReplace(s, '\n', ENDL, [rfReplaceAll]);
s := StringReplace(s, '\t', TAB, [rfReplaceAll]);
if p1 <> '' then
SrcStr := ReplaceAll(SrcStr, '\n', ENDL, True);
SrcStr := ReplaceAll(SrcStr, '\t', TAB, True);

//SrcStr := StringReplace(SrcStr, '\n', ENDL, [rfReplaceAll]);
//SrcStr := StringReplace(SrcStr, '\t', TAB, [rfReplaceAll]);

if SubStr1 <> '' then
begin
s := StringReplace(s, '%s', p1, []);
if p2 <> '' then s := StringReplace(s, '%s', p2, []);
SrcStr := ReplaceFirst(SrcStr, '%s', SubStr1, True);
if SubStr2 <> '' then SrcStr := ReplaceFirst(SrcStr, '%s', SubStr2, True);

//SrcStr := StringReplace(SrcStr, '%s', SubStr1, []);
//if SubStr2 <> '' then SrcStr := StringReplace(SrcStr, '%s', SubStr2, []);
end;
Result := s;
Result := SrcStr;
end;


Expand Down Expand Up @@ -295,9 +306,18 @@ function TLangSection.GetComponentProperty(const ComponentName, PropertyName: st
Result := Item.GetProperty(PropertyName, Default);
end;

function TLangSection.GetString(const KeyName: string; Default: string): string;
function TLangSection.GetString(const KeyName: string; Default: string = ''): string;
var
sKey, sDef: string;
begin
if not FLangStringItems.TryGetValue(KeyName, Result) then Result := Default;
sDef := Default;
if SplitStr(KeyName, sKey, sDef, '=') then
begin
if not FLangStringItems.TryGetValue(sKey, Result) then Result := sDef;
end
else
if not FLangStringItems.TryGetValue(KeyName, Result) then Result := sDef;

Result := fixs(Result);
end;

Expand Down Expand Up @@ -364,6 +384,8 @@ procedure TLangSection.UpdateComponents;
x: integer;
Obj: TObject;
begin
if FLangComponentItems.Count <= 0 then Exit;

for Key in FLangComponentItems.Keys do
begin
lci := FLangComponentItems[Key];
Expand All @@ -377,11 +399,9 @@ procedure TLangSection.UpdateComponents;
PropValue := lci.Properties[PropName];
PropValue := fixs(PropValue);

//if PropName.Contains('.') then
x := Pos('.', PropName);
if x > 0 then
begin
//x := Pos('.', PropName);
ObjName := Copy(PropName, 1, x - 1);
PropName2 := Copy(PropName, x + 1, Length(PropName));
if TryGetPropertyAsObject(lci.Component, ObjName, Obj) then SetPropertyText(Obj, PropName2, PropValue);
Expand Down Expand Up @@ -486,6 +506,16 @@ function TLangSection.acch(Component: TComponent): TLangComponentItem;
Result := AddComponentWithCaptionAndHint(Component);
end;

function TLangSection.AddComponentWithBoundLabel(Component: TComponent): TLangComponentItem;
var
s: string;
BoundLabelObj: TObject;
begin
if TryGetPropertyAsObject(Component, 'BoundLabel', BoundLabelObj) then s := GetPropertyText(BoundLabelObj, 'Caption', '')
else s := '';
Result := AddComponent(Component).AddProperty('BoundLabel.Caption', s);
end;

{$endregion Add component}


Expand All @@ -511,14 +541,20 @@ function TLangSection.AddStaticText(const StaticText: TStaticText; bCaption: Boo
if bHint then Result.AddProperty('Hint', StaticText.Hint);
end;

function TLangSection.AddLabeledEdit(const LabeledEdit: TCustomLabeledEdit; bLabelCaption, bEditText: Boolean): TLangComponentItem;
function TLangSection.AddLabeledEdit(const LabeledEdit: TCustomLabeledEdit; bLabelCaption: Boolean; bEditText: Boolean): TLangComponentItem;
begin
Result := AddComponent(LabeledEdit);
if bLabelCaption then Result.AddProperty('EditLabel.Caption', LabeledEdit.EditLabel.Caption);
if bEditText then Result.AddProperty('Text', LabeledEdit.Text);
end;

function TLangSection.AddRadioButton(const RadioButton: TRadioButton; bCaption, bHint: Boolean): TLangComponentItem;
function TLangSection.AddMenuItem(MenuItem: TMenuItem): TLangComponentItem;
begin
Result := AddComponent(MenuItem);
Result.AddProperty('Caption', MenuItem.Caption);
end;

function TLangSection.AddRadioButton(const RadioButton: TRadioButton; bCaption: Boolean; bHint: Boolean): TLangComponentItem;
begin
Result := AddComponent(RadioButton);
if bCaption then Result.AddProperty('Caption', RadioButton.Caption);
Expand Down Expand Up @@ -553,6 +589,14 @@ procedure TLangSection.AddString(const KeyName, StrValue: string);
FLangStringItems.AddOrSetValue(KeyName, StrValue);
end;

procedure TLangSection.AddString(const KeyNameAndValue: string);
var
sKey, sValue: string;
begin
if not SplitStr(KeyNameAndValue, sKey, sValue, '=') then Exit;
AddString(sKey, sValue);
end;

function TLangSection.AsInfoStr: string;
var
s, Key, Value: string;
Expand Down Expand Up @@ -606,11 +650,24 @@ function TLangComponentItem.ap(const PropertyName, PropertyString: string): TLan

function TLangComponentItem.AddProperty(const PropertyName: string): TLangComponentItem;
var
s: string;
s, SubComponentName, SubcomponentPropertyName: string;
SubComponentObj: TObject;
x: integer;
begin
Result := Self;
if not Assigned(Self.Component) then Exit;
s := GetPropertyText(Self.Component, PropertyName, INVALID_COMPONENT_PROP_STR);

x := Pos('.', PropertyName);
if x > 0 then
begin
SubComponentName := Copy(PropertyName, 1, x - 1);
SubcomponentPropertyName := Copy(PropertyName, x + 1, Length(PropertyName));
if not TryGetPropertyAsObject(Self.Component, SubComponentName, SubComponentObj) then Exit;
s := GetPropertyText(SubComponentObj, SubcomponentPropertyName, INVALID_COMPONENT_PROP_STR);
end
else
s := GetPropertyText(Self.Component, PropertyName, INVALID_COMPONENT_PROP_STR);

if s <> INVALID_COMPONENT_PROP_STR then Result := AddProperty(PropertyName, s);
end;

Expand Down Expand Up @@ -766,8 +823,6 @@ function TLangMgr.GetLanguageCount: integer;
Result := FLangIniList.Count;
end;



procedure TLangMgr.GetLanguageFileNames(const Strings: TStrings);
begin
FLangIniList.GetLanguageFileNames(Strings);
Expand Down Expand Up @@ -808,8 +863,6 @@ function TLangMgr.GetSectionByName(const SectionName: string): TLangSection;
end;
end;



procedure TLangMgr.RemoveSection(LangSection: TLangSection);
begin
FLangSections.Remove(LangSection);
Expand Down Expand Up @@ -842,7 +895,7 @@ procedure TLangMgr.LoadStringsFromFile(const IniFileName: string);
for Section in FLangSections do
begin
Section.LoadFromIniSection(Ini);
Section.UpdateComponents; //ShowMessage(Section.SectionName + ' ' + IniFileName);
Section.UpdateComponents;
end;
Ini.UpdateFileOnExit := False;
finally
Expand Down Expand Up @@ -920,8 +973,6 @@ procedure TLangMgr.AddFilesFromDir(const Dir: string; Ext: string);





{$region ' ------------------------------ TLangIniList ----------------------------------- '}

constructor TLangIniList.Create;
Expand Down Expand Up @@ -1086,10 +1137,6 @@ procedure TLangIniList.AddFilesFromDir(const Dir: string; Ext: string = 'ini');







{ TLangIniListItem }

function TLangIniListItem.AsInfoStr(Indent: string): string;
Expand Down

0 comments on commit 7905894

Please sign in to comment.