3
votes

I am developing an ASP.NET Core 3.1 MVC website that uses domain names to determine the culture for localization.

For example, all requests going to the domain example.com will have the en-US culture, whereas all requests going to the domain example.org will have the de-DE culture.

I modified my previously unlocalized website to use localization in views, etc.. I have utilized an own RequestCultureProvider implementation to set the culture based on the domain name. The result was the following:

  • https://example.com/Products/My-Application/Help is using en-US as the culture and have English texts.
  • https://example.org/Products/My-Application/Help is using de-DE as the culture and have German texts.

Note: Products is the controller, My-Application the name of the application (URL slug from the database), and Help is the action within the Products controller. The URL structure is the actual structure I am using for my website.

However, I also want to translate the URL while relying on English only names in the actual source code (controller classes and action functions).

For example, here is a dummy controller with a single action (breaking with the example URL above due to simplification).

[Controller]
[Route("/Products")]
public class ProductController : Controller
{
    [ActionName("EnglishName")]
    public IActionResult EnglishName()
    {
        return View();
    }
}

For this simplified example:

  • URL examples:
    • English URL: https://example.com/Products/EnglishName
    • German URL: https://example.org/Produkte/GermanName
  • In source code (for clarity reasons), the value of Route of the controller shall not be changed.
  • In source code (for clarity reasons), the value of ActionName of the EnglishName action function shall not be changed.
  • In source code (for clarity reasons), the function name of the action EnglishName shall not be changed.
  • In routing context, the value of Route shall be "/Produkte" in the de-DE culture.
  • In routing context, the value of ActionName of the EnglishName action shall be "GermanName" in the de-DE culture.
  • All localizations shall be using the "usual way" (.resx files, like the stock localization for views etc.).

How can I achieve this without manually building a dynamic routing table?

Currently, I have no starting point. Search results for e.g. ASP.NET Core route localization just bring up answers for having the culture in the URL (except domain name), but this is not my use case. I tried to use variable names for the attributes, but of course this does not work.

1

1 Answers

2
votes

Filip Woj has pointed out a solution in his blog post.

He shows the usage of MapDynamicControllerRoute in combination with DynamicRouteValueTransformer class to translate the routes.