Skip to content

Commit

Permalink
Adds localization to RoutaterTabs name.
Browse files Browse the repository at this point in the history
  • Loading branch information
tesar-tech committed Jan 22, 2025
1 parent e00ca70 commit ece265e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
8 changes: 7 additions & 1 deletion Source/Extensions/Blazorise.Components/Config.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#region Using directives

using System;
using Microsoft.Extensions.DependencyInjection;
#endregion

Expand All @@ -13,10 +15,14 @@ public static class Config
/// Adds the Blazorise Router Tabs services to the service collection.
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IServiceCollection AddBlazoriseRouterTabs( this IServiceCollection serviceCollection )
public static IServiceCollection AddBlazoriseRouterTabs( this IServiceCollection serviceCollection, Action<RouterTabsOptions> options = null )
{
serviceCollection.AddTransient<RouterTabsService>();
var opt = new RouterTabsOptions();
options?.Invoke( opt );
serviceCollection.AddSingleton( _ => opt );

return serviceCollection;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Blazorise.Components;

/// <summary>
/// Options for RouterTabs component
/// </summary>
public class RouterTabsOptions
{
/// <summary>
/// Func used for localizing router tabs names.
/// In: Name key, Out: Localized name
/// </summary>
public Func<string, string> NamesLocalizer { get; set; }
}
6 changes: 3 additions & 3 deletions Source/Extensions/Blazorise.Components/RouterTabs.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<Items>
@foreach ( var routerTab in RouterTabsService.Tabs )
{
<Tab @key=@routerTab.Name Name="@routerTab.Name" Class="@routerTab.TabClass">
@routerTab.Name
<Tab @key=@routerTab.LocalizedNameOrName Name="@routerTab.LocalizedNameOrName" Class="@routerTab.TabClass">
@routerTab.LocalizedNameOrName
@if ( routerTab.Closeable )
{
<CloseButton AutoClose=false Clicked="@(() => CloseTab(routerTab))" Margin="Margin.Is2.FromStart" />
Expand All @@ -18,7 +18,7 @@
<Content>
@foreach ( var routerTab in RouterTabsService.Tabs )
{
<TabPanel @key=@routerTab.Name Name="@routerTab.Name" Class="@routerTab.TabPanelClass">
<TabPanel @key=@routerTab.LocalizedNameOrName Name="@routerTab.LocalizedNameOrName" Class="@routerTab.TabPanelClass">
@routerTab.Body
</TabPanel>
}
Expand Down
14 changes: 14 additions & 0 deletions Source/Extensions/Blazorise.Components/RouterTabsPageAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,25 @@ public class RouterTabsPageAttribute : Attribute
/// </summary>
public readonly bool Closeable;

/// <summary>
/// Localization key for retrieving name of the tab
/// </summary>
public readonly string NameLocalizationKey;

public RouterTabsPageAttribute( string Name, string TabClass = "", string TabPanelClass = "", bool Closeable = true )
{
this.Name = Name;
this.TabClass = TabClass;
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;
}

}
12 changes: 10 additions & 2 deletions Source/Extensions/Blazorise.Components/RouterTabsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
Expand All @@ -37,13 +39,16 @@ public class RouterTabsService

internal IReadOnlyCollection<RouterTabsItem> 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
Expand Down Expand Up @@ -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 ) )
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ece265e

Please sign in to comment.