问题描述
它很难找到一个答案,这个特定的问题,所以在这里生病流行起来:
Its hard to find an answer to this specific question, so ill pop it up here:
我们要完全建立我们的网址特定语言,意为
We want to build up our urls completely language specific, meaning
www.mysite.com/EN
www.mysite.com/DE
www.mysite.com/DE
这是在做RouteConfig以
this is done in the RouteConfig with
url: "{language}/{controller}/{action}/{id}"
但随后棘手的问题:
But then the tricky part:
www.mysite.com/EN/Categories
www.mysite.com/DE/Kategorien
www.mysite.com/DE/Kategorien
即。使controllername出现在多语言,但指向同一个控制器。这甚至可能?
I.e. make the controllername appear in a multiple languages, but point to the same controller. Is this even possible?
推荐答案
好吧,这是部分可能的(语言部分专用)。在不同语言中的控制器名称,绝对是一个有趣的问题,但我认为这将是很难做到这一点。想想会是什么样子的比迪语言,如阿拉伯语和希伯来语。它很可能是在不同的语言使用的控制器是一个好主意,但是你可以创建一个破坏自己,我相信,你将不得不改变底层结构MVC考虑到这一点。
Well, this is partially possible (the language part only). The controller name in a different language is definitely an interesting point, but I think it would be hard to achieve that. Think about what it would look like for Bidi languages like Arabic and Hebrew. It would probably be a good idea to use controller in different languages but you would create a havoc for yourself and I believe that you would have to change the underlying MVC structure to allow for this.
变化部分的语言很容易,可以像下面来完成。
The language changing part is easy and could be done like below.
你也许想看看是全球化。基本上语言部分对应于当前线程的UI培养。你需要的是以下内容:
What you might want to look at is the globalization. Basically the language part corresponds to the current threads UI culture. What you need is the following:
-
定义的路由,是这样的:
Define a route, something like this:
var lang = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
routes.MapRoute(
name: "Default",
url: "{language}/{controller}/{action}/{id}",
defaults: new { language = lang, controller = "Home",
action = "Index", id = UrlParameter.Optional }
);
注册 Application_AcquireRequestState
,并将其定义是这样的:
Register Application_AcquireRequestState
and define it something like this:
protected void Application_AcquireRequestState()
{
var routes = RouteTable.Routes;
var httpContext = Request.RequestContext.HttpContext;
if (httpContext == null) return;
var routeData = routes.GetRouteData(httpContext);
var language = routeData.Values["language"] as string;
var cultureInfo = new CultureInfo(language);
System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
}
虽然的CurrentUICulture
是你所需要的语言切换负载免受资源文件的信息,你也应该修改的CurrentCulture
相同的的CultureInfo
;
Although CurrentUICulture
is what you need switch languages load information from a resource file, you should also change the CurrentCulture
to the same CultureInfo
;
最后,请确保您有相应的资源文件和后备资源文件。
Last, make sure you have corresponding resource files and a fallback resource file.
我用名称
的CultureInfo
,所以你会德语去-DE,英语的en-US等财产。
这应该做的伎俩。
I used Name
property of CultureInfo
so you German would be de-DE, English en-US, etc.This should do the trick.
如果你需要我可以上传一个样本MVC项目为你检查的详细信息。
In case you need more information I could upload a sample MVC project for you to examine.
更新:做你想要什么,原油的方法是线路段的顺序颠倒的东西是这样的:
UPDATE: One crude way of doing what you want is to invert the order of route segments to something like this:
routes.MapRoute(
name: "NewDefault",
url: "{language}/{id}/{action}/{controller}",
defaults: new { language = lang, controller = "Home", action = "Index", id = "Category"}
);
这样你可以做一个下面的请求。在这种情况下Kategorien映射到 ID
,而不是一个控制器。您控制器在英语或德语住宿(取决于你如何把它命名为),但用户将看到一个不同的语言。在此背景下你的看法可能是这样的:
This way you could make a following request http://www.your_url.com/de/Kategorien. In this case Kategorien maps to the id
rather than a controller. You controller stays in English or German (depending how you named it) but user sees a different language. In the background your view might be something like this:
public ActionResult Index(string id, string categoryName)
{
// Fetch data based on category name (categoryName)
return View();
}
您可以传递更多的信息作为参数,但你需要调整你的路线,看起来像:{语言} / {类别} / {子类} / {行动} / {}控制器
You could pass additional information as parameters, but you will need to adjust your route to look something like: {language}/{category}/{subcategory}/{action}/{controller}
就知道,这可以从长期来看成为颈项强痛,如果你试试这个,然后确保你记录得很好。
Just know that this can become pain in the neck in the long run and if you try this, then make sure you document it well.
这篇关于使URL特定语言(通过路由)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!