From ece265e4d2d82ffc8e82aa029382a5dcd5b2d820 Mon Sep 17 00:00:00 2001 From: tesar-tech Date: Wed, 22 Jan 2025 12:43:32 +0100 Subject: [PATCH] Adds localization to RoutaterTabs name. --- Source/Extensions/Blazorise.Components/Config.cs | 8 +++++++- .../Options/RouterTabsOptions.cs | 15 +++++++++++++++ .../Blazorise.Components/RouterTabs.razor | 6 +++--- .../RouterTabsPageAttribute.cs | 14 ++++++++++++++ .../Blazorise.Components/RouterTabsService.cs | 12 ++++++++++-- 5 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 Source/Extensions/Blazorise.Components/Options/RouterTabsOptions.cs diff --git a/Source/Extensions/Blazorise.Components/Config.cs b/Source/Extensions/Blazorise.Components/Config.cs index 855ec62a3c..5db251ac0e 100644 --- a/Source/Extensions/Blazorise.Components/Config.cs +++ b/Source/Extensions/Blazorise.Components/Config.cs @@ -1,4 +1,6 @@ #region Using directives + +using System; using Microsoft.Extensions.DependencyInjection; #endregion @@ -13,10 +15,14 @@ public static class Config /// Adds the Blazorise Router Tabs services to the service collection. /// /// + /// /// - public static IServiceCollection AddBlazoriseRouterTabs( this IServiceCollection serviceCollection ) + public static IServiceCollection AddBlazoriseRouterTabs( this IServiceCollection serviceCollection, Action options = null ) { serviceCollection.AddTransient(); + var opt = new RouterTabsOptions(); + options?.Invoke( opt ); + serviceCollection.AddSingleton( _ => opt ); return serviceCollection; } diff --git a/Source/Extensions/Blazorise.Components/Options/RouterTabsOptions.cs b/Source/Extensions/Blazorise.Components/Options/RouterTabsOptions.cs new file mode 100644 index 0000000000..fe45234d1c --- /dev/null +++ b/Source/Extensions/Blazorise.Components/Options/RouterTabsOptions.cs @@ -0,0 +1,15 @@ +using System; + +namespace Blazorise.Components; + +/// +/// Options for RouterTabs component +/// +public class RouterTabsOptions +{ + /// + /// Func used for localizing router tabs names. + /// In: Name key, Out: Localized name + /// + public Func NamesLocalizer { 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..67f1a0574c 100644 --- a/Source/Extensions/Blazorise.Components/RouterTabs.razor +++ b/Source/Extensions/Blazorise.Components/RouterTabs.razor @@ -6,8 +6,8 @@ @foreach ( var routerTab in RouterTabsService.Tabs ) { - - @routerTab.Name + + @routerTab.LocalizedNameOrName @if ( routerTab.Closeable ) { @@ -18,7 +18,7 @@ @foreach ( var routerTab in RouterTabsService.Tabs ) { - + @routerTab.Body } diff --git a/Source/Extensions/Blazorise.Components/RouterTabsPageAttribute.cs b/Source/Extensions/Blazorise.Components/RouterTabsPageAttribute.cs index 23d44f9380..27f43c511b 100644 --- a/Source/Extensions/Blazorise.Components/RouterTabsPageAttribute.cs +++ b/Source/Extensions/Blazorise.Components/RouterTabsPageAttribute.cs @@ -29,6 +29,11 @@ public class RouterTabsPageAttribute : Attribute /// public readonly bool Closeable; + /// + /// Localization key for retrieving name of the tab + /// + public readonly string NameLocalizationKey; + public RouterTabsPageAttribute( string Name, string TabClass = "", string TabPanelClass = "", bool Closeable = true ) { this.Name = Name; @@ -36,4 +41,13 @@ public RouterTabsPageAttribute( string Name, string TabClass = "", string TabPan this.TabPanelClass = TabPanelClass; this.Closeable = Closeable; } + + public RouterTabsPageAttribute( string NameLocalizationKey, bool Closeable = true, string TabClass = "", string TabPanelClass = "" ) + { + this.NameLocalizationKey = NameLocalizationKey; + 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..aa725639e5 100644 --- a/Source/Extensions/Blazorise.Components/RouterTabsService.cs +++ b/Source/Extensions/Blazorise.Components/RouterTabsService.cs @@ -17,7 +17,9 @@ internal class RouterTabsItem public string TabClass { get; set; } public string TabPanelClass { get; set; } public bool Closeable { get; set; } = true; - + public string NameLocalizationKey { get; set; } + public string LocalizedName { get; set; } + public string LocalizedNameOrName => LocalizedName ?? Name; } /// @@ -37,13 +39,16 @@ public class RouterTabsService internal IReadOnlyCollection Tabs => tabs.AsReadOnly(); + private RouterTabsOptions options; + #endregion #region Constructors - public RouterTabsService( NavigationManager navigationManager ) + public RouterTabsService( NavigationManager navigationManager, RouterTabsOptions options ) { this.navigationManager = navigationManager; + this.options = options; } #endregion @@ -99,6 +104,8 @@ internal void TrySetRouteData( RouteData routeData ) if ( routeData is not null ) { SetRouterTabsItemFromPageAttribute( routerTabsItem, routeData.PageType ); + if ( !string.IsNullOrWhiteSpace(routerTabsItem.NameLocalizationKey)) + routerTabsItem.LocalizedName = options?.NamesLocalizer.Invoke( routerTabsItem.NameLocalizationKey ); routerTabsItem.Body ??= CreateRouterTabsItemBody( routeData ); routerTabsItem.TypeName = routeData.PageType.FullName; if ( string.IsNullOrWhiteSpace( routerTabsItem.Name ) ) @@ -130,6 +137,7 @@ private void SetRouterTabsItemFromPageAttribute( RouterTabsItem pageItem, Type p if ( routerTabsPageAttr is not null ) { pageItem.Name = routerTabsPageAttr.Name; + pageItem.NameLocalizationKey = routerTabsPageAttr.NameLocalizationKey; pageItem.TabClass = routerTabsPageAttr.TabClass; pageItem.TabPanelClass = routerTabsPageAttr.TabPanelClass; pageItem.Closeable = routerTabsPageAttr.Closeable;