I need multi-language URL route of existing controller. Let me explain more:
I have a controller with name "Product" and View with name "Software"; therefore, by default if the user enters "http://example.com/en/Product/Software", get right content (that really exists in http://example.com/Product/Software),
However, if another user -- a French user -- types "http://example.com/fr/Produi开发者_运维问答ts/logiciels", must get above controller and show with right content (same http://example.com/Product/Software but with French text).
Note: I set the route table with "{language}/{controller}/{action}/{id}"
Any other invalid URL must show the 404 page.
Is it possible?
Building upon Dan's post, I'm using the beneath for translating my controller and action names.
I created a table to store the values, this could and probably should be held in the resource files to keep everything together; however I used a database table as it works better with my companies processes.
CREATE TABLE [dbo].[RoutingTranslations](
[RouteId] [int] IDENTITY(1,1) NOT NULL,
[ControllerName] [nvarchar](50) NOT NULL,
[ActionName] [nvarchar](50) NOT NULL,
[ControllerDisplayName] [nvarchar](50) NOT NULL,
[ActionDisplayName] [nvarchar](50) NOT NULL,
[LanguageCode] [varchar](10) NOT NULL)
The RouteConfig.cs file was then changed to:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Build up routing table based from the database.
//This will stop us from having to create shedloads of these statements each time a new language, controller or action is added
using (GeneralEntities db = new GeneralEntities())
{
List<RoutingTranslation> rt = db.RoutingTranslations.ToList();
foreach (var r in rt)
{
routes.MapRoute(
name: r.LanguageCode + r.ControllerDisplayName + r.ActionDisplayName,
url: r.LanguageCode + "/" + r.ControllerDisplayName + "/" + r.ActionDisplayName + "/{id}",
defaults: new { culture = r.LanguageCode, controller = r.ControllerName, action = r.ActionName, id = UrlParameter.Optional },
constraints: new { culture = r.LanguageCode }
);
}
}
//Global catchall
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new {culture = CultureHelper.GetDefaultCulture(), controller = "Default", action = "Index", id = UrlParameter.Optional }
//defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
By default this will always use the English controller and action names, but allows you to provide an override by entering the values into the table.
(My internationalization code is largely based from this great blog post. http://afana.me/post/aspnet-mvc-internationalization-part-2.aspx)
As has been suggested before, this does depart from convention where the website urls (and routes) use English.
Nevertheless, it is possible, but in order to do it, you'll probably have to look at generating one route per action for every foreign language. So for a website with 20 actions and three languages (English, French, and German), you'll need 41 routes (20 French, 20 German and 1 English). Not the most efficient system, I admit, but it works as you want it to.
//You'll only need one of these, which is the default.
routes.MapRoute(
"English route",
"en/{controller}/{action}/{id}"
new { controller = "Home", action = "Index", language = "en" },
);
routes.MapRoute(
"FrenchHome",
"fr/Demarrer/Index/{id}",
new { controller = "Home", action = "Index", language = "fr" }
);
routes.MapRoute(
"GermanHome",
"de/Heim/Index/{id}", //'Heim' is, I believe the correct usage of Home in German.
new { controller = "Home", action = "Index", language = "de" }
);
//Some more routes...
routes.MapRoute(
"FrenchSoftware",
"fr/Produit/Logiciels/{id}",
new { controller = "Product", action = "Software", language = "fr" }
);
routes.MapRoute(
"GermanSoftware",
"de/Produkt/Software/{id}", //In this instance, Software should be the same in German and English.
new { controller = "Product", action = "Software", language = "de" }
);
//And finally, the 404 action.
routes.MapRoute(
"Catchall",
"{language}/{*catchall}",
new { controller = "Home", action = "PageNotFound", language = "en" },
new { language = "^(en|fr|de)$" }
);
//This is for the folks who didn't put a language in their url.
routes.MapRoute(
"Catchall",
"{*catchall}",
new { controller = "Home", action = "PageNotFound", language = "en" }
);
In your actions, for example Product/Software...
public ActionResult Software(string language, int id)
{
//This would go off to the DAL and get the content in whatever language you want.
ProductModel model = ProductService.GetSoftware(language, id);
return View(model);
}
I would LOVE it if somebody came along and said that there's a better way of doing this, because I agree that having the url in a foreign language isn't good, and given that the Internet itself is moving towards allowing non-Roman characters in urls, the sooner we look at solutions to this, the better.
Not only that, but I know proud French people don't like to see their website urls contain English. :)
You should have url something like "http://mysite.com/en/Product/Software" for English and "http://mysite.com/fr/Product/Software" for French, which would make much sense.
Use the same view for both.
Happy coding.
I strongly recommend the following approach to implement the multi-language in a MVC 5 (and <) web application, because I find it to be the most versatile one among those I've tried in the latest years.
You basically need to implement three things:
- A multi-language aware route to handle incoming URLs (if you're using MVC5 or above you could also go for Attribute-based routing instead, but I still prefer to use a global rule to handle this).
- A LocalizationAttribute to handle these kinds of multi-language requests.
- An helper method to generate these URLs within your application (
Html.ActionLink
and/orUrl.Action
extension methods).
See this answer for further details and code samples.
For additional info and further samples on this topic you can also read you can also read this blog post that I wrote on this topic.
精彩评论