3

ASP.NET Core 2.2.0

I'm looking for language-based urls in ASP.NET Core. Found a lot of examples online for .NET Core MVC, but can't get it working in Razor Pages.

What I want to achieve is:

  • domain.com/informatie -> link to the 'information' page in default language (Dutch for me)
  • domain.com/en/information -> link to same the same page in English

Therefor I need 2 features:

  1. Recognize the language in the url, or set default language when language-tag is not provided
  2. Translate the names of my Pagemodels

I will write a custom method for translating the content.

Best fitting example

I found 2 examples that do almost the same I want, but they are using MVC:

The second one looks great, I tried to rewrite it for Razor Pages, but got stuck on the LocalizationController.cs (since I'm not using a controller, but a Pagemodel) CultureActionLinkTagHelper.cs (I use the asp-page attribute).

Is there anybody who has fixed this in Razor Pages and who would like to share the code? Or anybody who can help me through?

0

2 Answers 2

2

You need to build your application considering globalization/localization.

Here is the official documentation from Microsoft.

You may find this tutorial for Developing Multicultural Web Application helpful, it is for DotNetCore 2.1 Razor Pages, but it is valid for 2.2 as well.

Additionally, her is a sample project for localizing razor pages in GitHub

Sign up to request clarification or add additional context in comments.

5 Comments

Checked the tutorial and it helps me a lot. But the last missing link for me is how to translate the Pagemodels (the page-name in the url). Is that possible too?
do you want to have something like <a href="/en/register/">Register</a> for English and <a href="/es/registro/">Registro</a> for Spanish as an example? if so, I don't prefer to do that, it will add a complicated logic to the routing and it is not really necessary. Localizing only the display text will be enough.
Yes that is what I want. We have an international company selling products around the whole world, even to people who don't speak or read English. I really want it so that everyone can understand our url's. In this example link is a pretty simple logic for it, but it is written for MVC and not Razor.
@CribAd I have been looking to do this in MVC and am so surprised this doesn't seem to be a common thing - I am with you that for our non-english customers it is nice to give them URLS they can understand. after all the drive for MVC was to 'make URL's hackable'... I know you were looking for Razor Pages but I am so happy you found me something I can attempt in MVC!
@KrishanPatel you're welcome! Indeed the 2 linked examples are great for MVC!
0

I too was looking for a solution to have translated URLs in razor pages. I ended up with writing a small PageRouteModelConvention.

public class LocalRoute
{
    public string Page { get; set; }
    public List<string> Versions { get; set; }
}

public class LocalizedPageRouteModelConvention() : IPageRouteModelConvention
{
    private static List<LocalRoute> _routes =
    [
        new LocalRoute
        {
            Page = "/Pages/Matches.cshtml", //@page "/Matches"
            Versions = ["kamper"]
        },

        new LocalRoute
        {
            Page = "/Pages/Match.cshtml", //@page "/Match/{slug}"
            Versions = ["kamp/{slug}"]
        }
    ];
    
    public void Apply(PageRouteModel model)
    {
        var route = _routes.FirstOrDefault(p => p.Page == model.RelativePath);
        
        if (route == null) 
            return;
        
        //Clear default route, remove this if needed
        model.Selectors.Clear();
            
        foreach (var option in route.Versions)
        {
            model.Selectors.Add(new SelectorModel()
            {
                AttributeRouteModel = new AttributeRouteModel
                {
                    Template = option,
                }
            });
        }
    }
}

And updated my Program.cs:

builder.Services.AddRazorPages().AddRazorPagesOptions(options =>
{
    options.Conventions.Add(new LocalizedPageRouteModelConvention());
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.