diff --git a/Source/Extensions/Blazorise.Components/Config.cs b/Source/Extensions/Blazorise.Components/Config.cs index 855ec62a3c..e0b1168fd3 100644 --- a/Source/Extensions/Blazorise.Components/Config.cs +++ b/Source/Extensions/Blazorise.Components/Config.cs @@ -1,23 +1,29 @@ #region Using directives +using System; using Microsoft.Extensions.DependencyInjection; #endregion namespace Blazorise.Components; /// -/// Extension methods for building the blazorise options. +/// Provides extension methods for configuring Blazorise-related services. /// public static class Config { /// /// Adds the Blazorise Router Tabs services to the service collection. /// - /// - /// - public static IServiceCollection AddBlazoriseRouterTabs( this IServiceCollection serviceCollection ) + /// The to which the Router Tabs services will be added. + /// An optional configuration action to customize the . + /// The updated instance. + public static IServiceCollection AddBlazoriseRouterTabs( this IServiceCollection serviceCollection, Action options = null ) { serviceCollection.AddTransient(); + var routerTabsOptions = new RouterTabsOptions(); + options?.Invoke( routerTabsOptions ); + serviceCollection.AddSingleton( _ => routerTabsOptions ); + return serviceCollection; } } \ No newline at end of file diff --git a/Source/Extensions/Blazorise.Components/Options/RouterTabsOptions.cs b/Source/Extensions/Blazorise.Components/Options/RouterTabsOptions.cs new file mode 100644 index 0000000000..542fbb02e7 --- /dev/null +++ b/Source/Extensions/Blazorise.Components/Options/RouterTabsOptions.cs @@ -0,0 +1,31 @@ +#region Using directives +using Blazorise.Localization; +#endregion + +namespace Blazorise.Components; + +/// +/// Provides configuration options for the component. +/// +public class RouterTabsOptions +{ + /// + /// A function used to localize router tab names. + /// + /// + /// This function allows localization of router tab names based on a key. + /// Behavior: + /// + /// + /// Accepts a string parameter representing the name key. + /// + /// + /// Returns the localized name as a string. + /// + /// + /// Returns null if no localization is available. + /// + /// + /// + public TextLocalizerHandler NameLocalizer { get; set; } +} \ No newline at end of file diff --git a/Source/Extensions/Blazorise.Components/RouterTabs.razor b/Source/Extensions/Blazorise.Components/RouterTabs.razor index 2c9559c996..9b645cc8a0 100644 --- a/Source/Extensions/Blazorise.Components/RouterTabs.razor +++ b/Source/Extensions/Blazorise.Components/RouterTabs.razor @@ -6,8 +6,10 @@ @foreach ( var routerTab in RouterTabsService.Tabs ) { - - @routerTab.Name + var name = GetTabName( routerTab ); + + + @name @if ( routerTab.Closeable ) { @@ -18,7 +20,9 @@ @foreach ( var routerTab in RouterTabsService.Tabs ) { - + var name = GetTabName( routerTab ); + + @routerTab.Body } diff --git a/Source/Extensions/Blazorise.Components/RouterTabs.razor.cs b/Source/Extensions/Blazorise.Components/RouterTabs.razor.cs index d4e4766012..21c8543779 100644 --- a/Source/Extensions/Blazorise.Components/RouterTabs.razor.cs +++ b/Source/Extensions/Blazorise.Components/RouterTabs.razor.cs @@ -1,5 +1,6 @@ #region Using directives using System; +using Blazorise.Localization; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Routing; #endregion @@ -7,7 +8,7 @@ namespace Blazorise.Components; /// -/// Component that manages the router tabs. +/// A component that manages router-based tab navigation. /// public partial class RouterTabs : ComponentBase, IDisposable { @@ -19,6 +20,7 @@ public partial class RouterTabs : ComponentBase, IDisposable #region Methods + /// protected override void OnInitialized() { base.OnInitialized(); @@ -33,23 +35,34 @@ protected override void OnInitialized() RouterTabsService.TrySetRouteData( RouteData ); } + /// + /// Closes a specified router tab. + /// + /// The tab item to close. internal void CloseTab( RouterTabsItem routerTabsItem ) { - if ( RouterTabsService is not null ) - RouterTabsService.CloseRouterTab( routerTabsItem ); + RouterTabsService?.CloseRouterTab( routerTabsItem ); } - private void OnLocationChanged( object o, LocationChangedEventArgs _ ) + /// + /// Handles navigation location changes and updates the router tabs accordingly. + /// + /// The sender of the event. + /// The event arguments containing location change details. + private void OnLocationChanged( object sender, LocationChangedEventArgs args ) { - if ( RouterTabsService is not null ) - RouterTabsService.TrySetRouteData( RouteData ); + RouterTabsService?.TrySetRouteData( RouteData ); } + /// + /// Invokes a state change when the router tabs service state is updated. + /// private void OnStateHasChanged() { InvokeAsync( StateHasChanged ); } + /// protected virtual void Dispose( bool disposing ) { if ( !disposedValue ) @@ -78,15 +91,39 @@ public void Dispose() GC.SuppressFinalize( this ); } + /// + /// Gets the localized name of a specified tab. + /// + /// The tab item for which to retrieve the localized name. + /// The localized name of the tab. + private string GetTabName( RouterTabsItem tab ) + { + return NameLocalizer?.Invoke( tab.Name ) ?? tab.LocalizedNameOrName; + } + #endregion #region Properties + /// + /// Provides access to the navigation manager for handling routing events. + /// [Inject] public NavigationManager NavigationManager { get; set; } + /// + /// Provides access to the router tabs service for managing tab state. + /// [Inject] public RouterTabsService RouterTabsService { get; set; } + /// + /// Provides the current route data for the component. + /// [CascadingParameter] public RouteData RouteData { get; set; } + /// + /// A function used to localize tab names. + /// + [Parameter] public TextLocalizerHandler NameLocalizer { get; set; } + #endregion } \ No newline at end of file diff --git a/Source/Extensions/Blazorise.Components/RouterTabsPageAttribute.cs b/Source/Extensions/Blazorise.Components/RouterTabsPageAttribute.cs index 23d44f9380..213a8b11a0 100644 --- a/Source/Extensions/Blazorise.Components/RouterTabsPageAttribute.cs +++ b/Source/Extensions/Blazorise.Components/RouterTabsPageAttribute.cs @@ -5,35 +5,51 @@ namespace Blazorise.Components; /// -/// Attribute that sets the Router Tabs Page attributes +/// An attribute that defines configuration settings for a Router Tabs page. /// public class RouterTabsPageAttribute : Attribute { /// - /// Sets the name of the router tab. + /// Initializes a new instance of the class. /// + /// + /// The name of the router tab. + /// This can be used as a key for localization. + /// If left empty or null, the RouteData.PageType + /// will be used as the tab name. + /// + /// The CSS class to apply to the router tab. + /// The CSS class to apply to the router tab panel. + /// Indicates whether the router tab can be closed. Defaults to true. + public RouterTabsPageAttribute( string Name, string TabClass = "", string TabPanelClass = "", bool Closeable = true ) + { + this.Name = Name; + this.TabClass = TabClass; + this.TabPanelClass = TabPanelClass; + this.Closeable = Closeable; + } + + /// + /// Gets the name of the router tab. + /// + /// + /// - This name can be used as a key for localization. + /// - If left empty or null, the system will use RouteData.PageType. + /// public readonly string Name; /// - /// Sets the css class of the router tab. + /// Gets the CSS class assigned to the router tab. /// public readonly string TabClass; /// - /// Sets the css class of the router tab panel. + /// Gets the CSS class assigned to the router tab panel. /// public readonly string TabPanelClass; /// - /// Whether the router tab is closeable. + /// Gets a value indicating whether the router tab is closeable. /// public readonly bool Closeable; - - public RouterTabsPageAttribute( string Name, string TabClass = "", string TabPanelClass = "", bool Closeable = true ) - { - this.Name = Name; - this.TabClass = TabClass; - this.TabPanelClass = TabPanelClass; - this.Closeable = Closeable; - } } \ No newline at end of file diff --git a/Source/Extensions/Blazorise.Components/RouterTabsService.cs b/Source/Extensions/Blazorise.Components/RouterTabsService.cs index 50bd5fc062..1ce72d9c82 100644 --- a/Source/Extensions/Blazorise.Components/RouterTabsService.cs +++ b/Source/Extensions/Blazorise.Components/RouterTabsService.cs @@ -17,7 +17,8 @@ internal class RouterTabsItem public string TabClass { get; set; } public string TabPanelClass { get; set; } public bool Closeable { get; set; } = true; - + public string LocalizedName { get; set; } + public string LocalizedNameOrName => LocalizedName ?? Name; } /// @@ -37,13 +38,16 @@ public class RouterTabsService internal IReadOnlyCollection Tabs => tabs.AsReadOnly(); + private readonly RouterTabsOptions options; + #endregion #region Constructors - public RouterTabsService( NavigationManager navigationManager ) + public RouterTabsService( NavigationManager navigationManager, RouterTabsOptions options ) { this.navigationManager = navigationManager; + this.options = options; } #endregion @@ -99,6 +103,7 @@ internal void TrySetRouteData( RouteData routeData ) if ( routeData is not null ) { SetRouterTabsItemFromPageAttribute( routerTabsItem, routeData.PageType ); + routerTabsItem.LocalizedName ??= options?.NameLocalizer?.Invoke( routerTabsItem.Name ); routerTabsItem.Body ??= CreateRouterTabsItemBody( routeData ); routerTabsItem.TypeName = routeData.PageType.FullName; if ( string.IsNullOrWhiteSpace( routerTabsItem.Name ) )