Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Kentico/aspnetcore-localizedrouting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

No Maintenance Intended

⚠️ Deprecation Notice

This repository is no longer being maintained. Please use the AspNetCore.Mvc.Routing.Localization package instead.


Kentico AspNetCore LocalizedRouting

Build status

Introduction

The LocalizedRouting package is the extension for ASP.NET Core Localization which provides you localized routing.

Example

yoursite.com/cs-CZ/clanky  
yoursite.com/en-US/articles

The extension provides you several services:

  • LocalizedRouteAttribute
  • ILocalizedRoutingProvider
  • ILocalizedRoutingDynamicRouteValueResolver
  • LocalizedRoutingAnchorTagHelper

Getting started

This is an extension for ASP.NET Core Localization. You need to use RouteDataRequestCultureProvider for your culture provider.

Setup

You should install Kentico.AspNetCore.LocalizedRouting with NuGet:

Install-Package Kentico.AspNetCore.LocalizedRouting

You will need to configure services via IServiceCollection extension method.

services.AddLocalizedRouting();

You need to create a custom DynamicRouteValueTransformer and use ILocalizedRoutingDynamicRouteValueResolver service in TransforAsync method to resolve the translated route.

public class CustomLocalizedRoutingTranslationTransformer : DynamicRouteValueTransformer
{
    private ILocalizedRoutingDynamicRouteValueResolver _localizedRoutingDynamicRouteValueResolver;
    public CustomLocalizedRoutingTranslationTransformer(ILocalizedRoutingDynamicRouteValueResolver localizedRoutingDynamicRouteValueResolver)
    {
        _localizedRoutingDynamicRouteValueResolver = localizedRoutingDynamicRouteValueResolver
    }
    public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
    {
        return await _localizedRoutingDynamicRouteValueResolver.ResolveAsync(values);
    }
}

Register this service to DI.

services.AddSingleton<CustomLocalizedRoutingTranslationTransformer>();

Use CustomLocalizedRoutingTranslationTransformer in MapDynamicControllerRoute. For correct working, your routing template must contain these parameters - {culture}/{controller}/{action}.

app.UseEndpoints(endpoints =>
{
    endpoints.MapDynamicControllerRoute<CustomLocalizedRoutingTranslationTransformer>("{culture}/{controller}/{action}/{id?}");
    endpoints.MapControllerRoute("default", "{culture=en-US}/{controller=Home}/{action=Index}/{id?}");
});

Use LocalizedRouteAttribute on your controllers and action methods for specifying the translated route.

[LocalizedRoute("en-US", "articles")]
[LocalizedRoute("cs-CZ", "clanky")]
public class ArticlesController : Controller
{
    [LocalizedRoute("en-US", "index")]
    [LocalizedRoute("cs-CZ", "uvod")]
    public async Task<IActionResult> Index()
    {
        return View();
    }

}

If you need translated links in application, you have to also register LocalizedRoutingAnchorTagHelper instead of the default implementation.

@addTagHelper *, Kentico.AspNetCore.LocalizedRouting
@removeTagHelper Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper, Microsoft.AspNetCore.Mvc.TagHelpers

Feedback & Contributing

Check out the contributing page to see the best places to file issues, start discussions, and begin contributing.

About

ASP.NET Core extensions that provide localized routing capabilities.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages