This repository is no longer being maintained. Please use the AspNetCore.Mvc.Routing.Localization package instead.
The LocalizedRouting package is the extension for ASP.NET Core Localization which provides you localized routing.
yoursite.com/cs-CZ/clanky
yoursite.com/en-US/articles
The extension provides you several services:
- LocalizedRouteAttribute
- ILocalizedRoutingProvider
- ILocalizedRoutingDynamicRouteValueResolver
- LocalizedRoutingAnchorTagHelper
This is an extension for ASP.NET Core Localization. You need to use RouteDataRequestCultureProvider for your culture provider.
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
Check out the contributing page to see the best places to file issues, start discussions, and begin contributing.