ASP.NET Core 1.0 Library and nuget package to translate and replace tokens in routes.
Based of Strathweb blog post but repurposed and compatible with ASP.NET Core 1.0 RTM.
If you want to see the meat of the implementation, it's here LocalizedRouteConvetion
Install-Package LocalizedRoutes
or add to project.json
under dependencies:
"LocalizedRoutes":"1.0.3"
This library will allow you to specify attribroute and then replace them with localized versions on app startup:
[LocalizedRoutes("how-it-works", Name = "how")
public IActionResult Index() { ... }
You can store your translations however you want, for example in a dictionary or in a json file (you have to write the provider, look at the example:
{"how":"hur-det-fungerar"}
Add it to your Mvc service in Startup.cs
.
// Get your translations however you want by implementing IRouteLocalizationsAccessor
var routeLocalizer = new MyJsonRoutesLocalizer(Configuration["Culture"], Configuration["Theme"]);
var routes = routeLocalizer.GetLocalizations();
services.AddMvc(o => o.AddLocalizedRoutes(routes))
Your route would then be translated to: hur-det-fungerar, as if you had instrumented your action like so:
[Route("hur-det-fungerar")]
public IActionResult Index() { ... }
You can write advanced routes and just store what you want to translate by using <token>
.
[LocalizedRoutes("<account>/{id:int}/<details>", Name = "<how>")
public IActionResult Index() { ... }
Swedish:
Translation file: {"<how>":"<konto>,<detaljer>"}
Will render a route of: konto/{id:int}/detaljer
.
You can replace a lot of the functionality by changing the configuration and creating your own implementaions of Interfaces:
IRouteTokenReplacer
- Create your own token pattern, if you dont want to use the <token>
and <token>,<token>
pattern.IRouteLocalizationsAccessor
- Store your translation in any way you want, I use a JsonFile, but you could use a Dictionary, .ini-file or whatever.I built this library to power a MVC site that runs them same code for multiple sites. I use this code to theme my URLs.
What it does:
What it does not support:
However, the list above should be easy to support and if you could probably fork this lib to make it happen. Look at Starthwebs blog post to get the blueprint.
More documentation really needs to be written. Also look at the current Issue list to see more functionality / missing patterns.