AspNetCore.RequestLocalizationPipeline 1.0.6

dotnet add package AspNetCore.RequestLocalizationPipeline --version 1.0.6
                    
NuGet\Install-Package AspNetCore.RequestLocalizationPipeline -Version 1.0.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AspNetCore.RequestLocalizationPipeline" Version="1.0.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AspNetCore.RequestLocalizationPipeline" Version="1.0.6" />
                    
Directory.Packages.props
<PackageReference Include="AspNetCore.RequestLocalizationPipeline" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AspNetCore.RequestLocalizationPipeline --version 1.0.6
                    
#r "nuget: AspNetCore.RequestLocalizationPipeline, 1.0.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=AspNetCore.RequestLocalizationPipeline&version=1.0.6
                    
Install as a Cake Addin
#tool nuget:?package=AspNetCore.RequestLocalizationPipeline&version=1.0.6
                    
Install as a Cake Tool

AspNetCore.RequestLocalizationPipeline

A helper project to add route localization in .NET Core Web API projects.

You can use it by adding it as a separate project and reference it in the Web.API project you have, or as a nuget package:

Install-Package AspNetCore.RequestLocalizationPipeline

What is it for ?

This package will help you pass culture through URL like this:

https://mydomain/en/api/myControler
// OR like this:
https://mydomain/en-US/api/myController

The parts before the myController will be considered as [route convention|https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-3.0], you can set the culture route key where ever you want in the convention area, so it'll be applied to all your controllers.

notes: the default provider that comes with AspNetCore doesn't do the work.

How to use it:

There're couple of snippets you need to add to your startup.cs before you'll be able to use this package.

  1. Specify the route key you want to use for culture:
const string langKey = "lang";
  1. Define a route convention as a readonly field in startup:
readonly string ROUTE_CONVENTION = "{" + langKey + "}/api";

Adding a route convention to your controllers, where a convention is a prefix that will be prepended to all the routes in your project.

  1. In method ConfigureServices we have to prepare the localization options and add it as a singleton to be used later:
public void ConfigureServices(IServiceCollection services)
{
	// An extension method from Microsoft.Extensions.DependencyInjection to add required services for localization.
	services.AddLocalization(options => options.ResourcesPath = "Resources");

	// An extension method from the package to prepare the options and add the object as Singleton in DI.
	// First parameter is the same route key you defined in step 1.
	// Second parameter is the default culture you want your api to use in case no culture passed through URL.
	services.AddLocalizationOptions(langKey);
}

Notes:

  • AddLocalizationOptions will add the RouteDataRequestCultureProvider in index 0 of the list RequestCultureProviders.
  • By default the package adds support for two languages (en, ar), if you need to add more you do this by filling this list before calling the method AddLocalizationOptions like this:
      LocalizationOptions.SupportedCultures.Add(new CultureInfo("fr-FR"));
      // and you can add more languages
      services.AddLocalizationOptions(langKey);
    
  1. Now we will add the localization pipeline as a global action filter in your project:
services.AddMvc(opts =>
    {
		...
		// Make sure it's set before any other filters ,if you have, if you want to use the culture in them.
        opts.AddLocalizationPipeLine(ROUTE_CONVENTION);
		...
    })
  1. In Configure method, we'll add a rewriter to make sure we accept requests that don't have any culutre in the position of the specified culture key:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
	app.UseDefaultCultureRewrite(ROUTE_CONVENTION);
}
  1. In Configure method, we'll add a rewriter to make sure we accept cultures in two letters (not only ISO 3 letters):
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
	app.UseDefaultCultureRewrite(ROUTE_CONVENTION)
		.UseTwoLettersCultureRewrite(ROUTE_CONVENTION);
}
Note:

If you want to make sure if it's working, comment app.UseMvc() in method Configure and add this temporarly:

app.Run(context => context.Response.WriteAsync(
            $"Rewritten or Redirected Url: " +
            $"{context.Request.Path + context.Request.QueryString}"));

At the end, this is my first project on github, I may did something wrong in it, or it's not clean code, please advice and feel free to contribute.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.2 is compatible.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.6 802 10/11/2019