Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RouterTabs: Adds localization #5948

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading